gpt4 book ai didi

ios - Watchkit-应用程序 : handleWatchKitExtensionRequest error

转载 作者:行者123 更新时间:2023-12-01 17:51:12 25 4
gpt4 key购买 nike

通过上述方法请求 XML 项字典时出现以下错误:

> NSLocalizedDescription=The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]

我传递一个由 NSString 的 NSMutableArray 组成的 NSDictionary 没有问题。

从接口(interface) Controller :
- (void) requestFeedsFromPhone
{
[WKInterfaceController openParentApplication:@{@"request":@"feeds"}
reply:^(NSDictionary *replyInfo, NSError *error) {

// the request was successful
if(error == nil) {

// get the array of items
NSMutableDictionary *tempDictionary = replyInfo[@"feeds"];


NSLog(@"tempDictionary: %@", tempDictionary[@"feedsArray"]);
self.feeds = tempDictionary[@"feedsArray"];
[self setupTable];
}
else{
NSLog(@"ERROR: %@", error);
}
}];
}

在应用委托(delegate)中:
- (void) application:(UIApplication *)application
handleWatchKitExtensionRequest:(NSDictionary *)userInfo
reply:(void (^)(NSDictionary *))reply
{

MasterViewController *mainController = (MasterViewController*) self.window.rootViewController;

//this is the troublesome line - calling this method results in the error
NSDictionary *feedsDictionary = [mainController returnFeedsDictionary];

reply(@{@"feeds": feedsDictionary});
}

在 MasterViewController 中:
-(NSDictionary *) returnFeedsDictionary
{

NSURL *url = [NSURL URLWithString:@"http://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss"];
SeparateParser *separateParser = [[SeparateParser alloc] initWithURL:url];
[separateParser parse];
NSArray *tempArray = [separateParser returnFeeds];
return @{@"feedsArray": tempArray};

}
returnFeeds方法返回一个由 NSMutableDictionarys 组成的 NSMutableArray,其中填充了 NSMutableStrings(标题、链接、imageURL 等)。

我假设我的问题是我的一些数据不符合属性列表,但我认为数组、字符串和字典是可以接受的。

最佳答案

您正在点击 Law of Leaky Abstractions .

您的 iPhone-App 和 WatchKit-Extension 作为两个独立的进程和两个独立的沙箱运行。 OpenParentApplication/handleWatchKitExtensionRequest/reply 机制只是 Apple 为包装进程内通信而提供的一种便利机制。

抽象泄漏:

回复(feeds 字典)

返回的字典只能包含

  • NSData
  • NSString
  • NSD日期
  • NSArray
  • NS词典

    如果您的返回字典包含除此之外的任何内容,例如自定义
    类实例,然后你会得到那个糟糕的错误,说 reply() 是
    从来没有打电话,即使你打电话!



  • 解决方案一 :序列化您的对象并将其从应用程序发送到 watch ,然后在 watch 上反序列化(又名编码/解码)。
  • 这需要使用 NSCoding,如上一篇文章所述:序列化为 NSData* 流,发送回复(数据流),然后在 WatchKit 扩展中反序列化。
  • NSCoding 是一种痛苦。您需要在发送前显式使用 NSKeyedArchiver 进行序列化,并使用 NSKeyedUnarchiver 反序列化 tutorial here .
  • 需要大量繁琐的代码来支持类 Object 本身内的编码/解码(以满足 NSCoder)

  • 解决方案二 :只需传递一个字符串作为 Key,并在 Watch Extension 中实例化您的对象(而不是传递胖对象实例本身)
  • 通过单击 {class}.m、按 command+option+1 并单击 Target Membership 下的“...WatchKit Extension”复选框,允许 WatchKit Extension 针对您的类文件进行编译(无论如何,您都需要这样做,即使使用解决方案 1)
  • 例如您的 WatchKit 扩展将分配/初始化模型的一个新实例,而不是试图通过
  • 传递它。
  • 例如您可以将 URL 作为字符串传递,WatchKit 扩展会进行 NSURLConnection 调用。
  • 关于ios - Watchkit-应用程序 : handleWatchKitExtensionRequest error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30060914/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com