如何从我的 WatchKit 应用打开 iPhone 上的父应用?
How can I open the parent app on iPhone from my WatchKit app?
我正在尝试打开我的 Apple Watch 应用程序的父应用程序。
在 Xcode Beta 2 中,我们可以使用此代码:
WKInterFaceController.openParentApplication
但是,在 Xcode beta 3 中,我再也找不到该代码了。现在我不知道如何从手表应用程序打开父应用程序。请帮忙
+ (BOOL)openParentApplication:(NSDictionary *)userInfo
reply:(void (^)(NSDictionary *replyInfo,
NSError *error))reply
class func openParentApplication(_ userInfo: [NSObject : AnyObject]!,
reply reply: (([NSObject : AnyObject]!,
NSError!) -> Void)!) -> Bool
因此您需要向 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);
}];
您的 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);
}
WKInterfaceController 方法 openParentApplication:reply:当 iPhone(或 iOS 模拟器)解锁或锁定时,在后台启动包含的应用程序。请注意,Apple 的声明表明 WatchKit 扩展始终旨在在后台启动您的 iPhone 应用程序,而它似乎只是在模拟器中启动您的 iPhone 应用程序的实现细节以前测试版的前景。
如果您想同时测试您的 WatchKit 应用和 iPhone 应用 运行,只需从“方案”菜单下的 Xcode 启动 WatchKit 应用,然后手动启动单击其跳板图标,在模拟器中启动您的 iPhone 应用程序。
如果您需要在前台打开父应用程序,请使用Handoff!
示例:
两者共享的地方:
static let sharedUserActivityType = "com.yourcompany.yourapp.youraction"
static let sharedIdentifierKey = "identifier"
在您的手表上:
updateUserActivity(sharedUserActivityType, userInfo: [sharedIdentifierKey : 123456], webpageURL: nil)
您在 App Delegate 中的 iPhone:
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
}
最后(这一步很重要!!!):在你的Info.plist(s)
我正在尝试打开我的 Apple Watch 应用程序的父应用程序。
在 Xcode Beta 2 中,我们可以使用此代码:
WKInterFaceController.openParentApplication
但是,在 Xcode beta 3 中,我再也找不到该代码了。现在我不知道如何从手表应用程序打开父应用程序。请帮忙
+ (BOOL)openParentApplication:(NSDictionary *)userInfo
reply:(void (^)(NSDictionary *replyInfo,
NSError *error))reply
class func openParentApplication(_ userInfo: [NSObject : AnyObject]!,
reply reply: (([NSObject : AnyObject]!,
NSError!) -> Void)!) -> Bool
因此您需要向 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);
}];
您的 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);
}
WKInterfaceController 方法 openParentApplication:reply:当 iPhone(或 iOS 模拟器)解锁或锁定时,在后台启动包含的应用程序。请注意,Apple 的声明表明 WatchKit 扩展始终旨在在后台启动您的 iPhone 应用程序,而它似乎只是在模拟器中启动您的 iPhone 应用程序的实现细节以前测试版的前景。
如果您想同时测试您的 WatchKit 应用和 iPhone 应用 运行,只需从“方案”菜单下的 Xcode 启动 WatchKit 应用,然后手动启动单击其跳板图标,在模拟器中启动您的 iPhone 应用程序。
如果您需要在前台打开父应用程序,请使用Handoff!
示例:
两者共享的地方:
static let sharedUserActivityType = "com.yourcompany.yourapp.youraction"
static let sharedIdentifierKey = "identifier"
在您的手表上:
updateUserActivity(sharedUserActivityType, userInfo: [sharedIdentifierKey : 123456], webpageURL: nil)
您在 App Delegate 中的 iPhone:
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
}
最后(这一步很重要!!!):在你的Info.plist(s)