2015-04-05 58 views

回答

0

苹果共享iPhone与手表之间的信息推荐的方法是通过一个应用程序组使用的共享对象:Apple Watch Programming Guide,请参见“共享数据与您包含的iOS应用程序”

所以设置就可以使用AppDelegate中的applicationDidEnterBackgroundapplicationWillEnterForeground(或类似的方法适合你的需求)写在共享对象至极信息共享应用程序组可以通过watchkit延伸阅读后:

的AppDelegate

func applicationDidEnterBackground(application: UIApplication) { 
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 

    // Shared object 
    let sharedDefaults = NSUserDefaults(suiteName: "group.com.example.myApp") 

    sharedDefaults.setBool(false, forKey: "foreground") 
    sharedDefaults.synchronize() 
} 

func applicationWillEnterForeground(application: UIApplication) { 
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 

    // Shared object 
    let sharedDefaults = NSUserDefaults(suiteName: "group.com.example.myApp") 

    sharedDefaults.setBool(true, forKey: "foreground") 
    sharedDefaults.synchronize() 
} 

Watchkit扩展

...一些在你需要的信息,其中...

class MainInterfaceController: WKInterfaceController {  

    override init() { 
     // Initialize variables here. 
     super.init() 
    } 


    override func willActivate() { 
     // This method is called when watch view controller is about to be visible to user 
     super.willActivate() 

     let sharedDefaults = NSUserDefaults(suiteName: "group.com.example.myApp")! 

     let isForeground = sharedDefaults.boolForKey("foreground") 
     ... 

    } 

    override func didDeactivate() { 
     // This method is called when watch view controller is no longer visible 
     super.didDeactivate() 
    } 
} 
0

正如你知道这个函数发送watchKit应用请求,iOS应用程序,并与handleWatchKitExtensionRequest iOS应用将抓住这一请求。所以下面的函数有reply这是负责处理您的信息或数据的连接。所以在appDelegate中,您应该为reply提供任何值,然后在watchKit控制器中检查此值。

class func openParentApplication(_ userInfo: [NSObject : AnyObject], 
           reply reply: (([NSObject : AnyObject], 
               NSError?) -> Void)?) -> Bool