简体   繁体   English

使用Handkit和Watchkit将iPhone应用程序带到前台

[英]Using Handoff with Watchkit to bring the iPhone app to the foreground

I have a simple project where the user taps a button on the Apple Watch and some audio plays on the iPhone, this is easy enough to do with the openParentApplication method and having the handleWatchKitExtensionRequest code in AppDelegate. 我有一个简单的项目,用户点击Apple Watch上的按钮和iPhone上的一些音频播放,这很容易使用openParentApplication方法并在AppDelegate中使用handleWatchKitExtensionRequest代码。 However, while this works in the simulator, it will NOT work on the actual devices if the iPhone app is not already open. 但是,虽然这在模拟器中有效,但如果iPhone应用程序尚未打开,它将无法在实际设备上运行。 I'm trying to find if it's possible to use other methods, that will work even if the iPhone app isn't already open. 我试图找到是否可以使用其他方法,即使iPhone应用程序尚未打开也可以使用。

I've read on a stackoverflow answer here that it is possible to use Handoff to (partially) bring the phone app to the foreground, using WKInterfaceController updateUserActivity:userInfo:webpageURL: and UIApplicationDelegate application:continueUserActivity:restorationHandler . 我在这里阅读了stackoverflow的答案,可以使用Handoff(部分)将手机应用程序带到前台,使用WKInterfaceController updateUserActivity:userInfo:webpageURL:UIApplicationDelegate application:continueUserActivity:restorationHandler However, as a new developer I'm struggling to work out how to do this properly without any examples. 但是,作为一名新开发人员,我正在努力研究如何在没有任何示例的情况下正确地做到这一点。 Can anyone give some example code of how this would work, where both these are used together to run some code on the iphone app? 任何人都可以提供一些示例代码,说明这将如何工作,哪些都一起用于在iPhone应用程序上运行一些代码?

  1. Register your activity type names in your iphone application's plist. 在您的iphone应用程序的plist中注册您的活动类型名称。 Add a row named NSUserActivityTypes and make it an array. 添加名为NSUserActivityTypes的行并使其成为一个数组。 Eg: 例如:

在此输入图像描述

  1. Include the continueUserActivity: method in your AppDelegate. 在AppDelegate中包含continueUserActivity:方法。 Eg: 例如:

     -(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *))restorationHandler { // Extract the payload NSString *type = [userActivity activityType]; NSDictionary *userInfo = [userActivity userInfo]; // Assume the app delegate has a text field to display the activity information NSLog(@"User activity is of type %@, and user info %@", type, userInfo); restorationHandler(@[self.window.rootViewController]); return YES; } 
  2. In your watchkit controller's awakeWithContext, add the updateUserActivity method. 在watchkit控制器的awakeWithContext中,添加updateUserActivity方法。

     [self updateUserActivity:@"com.co.YourApp.watchkitextension.activity" userInfo:@{@"yo": @"dawg"} webpageURL:nil]; 

You should now see the app icon after opening the selected viewcontroller in your watch app. 现在,您应该在手表应用程序中打开所选的viewcontroller后看到应用程序图标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM