ViewController不符合协议MSBandClientManagerDelegate

时间:2015-03-30 04:44:31

标签: microsoft-band

我正在尝试使用Swift来实现Microsoft Band SDK。我在尝试设置代码时遇到了这个错误。

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

我以前从未见过这个,但我也从未试过将Objective C示例转换为Swift。

任何帮助将不胜感激!

编辑:这是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

编辑2:使用建议的Swift Helper类后

这就是我尝试建立连接的方式。

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

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

我不知道应该如何设置

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

然后,当试图将照片发送到乐队时,此功能不起作用

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

通过使用帮助程序类,我被抛弃了。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

示例项目

我链接了一个可以向乐队发送触觉的Microsoft Band Kit iOS的示例Swift项目。在此处找到链接:http://droolfactory.blogspot.com/2015/03/ios-swift-example-of-connecting-with.html


Microsoft Band Bridging Header

首先转换要与Swift一起使用的Objective-C类,创建一个Bridging Header。对于MicrosoftBandKit-iOS框架,我看起来像这样:

#ifndef ModuleName_Bridging_Header_h
#define ModuleName_Bridging_Header_h

#import <MicrosoftBandKit_iOS/MicrosoftBandKit_iOS.h>

#endif

确保将ModuleName替换为应用模块的名称。有关桥接头文件的更多信息,请访问:https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html


乐队助手类

接下来,我将MSBClientManagerDelegate包装在一个帮助器类(BandManager)中,该类使用单例来管理Band。我在这里有一个要点(https://gist.github.com/mthistle/8f6eb30c68a918fc6240

这个要点的代码是:

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])
    }

}