2016-03-23 13 views
1

Ben here bulundu WCSession iletileri yönetmek için bir singleton kullanmaya çalışıyorum.watchOS WCSession 'eşleştirilmiş' ve 'watchAppAvailable' kullanılamıyor

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

Hata:

bunu yapmaya çalışıyor anlamak, ancak bir hata alıyorum neden ben anlamıyorum ... Bu ben ile mücadele ediyorum çizgidir 'watchAppInstalled'

Soru 'eşli' unavaiable edilir: Nasıl bu özellikleri bir yapabilirsiniz kullanılamaz

Hata edilir ullanılabilir? WatchOS ve ios genel olarak yeni. Teşekkürler!

Bütün kodu:

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 uygulamasında ve WatchKit uzantısında singleton'u kullanıyor musunuz? – ccjensen

+0

@ccjensen evet, sonunda hem kullanıyorum. Neden olduğunu göremediğim halde watchos1 olarak derlediğimi hissediyorum. – Charlie

cevap

5

oluşuyor bir sorun iOS SDK ve watchos SDK ikisini de kullanarak derleme yaparken aynı singleton kullanarak olmasıdır. WCSession özelliklerinden bazıları yalnızca birinde veya diğerinde kullanılabilir, bu nedenle kodunuz bunu dikkate almak zorunda kalacaktır. Eğer WCSession için Objective-C başlığına bakmak Özellikle göreceğiniz:

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

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

Bu singleton'ununu kullanmaya devam etmek istiyorsanız eğer demektir, bu bölümü değiştirmek zorunda kalacak

gibi daha bir şey olması (bu çözmek için başka yolları da vardır, ama bu bir çözümdür):

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

Bu şartlı farklı bir kod derleme onun varlık derleme olmadığını iOS veya watchOS için d. Aynı numarayı tekillerin diğer kısımlarına da uygulamak zorunda kalabilirsiniz, ama bu en azından başlamanız gerekir!

İlgili konular