[英]The UIApplicationDelegate in the iPhone App never called reply
I am trying to launch my iPhone app from watch simulator using the below code : 我正在尝试使用以下代码从手表模拟器启动我的iPhone应用程序:
WKInterfaceController subclass WKInterfaceController子类
[WKInterfaceController openParentApplication:[NSDictionary dictionaryWithObject:@"red" forKey:@"color"] reply:^(NSDictionary *replyInfo, NSError *error) {
NSLog(@"replyInfo %@",replyInfo);
NSLog(@"Error: %@",error);
}];
AppDelegate.m AppDelegate.m
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply
{
NSLog(@"appdelegate handleWatchKitExtensionRequest");
NSLog(@"NSDictionary: %@",userInfo);
NSLog(@"replyInfo: %@",replyInfo);
}
The error I am getting is : 我得到的错误是:
Error: Error Domain=com.apple.watchkit.errors Code=2 "The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]" UserInfo=0x7f8603227730 {NSLocalizedDescription=The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]}
错误:错误Domain = com.apple.watchkit.errors Code = 2“iPhone App中的UIApplicationDelegate从未调用reply() - [UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]”UserInfo = 0x7f8603227730 {NSLocalizedDescription = iPhone中的UIApplicationDelegate应用程序永远不会调用reply() - [UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]}
You need to call the reply block, even if you return nil
. 您需要调用回复块,即使您返回
nil
。 The following will resolve your error: 以下将解决您的错误:
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply
{
NSLog(@"appdelegate handleWatchKitExtensionRequest");
NSLog(@"NSDictionary: %@",userInfo);
NSLog(@"replyInfo: %@",replyInfo);
reply(nil);
}
See the Apple documentation for further information. 有关详细信息,请参阅Apple文档 。 You can also return an NSDictionary
reply(myNSDictionary);
您还可以返回NSDictionary
reply(myNSDictionary);
with whatever information it would be useful to return to your Watchkit extension, although the dictionary can only contain information that can be serializable to a property list file, so for instance you can pass strings but you can't just pass a dictionary containing references to instances of your custom classes without packaging them up as NSData first. 尽管字典只能包含可以序列化到属性列表文件的信息,但是对于返回到Watchkit扩展名的任何信息都是有用的,所以例如你可以传递字符串但是你不能只传递包含引用的字典。您的自定义类的实例,而不是首先将它们打包为NSData。
Aside from just not calling the reply block, this can happen for at least a couple reasons: 除了不调用回复块之外,这可能至少有几个原因:
On the phone side build your reply dictionary: 在手机端构建您的回复词典:
NSMutableDictionary *reply = [NSMutableDictionary new];
MyCustomObject *myObject = <something you need to send>;
reply[@"myKey"] = [NSKeyedArchiver archivedDataWithRootObject: myObject];
NSAttributedString *myString = <some attributed string>;
reply[@"otherKey"] = [NSKeyedArchiver archivedDataWithRootObject: myString];
And unpack it back on the watch side: 然后将其打开放回手表侧:
NSData *objectData = replyInfo[@"myKey"];
MyCustomObject *myObject = [NSKeyedUnarchiver unarchiveObjectWithData: objectData];
NSData *stringData = replyInfo[@"otherKey"];
NSAttributedString *myString = [NSKeyedUnarchiver unarchiveObjectWithData: stringData];
I would like to add that it is important to start a background task in handleWatchKitExtensionRequest as specified in the documentation . 我想补充一点,在文档中指定的handleWatchKitExtensionRequest中启动后台任务很重要。 This ensures that the main app on the iPhone is not suspended before it can send its reply.
这可以确保iPhone上的主应用程序在发送回复之前不会被暂停。 (Not initiating a background task does not cause a problem in the simulator or when the iPhone app is active. However, it causes a problem when the iPhone app is inactive.)
(不启动后台任务不会在模拟器中或iPhone应用程序处于活动状态时出现问题。但是,当iPhone应用程序处于非活动状态时会导致问题。)
Code in the app delegate of the main app on iPhone: iPhone主应用程序的app代理中的代码:
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void ( ^)( NSDictionary * ))reply
{
__block UIBackgroundTaskIdentifier watchKitHandler;
watchKitHandler = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"backgroundTask"
expirationHandler:^{
watchKitHandler = UIBackgroundTaskInvalid;
}];
if ( [[userInfo objectForKey:@"request"] isEqualToString:@"getData"] )
{
// get data
// ...
reply( data );
}
dispatch_after( dispatch_time( DISPATCH_TIME_NOW, (int64_t)NSEC_PER_SEC * 1 ), dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
[[UIApplication sharedApplication] endBackgroundTask:watchKitHandler];
} );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.