Launch host app from watch app

断了今生、忘了曾经 提交于 2019-11-27 11:45:30

问题


I know that the openParentApplication api in watch kit extension can open the host app in the background but not in the foreground.

I also tried using openUrl() api of NSExtensionContext as below:

NSExtensionContext *ctx = [[NSExtensionContext alloc] init];

NSURL *url = [NSURL URLWithString:@"myScheme://today"];
[ctx openURL:url completionHandler:^(BOOL success) {
    NSLog(@"fun=%s after completion. success=%d", __func__, success);
}];
[ctx completeRequestReturningItems:ctx.inputItems completionHandler:nil];

Here too the host app is not launched. Am I missing something? or is it not possible to launch the host app from watch kit extension?


回答1:


If you need to open your parent app in the foreground, use Handoff!

https://developer.apple.com/handoff/

Example:

Somewhere shared for both:

static let sharedUserActivityType = "com.yourcompany.yourapp.youraction"
static let sharedIdentifierKey = "identifier"

on your Watch:

updateUserActivity(sharedUserActivityType, userInfo: [sharedIdentifierKey : 123456], webpageURL: nil)

on your iPhone in App Delegate:

func application(application: UIApplication, willContinueUserActivityWithType userActivityType: String) -> Bool {
    if (userActivityType == sharedUserActivityType) {
        return true
    }
    return false
}

func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]!) -> Void) -> Bool {
    if (userActivity.activityType == sharedUserActivityType) {
        if let userInfo = userActivity.userInfo as? [String : AnyObject] {
            if let identifier = userInfo[sharedIdentifierKey] as? Int {
                //Do something
                let alert = UIAlertView(title: "Handoff", message: "Handoff has been triggered for identifier \(identifier)" , delegate: nil, cancelButtonTitle: "Thanks for the info!")
                alert.show()
                return true
            }
        }
    }
    return false
}

And finally (this step is important!!!): In your Info.plist(s)




回答2:


As of Beta 3 of iOS 8.2 it is currently not possible to open iOS app to foreground. As you said openParentApplication can open app in background. Unfortunately there is no sign of API to open app on iPhone.

Multiple posts on Apple Dev Forums mentioned that it's not possible

https://devforums.apple.com/message/1076125#1076125

Correct, a notification can still declare a background action that the iPhone app will handle, so in that sense it can launch the iPhone app. But the iPhone app cannot be brought to the foreground by a WatchKit app.

And other post

https://devforums.apple.com/message/1082620#1082620

On a device, it[Watch app] will not - bring your iOS app to the foreground.




回答3:


I'm hopeful that Apple will provide API for launching the parent app from a watch app in a future beta, but for now I've managed to achieve it by doing the following...

Call openParentApplication:reply: as normal:

- (void)openPhoneApp {
    [WKInterfaceController openParentApplication:[NSDictionary new] reply:nil];
}

Implement application:handleWatchKitExtensionRequest:reply: in your AppDelegate, and launch itself using a custom URL scheme:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"myappscheme://open"]];
}


来源:https://stackoverflow.com/questions/27700907/launch-host-app-from-watch-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!