如何从我的WatchKit应用程序在iPhone上打开父应用程序?

Hie*_*ham 7 swift xcode6 watchkit

我正在尝试打开Apple Watch应用程序的父应用程序.

在Xcode Beta 2中,我们可以使用以下代码:

WKInterFaceController.openParentApplication
Run Code Online (Sandbox Code Playgroud)

但是,在Xcode beta 3中,我再也找不到代码了.现在我不知道如何从watch app打开父应用程序.请帮忙.

Dun*_*age 15

Objective-C方法是:

+ (BOOL)openParentApplication:(NSDictionary *)userInfo
                    reply:(void (^)(NSDictionary *replyInfo,
                                    NSError *error))reply
Run Code Online (Sandbox Code Playgroud)

Swift方法是:

class func openParentApplication(_ userInfo: [NSObject : AnyObject]!,
                    reply reply: (([NSObject : AnyObject]!,
                                    NSError!) -> Void)!) -> Bool
Run Code Online (Sandbox Code Playgroud)

因此,您需要将iPhone应用程序传递给reply()块,以便从WatchKit扩展程序中激活它.这是可以实现的一种方式,例如:

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

[WKInterfaceController openParentApplication:applicationData reply:^(NSDictionary *replyInfo, NSError *error) {
    NSLog(@"\nReply info: %@\nError: %@",replyInfo, error);
   }];
Run Code Online (Sandbox Code Playgroud)

您的iPhone应用程序的AppDelegate需要实现以下方法:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply {
NSString * request = [userInfo objectForKey:@"requestString"];

if ([request isEqualToString:@"executeMethodA"]) {
    // Do whatever you want to do when sent the message. For instance... 
    [self executeMethodABC];
}

// 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 'reply(nil)'.
// All of the objects in the dictionary [must be serializable to a property list file][3].
// If necessary, you can covert other objects to NSData blobs first. 
NSArray * objects = [[NSArray alloc] initWithObjects:myObjectA, myObjectB, myObjectC, nil];
NSArray * keys = [[NSArray alloc] initWithObjects:@"objectAName", @"objectBName", @"objectCName", nil];
NSDictionary * replyContent = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];

reply(replyContent);
}
Run Code Online (Sandbox Code Playgroud)

WKInterfaceController方法openParentApplication:reply:当iPhone(或iOS模拟器)解锁或锁定时,在后台启动包含应用程序.请注意,来自Apple的声明表明WatchKit扩展程序总是用于在后台启动您的iPhone应用程序,它只是模拟器的一个实现细节,它似乎在以前的测试版中在前台启动您的iPhone应用程序.

如果您想测试同时运行的WatchKit应用程序和iPhone应用程序,只需在Schemes菜单下从Xcode启动WatchKit应用程序,然后通过单击其跳板图标在模拟器中手动启动iPhone应用程序.

  • Hieu,在没有自己检查的情况下,对你回答问题的人说"你是否检查过",并不是很好的Stack Overflow练习.因为很明显你没有检查它:https://developer.apple.com/library/prerelease/ios/documentation/WatchKit/Reference/WKInterfaceController_class/index.html#//apple_ref/occ/clm/WKInterfaceController/openParentApplication:答复: (2认同)
  • @DuncanBabbage 这个答案是错误的。`openParentApplication` 不会在前台启动手机上的应用程序,只会在后台启动。你没有回答原来的问题。 (2认同)

stk*_*stk 5

如果您需要在前台打开您的父应用程序,请使用Handoff!

示例:

某处共享两者:

static let sharedUserActivityType = "com.yourcompany.yourapp.youraction"
static let sharedIdentifierKey = "identifier"
Run Code Online (Sandbox Code Playgroud)

在你的手表上:

updateUserActivity(sharedUserActivityType, userInfo: [sharedIdentifierKey : 123456], webpageURL: nil)
Run Code Online (Sandbox Code Playgroud)

在你的iPhone App Appate:

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
}
Run Code Online (Sandbox Code Playgroud)

最后(这一步很重要!):在你的Info.plist(s)中

在此输入图像描述