Apple Watch,WatchKit扩展和主要应用程序 [英] Apple Watch, WatchKit Extension and main application

查看:172
本文介绍了Apple Watch,WatchKit扩展和主要应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

There is main application with logic and we extend app to Apple Watch.

添加目标后,xCode将创建另外两个应用程序:带代码的扩展和监视套件应用程序.

After adding target xCode creates 2 more applications: extension with code and watch kit application.

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

Question: How code from extension can reuse logic of ready and already made main iOS app? How extension app can communicate with main App and send commands.

推荐答案

要与包含的iPhone应用进行通信,您可以使用

To communicate to the containing iPhone app you can use

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

在您的WKInterfaceController

来自Apple文档

使用此方法与您所包含的iOS应用进行通信.呼唤 该方法使iOS在后台启动应用(根据需要) 并调用application:handleWatchKitExtensionRequest:reply:方法 它的应用程序委托.该方法具有以下签名:

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

应用程序委托接收您传递给userInfo的字典 参数并使用它来处理您提出的任何请求.如果它 提供回复,WatchKit执行您在 此方法的回复参数.

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.

这篇关于Apple Watch,WatchKit扩展和主要应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆