2015-11-04 45 views
0

我正在使用模擬器,因爲我沒有Apple Watch來測試此功能。我的應用程序「應用程序委託」中包含此代碼。手錶連接不正常

import UIKit 
import CoreData 
import Parse 
import WatchConnectivity 

@available(iOS 9.0, *) 
extension AppDelegate : WCSessionDelegate { 

    func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) 
    { 

    } 

    // Sender 

    @available(iOS 9.0, *) 
    func transferUserInfo(userInfo: [String : AnyObject]) -> WCSessionUserInfoTransfer? { 
     return WCSession.defaultSession().transferUserInfo(userInfo) 
    } 

    @available(iOS 9.0, *) 
    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 
    } 

    @available(iOS 9.0, *) // Receiver 
    func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) { 

    } 
} 


@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

     if #available(iOS 9.0, *) { 

      if WCSession.isSupported() { 
       let session = WCSession.defaultSession() 
       session.delegate = self 
       session.activateSession() 

       WCSession.defaultSession().transferUserInfo(["hello" : "Hey Alvin Varghese"]) 
      } 

     } else { 
     } 
    } 
    } 

正如你們可以看到的那樣,我通過「userInfo」發送了一個虛擬數據。我正在Apple Watch interfaceController中成功接收它。代碼如下。

import WatchKit 
import Foundation 
import WatchConnectivity 

@available(iOS 9.0, *) 
extension StartingPageInterfaceController : WCSessionDelegate { 

    // Sender 

    @available(iOS 9.0, *) 
    func transferUserInfo(userInfo: [String : AnyObject]) -> WCSessionUserInfoTransfer? { 
     return WCSession.defaultSession().transferUserInfo(userInfo) 
    } 

    @available(iOS 9.0, *) 
    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 
    } 

    @available(iOS 9.0, *) // Receiver 
    func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) { 

     dispatch_async(dispatch_get_main_queue()) { 


      if let hey = userInfo["hello"] as? String 
      { 
       let alert : WKAlertAction = WKAlertAction(title: "Okay", style: WKAlertActionStyle.Default, handler: { 
       }) 

       self.presentAlertControllerWithTitle("Okay", message: hey, preferredStyle: WKAlertControllerStyle.Alert, actions: [alert]) 
      } 
     } 
    } 
} 

class StartingPageInterfaceController: WKInterfaceController { 

    @IBOutlet var searchButton: WKInterfaceButton! 
    override func awakeWithContext(context: AnyObject?) { 
     super.awakeWithContext(context) 

     if #available(iOS 9.0, *) { 

      if WCSession.isSupported() { 
       let session = WCSession.defaultSession() 
       session.delegate = self 
       session.activateSession() 
      } 

     } else { 
     } 

    } 
    } 

好吧,現在我在我的模擬器iPhone 6中運行這個,我可以看到安裝成功。現在我轉移到蘋果手錶32毫米模擬器,並點擊應用程序,這是剛剛安裝,打開並顯示這樣的警報消息 - <「好的你好Alvin Varghese」(雖然父應用程序是在iPhone 6模擬器打開)。現在,我直接將Apple Watch應用程序安裝到Apple Watch 32毫米模擬器中,沒有發生任何事情,我期待着相同的警報消息(雖然父應用程序未在iPhone 6模擬器中打開)。

如果我在Apple Watch 32mm模擬器中運行Apple Watch應用程序並立即打開父項,則可以看到警報消息。那麼這裏發生了什麼?我讀過Watch Connectivity框架將在後臺進行通信。

因此,從這我可以看出,這隻適用於父設備是積極的,還是因爲我在模擬器上測試它?它會在物理設備中正常工作嗎?

我的問題很簡單,當我打開蘋果手錶我想從我的父應用程序的數據。我怎麼能做到這一點?

讓我知道你的想法,提前致謝。

回答

1

首先,我們假設您想在實例變量infoNeeded中獲得所需的信息。你將不得不在你的類聲明如下(例如)

var infoNeeded : [String : AnyObject]? 

現在,當你希望得到所需要的信息(比如,例如在applicationDidBecomeActive方法在ExtensionDelegate在WatchOS擴展),你需要從手錶發送消息到iPhone。此外,您還必須準備好從iOS獲取您需要的數據的潛在響應。對於這一點,你做

let replyHandler: ([String : AnyObject]) -> Void = { reply in 
    self.infoNeeded = reply 
} 

let msg = ["InfoType" : "MainInfo"] // This must be something that allows the iOS WCSessionDelegate to know what info it must provide in the reply 

WCSession.defaultSession().sendMessage(msg, replyHandler:replyHandler, errorHandler:nil) // you can pass an error handler if you wish 

然後,在iOS裝置WCSessionDelegate(在你的情況,你的AppDelegate),你就必須實現該功能

func session(_ session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler replyHandler: ([String : AnyObject]) -> Void) { 
    let data: [String : AnyObject] = <whatever info you need to pass> 
    replyHandler(data) 
} 

有了這個,你那是什麼手錶中的變量infoNeeded充滿了在iOS中創建的data

+0

好吧,我明白了。但你說我不能通過使用userInfo實現這一點? –