2016-03-23 51 views
1

我想用單例來管理WCSession消息,我發現herewatchOS WCSession'paired'和'watchAppAvailable'不可用

我明白它試圖做的,但我不明白爲什麼我收到一個錯誤......這是我掙扎行:

if let session = session where session.paired && session.watchAppInstalled { 

錯誤:「watchAppInstalled」不可用

錯誤: '配對' 是unavaiable

問:我怎樣才能使這些特性vailable?一般來說,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! 
     } 
    } 
} 
+0

您是否在iOS應用程序和WatchKit擴展中使用單例? – ccjensen

+0

@ccjensen是的,我最終都在使用它。我覺得我正在編寫watchos1,即使我看不出爲什麼...... – Charlie

回答

5

您所遇到的問題是,你同時使用了iOS SDK和watchOS SDK編譯時使用相同的單例。有些WCSession屬性只能在其中一個上使用,所以你的代碼將不得不考慮這一點。具體來說,如果你看的Objective-C標頭WCSession你會看到:

/** 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; 

這意味着,如果你想使用單保留,你將不得不改變這一節:

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

要更多的東西一樣(也有其他的方法來解決這個問題,但這是一個解決方案):

#if os(iOS) 
    if let session = session where session.paired && session.watchAppInstalled { 
     return session 
    } 
    return nil 
#else 
    return session 
#endif 

這是有條件編譯不同的代碼是否其正編譯d適用於iOS或watchOS。你可能不得不在單身人士的其他部分應用這個相同的技巧,但這至少應該讓你開始!