是否有可能从Watchkit扩展中知道iOS应用在前台运行?从watchkit扩展中获取iOS应用的状态
0
A
回答
0
苹果共享iPhone与手表之间的信息推荐的方法是通过一个应用程序组使用的共享对象:Apple Watch Programming Guide,请参见“共享数据与您包含的iOS应用程序”。
所以设置就可以使用AppDelegate中的applicationDidEnterBackground
和applicationWillEnterForeground
(或类似的方法适合你的需求)写在共享对象至极信息共享应用程序组可以通过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
相关问题
- 1. Watchkit扩展时的错误
- 2. 如何从核心iOS代码中引用WatchKit扩展中的Swift类?
- 3. iOS扩展 - 检测“扩展状态更改”(后台/前台)
- 4. 获取响应从background.js的铬扩展
- 5. Watchkit扩展程序无法从iCloud中读取
- 6. 参考我WatchKit扩展(SWIFT)
- 7. WatchKit 2.0扩展应用程序中的沙箱如何工作?
- 8. 如何获取iOS 10通知中的应用程序状态?
- 9. 从iOS 8中的PHAsset获取文件扩展名?
- 10. TestFlight不支持使用Watchkit扩展的应用程序
- 11. 如何从iOS8的扩展中获取应用程序版本?
- 12. 从文件获取扩展
- 13. WatchKit从iPhone中的关闭应用程序获取数据
- 14. watchkit扩展2 iOS应用程序在模拟器启动时崩溃
- 15. WatchKit App在Watch OS 3.0中不包含任何WatchKit扩展
- 16. Firefox扩展:共享状态
- 17. WatchKit中状态栏的高度
- 18. 从响应状态获取行号
- 19. 从用户获取publish_stream扩展权限
- 20. 从扩展类中获取类名称
- 21. 从文件中获取扩展属性
- 22. 从Air扩展中获取NSwindow
- 23. 从文件url中获取扩展
- 24. ReactJS:采用扩展元更新状态
- 25. 从Firefox扩展中读取HTTP响应
- 26. ngrx/strore示例应用程序,为什么子状态扩展了根状态?
- 27. 从ASyncResult获取状态
- 28. 从IntuitBatchResponse获取状态
- 29. 在iOS的共享扩展中为Safari获取URL在SLComposeServiceViewController中
- 30. 从休眠状态获取SQL获取