我在使用 Watchkit 应用程序时遇到错误。当我启动它时,我要求包含的 iOS 应用程序从网络获取一些数据。问题是我收到一条错误消息,指出包含的应用程序从不调用“reply()”:o 但是查看我的代码,它应该调用它。
我尝试调试从 openParentApplication 到“reply()”调用的每一步,它似乎运行良好 =X
这是我在 Watchkit 扩展中的代码
- (void)initDiaporamasWithSuccess:(void (^)())success andFailure:(void (^)(NSError*))failure {
NSLog(@"Ask to load diapos");
__weak typeof(self) weakSelf = self;
[WKInterfaceController openParentApplication:@{@"watchKit": @"watchKit.initDiapos"} reply:^(NSDictionary *replyInfo, NSError *error) {
if (error) {
NSLog(@"%@", error);
if (failure) {
failure(error);
}
return;
}
NSLog(@"got items : %@", replyInfo[@"diapos"]);
weakSelf.diaporamas = replyInfo[@"diapos"];
[weakSelf setDiaporama:replyInfo[@"firstDiapo"] AtIndex:0];
if (success) {
success();
}
}];
结果应该是一个包含 NSArray 的 NSDictionary 和一些 diaporamas 的基本信息,以及一个对象 (Diapo) 包含第一个 diaporamas 的完整信息(例如 self.diaporamas[0])
这是包含应用程序的 AppDelegate 中的代码:
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply {
// Maybe we could handle multiple watchKit extension calls that way ?
// Something like a key-value 'protocol' to run the right block of code
NSString *watchKitCall = userInfo[@"watchKit"];
NSLog(@"watchKit handled");
if ([watchKitCall isEqualToString:@"watchKit.initDiapos"]) {
[AppDelegate watchInitialObjects:^(NSDictionary *info) {
NSLog(@"Managed to get initial infos");
reply(info);
} failure:^(NSError *error) {
NSLog(@"Fail : %@", error);
reply(@{@"error": error});
}];
}
+ (void) watchInitialObjects:(void (^)(NSDictionary *info))success failure:(void (^)(NSError *error))failure {
NSDictionary *parameters = @{@"site" : @(14), @"limit" : @(10)};
[AppDelegate requestDiapoListWithParams:parameters success:^(NSArray *items) {
if ([items count] == 0)
{
NSError *error = [NSError errorWithDomain:@"com.domain.app" code:404 userInfo:nil];
failure(error);
return;
}
Diapo *firstDiapo = [items firstObject];
[AppDelegate requestDiapoDetailWithDiapo:firstDiapo success:^(Diapo *diapo) {
if (!diapo)
{
NSError *error = [NSError errorWithDomain:@"com.domain.app" code:404 userInfo:nil];
failure(error);
return;
}
NSDictionary *result = @{
@"firstDiapo" : diapo,
@"diapos" : items
};
success(result);
} failure:^(NSError *error) {
failure(error);
}];
} failure:^(NSError *error) {
failure(error);
}];
在 watchKitHandler 中,我调用 watchInitialObjects 来获取 diaporamas 数组和第一个 diaporama 的信息。 在 watchInitialObjects 中,我进行了第一次网络调用以获取数组,成功后,我进行了另一个网络调用以获取第一个 diaporama 信息。
为了进行调用并将 JSON 映射到对象,我使用 RESTKit
我真的不明白错误是什么=x
更新
我忘了写我得到的错误,这里是:
Error Domain=com.apple.watchkit.errors Code=2 "The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]" UserInfo=0x7fcb53e12830 {NSLocalizedDescription=The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]}
我一直想知道为什么会出现这个错误,我想我找到了: 似乎有一个(很少)超时来完成包含应用程序中的工作。但是我映射了我直接在包含应用程序中收到的 JSON 数据,然后在 reply() 中发送这些自定义对象。但是当我删除映射部分时,它运行良好!
所以...这就是为什么我认为这是问题所在 =X 有没有人可以赞同我的想法或纠正我?
最佳答案
经过几个小时的搜索和测试不同的代码,我终于找到了我的问题...当我们认真阅读有关“application:handleWatchKitExtensionRequest:reply:”的 Apple 文档时很明显...
这是答案:(在文档中)
The contents of the dictionary must be serializable to a property list file.
这意味着对象只能是字典、数组、字符串、数字(整数和 float )、日期、二进制数据或 bool 值
...我觉得很蠢><
关于ios - 打开父应用程序 :reply: error with asynchronous network call in containing app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29141095/