Search code examples
microsoft-band

ViewController does not conform to protocol MSBandClientManagerDelegate


I am trying to use Swift to implement the Microsoft Band SDK. I keep getting this error when trying to set up my code.

class ViewController: UIViewController, UITableViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, MSBClientManagerDelegate, UIScrollViewDelegate {

I have never seen this before, but I have also never tried to convert an Objective C sample to Swift.

Any help would be appreciated!

EDIT: Here is the protocol from Objective C

@protocol MSBClientManagerDelegate<NSObject>

- (void)clientManager:(MSBClientManager *)clientManager clientDidConnect:(MSBClient *)client;
- (void)clientManager:(MSBClientManager *)clientManager clientDidDisconnect:(MSBClient *)client;
- (void)clientManager:(MSBClientManager *)clientManager client:(MSBClient *)client didFailToConnectWithError:(NSError *)error;

@end

EDIT 2: After using suggested Swift Helper class

This is how I am trying to set up the connection.

 var clients:NSArray = bandHelper.attachedClients()!
    var firstClient: MSBClient = clients[0] as MSBClient

if (clients.count == 0){
    println("The band is not detected")
    return
}

I have no clue how this should be set up

bandHelper.connectClient(firstClient, {completion: (connected:true -> void in)})
println("Please wait...connecting to band")

Then, when trying to send a photo to the band, this function does not work

bandHelper.client?.personalizationManager.updateMeTileImage(bandScaledImage, { (completionHandler: NSError!) -> Void in
           NSLog("%@", NSError())})

I am getting thrown off by using the helper class. Any help would be appreciated!


Solution

  • Sample Project

    I linked a sample Swift project for Microsoft Band Kit iOS that can send a haptic to the band. Find the link here: http://droolfactory.blogspot.com/2015/03/ios-swift-example-of-connecting-with.html


    Microsoft Band Bridging Header

    First to convert the Objective-C classes to be used with Swift, create a Bridging Header. Mine look like this for just the MicrosoftBandKit-iOS framework:

    #ifndef ModuleName_Bridging_Header_h
    #define ModuleName_Bridging_Header_h
    
    #import <MicrosoftBandKit_iOS/MicrosoftBandKit_iOS.h>
    
    #endif
    

    Make sure to replace the ModuleName with the name of your apps Module. Find more on Bridging Header files at: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html


    Band Helper Class

    Next I wrapped the MSBClientManagerDelegate in a helper class (BandManager) which uses a singleton to manage the Band. I have a gist for it here (https://gist.github.com/mthistle/8f6eb30c68a918fc6240)

    The code for this the gist is:

    import Foundation
    
    let kConnectionChangedNotification = "kConnectionChangedNotification"
    let kConnectionFailedNotification  = "kConnectionFailedNotification"
    
    private let _SharedBandManagerInstance = BandManager()
    
    class BandManager : NSObject, MSBClientManagerDelegate {
    
        private(set) var client: MSBClient?
        private var connectionBlock: ((Bool) -> ())?
        private var discoveredClients = [MSBClient]()
    
        private var clientManager = MSBClientManager.sharedManager()
    
        class var sharedInstance: BandManager {
            return _SharedBandManagerInstance
        }
    
        override init() {
            super.init()
            self.clientManager.delegate = self
        }
    
        func attachedClients() -> [MSBClient]? {
            if let manager = self.clientManager {
                self.discoveredClients = [MSBClient]()
                for client in manager.attachedClients() {
                    self.discoveredClients.append(client as! MSBClient)
                }
            }
            return self.discoveredClients
        }
    
        func disconnectClient(client: MSBClient) {
            if (!client.isDeviceConnected) {
                return;
            }
            if let manager = self.clientManager {
                manager.cancelClientConnection(client)
                self.client = nil
            }
        }
    
        func connectClient(client: MSBClient, completion: (connected: Bool) -> Void) {
            if (client.isDeviceConnected && self.client == client) {
                if (self.connectionBlock != nil)
                {
                    self.connectionBlock!(true)
                }
                return;
            }
    
            if let connectedClient = self.client {
                self.disconnectClient(client)
            }
    
            self.connectionBlock = completion;
            self.clientManager.connectClient(client)
        }
    
        func clientManager(clientManager: MSBClientManager!, clientDidConnect client: MSBClient!) {
            if (self.connectionBlock != nil) {
                self.client = client
                self.connectionBlock!(true)
                self.connectionBlock = nil
            }
    
            self.fireClientChangeNotification(client)
        }
    
        func clientManager(clientManager: MSBClientManager!, clientDidDisconnect client: MSBClient!) {
            self.fireClientChangeNotification(client)
        }
    
        func clientManager(clientManager: MSBClientManager!, client: MSBClient!, didFailToConnectWithError error: NSError!) {
            if error != nil {
                println(error)
            }
            NSNotificationCenter.defaultCenter().postNotificationName(kConnectionFailedNotification, object: self, userInfo: ["client": client])
        }
    
        func fireClientChangeNotification(client: MSBClient) {
            NSNotificationCenter.defaultCenter().postNotificationName(kConnectionChangedNotification, object: self, userInfo: ["client": client])
        }
    
    }