简体   繁体   中英

WCSession issue with Xcode 7.3

Helo

Before updating Xcode to the 7.3 version I had an app with an WatchOS 2 app, The watch app would call the func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) { and the iOS app would pick the call and insert the passed value. All was fine.

But since updating to Xcode 7.3 one issue i noticed, the func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) { is being called twice ONLY when the iOS app is launched for the first time, if the app is running or is in the background, that function is only called once.

If I pass the values 1, 5, and 10 and the iOS app is not running, the values 1, 5, 10, 1, 5, and 10 are added. But if the app is running in any form, the values 1, 5, and 10 are added.

Any idea why?

Here is the code from the WatchOS side, I did think of that myself, but according to my tests they are only called once. I have done many tests, and this is only happening when the iOS app is launched, not when its ruling in the background.

@IBAction func ConfirmButtonPressed() {

    let applicationDict = ["Amount out":  self.AmountText    ]// Create a dict of application data
      //applicationDict = ["status":   "0"   ]// Create a dict of application data
    WCSession.defaultSession().transferUserInfo(applicationDict)
}

Here is the iOS app code from the app delegate

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

    if (WCSession.isSupported()) {
        print("xyz3")
        session = WCSession.defaultSession()
        session.delegate = self
        session.activateSession()
    }

..........

func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {

    var status = false
    var AmountUILabel = ""

          status = false
         AmountUILabel  = userInfo["Amount out"]  as! String
        print(userInfo["Amount out"]  )

    let i  =  NSString (string:   AmountUILabel ).doubleValue
      let when = NSDate()
     let list :[AnyObject] =  controller.viewControllers!
    let j = list[1].topViewController  as! AllEntriesTableViewController

    j.AddAmount(i , date:  when, what: "---", status: status)
   }

I was able to figure out the answer after a whole day of research. I should have started the didReceiveUserInfo with dispatch_async

That fixed it and increased up the communication speed between the watch app and the iOS one.

func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
    dispatch_async(dispatch_get_main_queue()) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM