菜鸟教程小白 发表于 2022-12-13 10:41:24

ios - 了解 handleWatchKitExtensionRequest


                                            <p><p>我正在测试一些代码在 iPhone 应用上的执行。我遵循 Apple 建议的文档(不使用后台任务,仅使用 <strong>控制台日志</strong>)。但是我在控制台上没有得到任何东西(我想看到字符串“howdy”)。</p>

<p>这是因为我<strong>在模拟器上运行 WatchKit 扩展应用</strong>吗?还是我遗漏了什么?</p>

<hr/>

<p>苹果说:</p>

<blockquote>
<p>If you are using openParentApplication:reply:, make sure you create a
background task immediately upon entering
application:handleWatchKitExtensionRequest:reply:. This will make sure
that the iPhone app gets time in the background instead of being
suspended again. Additionally, wrap the call to endBackgroundTask: in
a dispatch_after of 2 seconds to ensure that the iPhone app has time
to send the reply before being suspended again.</p>
</blockquote>

<p>我在 WatchKit 扩展上的实现(链接到按钮的操作方法):</p>

<pre><code>- (IBAction)sendMessageToApp{

    NSString *requestString = ; // This string is arbitrary, just must match here and at the iPhone side of the implementation.
    NSDictionary *applicationData = [ initWithObjects:@ forKeys:@[@&#34;theRequestString&#34;]];

    [WKInterfaceController openParentApplication:applicationData reply:^(NSDictionary *replyInfo, NSError *error) {
      NSLog(@&#34;\nReply info: %@\nError: %@&#34;,replyInfo, error);
    }];

    NSLog(@&#34;sending message..&#34;);
}
</code></pre>

<p>我在 <strong>AppDelegate.m</strong> 文件上的实现:</p>

<pre><code>- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply {
    NSString * request = ;

    NSLog(@&#34;howdy&#34;);


    // This is just an example of what you could return. The one requirement is
    // you do have to execute the reply block, even if it is just to &#39;reply(nil)&#39;.
    // All of the objects in the dictionary .
    // If necessary, you can covert other objects to NSData blobs first.
    NSArray * objects = [ initWithObjects:,,, , nil];

    NSArray * keys = [ initWithObjects:@&#34;objectAName&#34;, @&#34;objectdName&#34;, @&#34;objectBName&#34;, @&#34;objectCName&#34;, nil];
    NSDictionary * replyContent = [ initWithObjects:objects forKeys:keys];

    reply(replyContent);
}
</code></pre>

<p>我在控制台中得到了什么:</p>

<pre><code>2015-04-16 14:43:44.034 FanLink WatchKit Extension &lt;InterfaceController: 0x608000081fe0&gt; initWithContext
2015-04-16 14:44:04.848 FanLink WatchKit Extension sennding message..
2015-04-16 14:44:04.854 FanLink WatchKit Extension
Reply info: {
    objectAName = &#34;2015-04-16 13:44:04 +0000&#34;;
    objectBName = &#34;2015-04-16 13:44:04 +0000&#34;;
    objectCName = &#34;2015-04-16 13:44:04 +0000&#34;;
    objectdName = &#34;2015-04-16 13:44:04 +0000&#34;;
}
Error: (null)
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这是因为您只是在调试 WatchKit 扩展目标。如果您还想在控制台中查看主应用程序日志,则需要通过 Xcode 调试器附加主应用程序的进程。
转到调试-->附加一个进程-->(然后按标识符选择并输入您的应用程序名称)</p>

<p>为了更好地浏览,我为您找到了这个很好的资源,专门用于 WatchKit/WatchKit 扩展调试:
<a href="https://mkswap.net/m/blog/How+to+debug+an+iOS+app+while+the+WatchKit+app+is+currently+running+in+the+simulator" rel="noreferrer noopener nofollow">https://mkswap.net/m/blog/How+to+debug+an+iOS+app+while+the+WatchKit+app+is+currently+running+in+the+simulator</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 了解 handleWatchKitExtensionRequest,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/29677132/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/29677132/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 了解 handleWatchKitExtensionRequest