Swift watchOS-WCSession';成对';和';手表可用';不可用

Swift watchOS-WCSession';成对';和';手表可用';不可用,swift,watchkit,watchos-2,wcsession,Swift,Watchkit,Watchos 2,Wcsession,我正在尝试使用单例来管理WCSession消息,我发现了它 我明白它想做什么,但我不明白为什么我会出错。。。这是我正在努力解决的问题: if let session = session where session.paired && session.watchAppInstalled { 错误:“watchAppInstalled”不可用 错误:“配对”不可用 问题:如何使这些属性可用?一般来说,watchOS和ios都是新产品。谢谢 完整代码: import WatchCon

我正在尝试使用单例来管理WCSession消息,我发现了它

我明白它想做什么,但我不明白为什么我会出错。。。这是我正在努力解决的问题:

if let session = session where session.paired && session.watchAppInstalled {
错误:“watchAppInstalled”不可用

错误:“配对”不可用

问题:如何使这些属性可用?一般来说,watchOS和ios都是新产品。谢谢

完整代码:

import WatchConnectivity

class WatchSessionManager: NSObject, WCSessionDelegate {

    static let sharedManager = WatchSessionManager()
    private override init() {
        super.init()
    }

    private let session: WCSession? = WCSession.isSupported() ? WCSession.defaultSession() : nil

    private var validSession: WCSession? {

        // paired - the user has to have their device paired to the watch
        // watchAppInstalled - the user must have your watch app installed

        // Note: if the device is paired, but your watch app is not installed
        // consider prompting the user to install it for a better experience

        if let session = session where session.paired && session.watchAppInstalled {
            return session
        }
        return nil
    }

    func startSession() {
        session?.delegate = self
        session?.activateSession()
    }
}

// MARK: Application Context
// use when your app needs only the latest information
// if the data was not sent, it will be replaced
extension WatchSessionManager {

    // Sender
    func updateApplicationContext(applicationContext: [String : AnyObject]) throws {
        if let session = validSession {
            do {
                try session.updateApplicationContext(applicationContext)
            } catch let error {
                throw error
            }
        }
    }

    // Receiver
    func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
        // handle receiving application context

        dispatch_async(dispatch_get_main_queue()) {
            // make sure to put on the main queue to update UI!
        }
    }
}

// MARK: User Info
// use when your app needs all the data
// FIFO queue
extension WatchSessionManager {

    // Sender
    func transferUserInfo(userInfo: [String : AnyObject]) -> WCSessionUserInfoTransfer? {
        return validSession?.transferUserInfo(userInfo)
    }

    func session(session: WCSession, didFinishUserInfoTransfer userInfoTransfer: WCSessionUserInfoTransfer, error: NSError?) {
        // implement this on the sender if you need to confirm that
        // the user info did in fact transfer
    }

    // Receiver
    func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
        // handle receiving user info
        dispatch_async(dispatch_get_main_queue()) {
            // make sure to put on the main queue to update UI!
        }
    }

}

// MARK: Transfer File
extension WatchSessionManager {

    // Sender
    func transferFile(file: NSURL, metadata: [String : AnyObject]) -> WCSessionFileTransfer? {
        return validSession?.transferFile(file, metadata: metadata)
    }

    func session(session: WCSession, didFinishFileTransfer fileTransfer: WCSessionFileTransfer, error: NSError?) {
        // handle filed transfer completion
    }

    // Receiver
    func session(session: WCSession, didReceiveFile file: WCSessionFile) {
        // handle receiving file
        dispatch_async(dispatch_get_main_queue()) {
            // make sure to put on the main queue to update UI!
        }
    }
}


// MARK: Interactive Messaging
extension WatchSessionManager {

    // Live messaging! App has to be reachable
    private var validReachableSession: WCSession? {
        if let session = validSession where session.reachable {
            return session
        }
        return nil
    }

    // Sender
    func sendMessage(message: [String : AnyObject],
        replyHandler: (([String : AnyObject]) -> Void)? = nil,
        errorHandler: ((NSError) -> Void)? = nil)
    {
        validReachableSession?.sendMessage(message, replyHandler: replyHandler, errorHandler: errorHandler)
    }

    func sendMessageData(data: NSData,
        replyHandler: ((NSData) -> Void)? = nil,
        errorHandler: ((NSError) -> Void)? = nil)
    {
        validReachableSession?.sendMessageData(data, replyHandler: replyHandler, errorHandler: errorHandler)
    }

    // Receiver
    func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
        // handle receiving message
        dispatch_async(dispatch_get_main_queue()) {
            // make sure to put on the main queue to update UI!
        }
    }

    func session(session: WCSession, didReceiveMessageData messageData: NSData, replyHandler: (NSData) -> Void) {
        // handle receiving message data
        dispatch_async(dispatch_get_main_queue()) {
            // make sure to put on the main queue to update UI!
        }
    }
}

您遇到的问题是,在同时使用iOS SDK和watchOS SDK进行编译时,您使用的是同一个单例。某些
WCSession
属性仅在其中一个上可用,因此您的代码必须考虑到这一点。特别是,如果您查看
WCSession
的Objective-C标题,您将看到:

/** Check if iOS device is paired to a watch */
@property (nonatomic, readonly, getter=isPaired) BOOL paired __WATCHOS_UNAVAILABLE;

/** Check if the user has the Watch app installed */
@property (nonatomic, readonly, getter=isWatchAppInstalled) BOOL watchAppInstalled __WATCHOS_UNAVAILABLE;
这意味着如果您想继续使用singleton,则必须更改此部分:

if let session = session where session.paired && session.watchAppInstalled {
    return session
}
return nil
更像(有其他方法可以解决这个问题,但这是一个解决方案):


这是有条件地编译不同的代码,无论是针对iOS还是watchOS编译。您可能必须在singleton的其他部分应用相同的技巧,但这至少应该让您开始

你在iOS应用程序和WatchKit扩展中都使用singleton吗?@ccjensen是的,我最终都在使用它。我觉得我正在编译watchos1,尽管我不明白为什么。。。
#if os(iOS)
    if let session = session where session.paired && session.watchAppInstalled {
        return session
    }
    return nil
#else
    return session
#endif