ios - Apple Watch、WatchKit Extension 和主要应用程序

标签 ios program-entry-point watchkit

有逻辑的主要应用程序,我们将应用程序扩展到 Apple Watch。

添加目标后,xCode 创建了另外 2 个应用程序:带代码的扩展和 watch 套件应用程序。

问题:扩展中的代码如何重用现成的和已经制作好的主 iOS 应用程序的逻辑?扩展应用程序如何与主应用程序通信并发送命令。

最佳答案

要与包含的 iPhone 应用程序通信,您可以使用

(BOOL)openParentApplication:(NSDictionary *)userInfo
                        reply:(void (^)(NSDictionary *replyInfo,
                                        NSError *error))reply

在你的WKInterfaceController

来自 Apple 文档

Use this method to communicate with your containing iOS app. Calling the method causes iOS to launch the app in the background (as needed) and call the application:handleWatchKitExtensionRequest:reply: method of its app delegate. That method has the following signature:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo
reply:(void(^)(NSDictionary *replyInfo))reply

The app delegate receives the dictionary you pass into the userInfo parameter and uses it to process whatever request you made. If it provides a reply, WatchKit executes the block you provided in the reply parameter of this method.

关于ios - Apple Watch、WatchKit Extension 和主要应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27190949/

相关文章:

iOS Today Extension 在 iPhone 上不断崩溃,而不是在模拟器中

ios - 更新 iOS 11 中的左侧导航项目

使用 IntelliJ 调试 Go 单个文件

c++ - 使用 main() 重用文件中的类

ios - 删除 Apple Watch 应用程序后是否可以发布 iOS 应用程序更新?

ios - 单网页的iOS App能否上传到App Store(**Note :my app contains only one screen** )

ios - 如何关闭我所有的应用程序声音?

java - 在 Java 中使用另一个类从 main 绘制到 GUI

ios - 创建 WatchKit PNG 动画

ios - Apple WatchKit,构建具有与操作系统相同的 UI 的应用程序