简体   繁体   English

iPhone应用程序中的UIApplicationDelegate从未调用过回复

[英]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: 除了不调用回复块之外,这可能至少有几个原因:

  1. Your iPhone app crashed while it was processing the request and therefore was never able to call the reply block. 您的iPhone应用程序在处理请求时崩溃,因此无法调用回复块。 Check that you are not accidentally putting nil into an NSMutableDictionary, as that will cause a crash. 检查您是否不小心将nil放入NSMutableDictionary中,因为这会导致崩溃。
  2. You are trying to put something that can't be serialized into a plist file into the replyInfo dictionary (hat tip to @duncan-babbage). 你试图将一些无法序列化的东西放入一个plist文件到replyInfo字典中(帽子提示到@duncan-babbage)。 If you need to pass an NSAttributedString or your custom object, make sure it conforms to NSCoding and do this: 如果需要传递NSAttributedString或自定义对象,请确保它符合NSCoding并执行以下操作:

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.

相关问题 “ iPhone应用程序中的UIApplicationDelegate从未在-[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]中调用reply()” - “The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]” “ iPhone应用程序中的UIApplicationDelegate从未调用reply()”-Xamarin - “The UIApplicationDelegate in the iPhone App never called reply() ” - Xamarin “ iPhone应用程序中的UIApplicationDelegate从未调用reply()”错误突然 - 'The UIApplicationDelegate in the iPhone App never called reply()' error all of a sudden 错误Domain = com.apple.watchkit.errors代码= 2“ iPhone应用程序中的UIApplicationDelegate从未调用Reply()nsmutablearray - Error Domain=com.apple.watchkit.errors Code=2 "The UIApplicationDelegate in the iPhone App never called reply() nsmutablearray 应用收到通知,但在第一次会话中未调用UIApplicationDelegate方法 - App receives notification but UIApplicationDelegate method isn't called in first session 不调用UIApplicationDelegate方法 - UIApplicationDelegate methods not being called iPhone应用程序永远不会在设备上运行 - iphone app never runs on device 从未在应用图标触摸启动中调用didReceiveLocalNotification - didReceiveLocalNotification is never called in app icon touched launch productsRequest 从未在应用程序购买中调用 swift 5 - productsRequest never called in app purchases swift 5 对于iPad 1或iPhone 3G设备,ViewDidAppear永远不会被调用 - ViewDidAppear never gets called for iPad 1 or iPhone 3G devices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM