gpt4 book ai didi

ios - WatchKit openParentApplication :reply

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:21:25 27 4
gpt4 key购买 nike

我目前有一个问题。我想要的行为:如果我打开我的 WatchKit 应用程序,我会调用“openParentApplication”。我收到了我想要的数据。但是如果我在真实设备上测试,它就不起作用,因为我在 iPhone 中打开了父应用程序。但是,当我在模拟器中进行测试时,它可以在不打开父应用程序的情况下工作。

我的 Xcode 版本是 6.3.2 和 iOS 8.3。

可能是什么问题?

InterfaceController.m

- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];

NSDictionary *userInfo = @{@"request":@"refreshData"};
[WKInterfaceController openParentApplication:userInfo reply:^(NSDictionary *replyInfo, NSError *error)
{
entries = replyInfo;
NSLog(@"Reply: %@",replyInfo);
[self reloadTable];
[self.city setText:[entries valueForKey:@"city"][0] ];
}];

}

AppDelegate.m

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply
{
NSString *refresh = [userInfo valueForKey:@"request"];
if([refresh isEqualToString:@"refreshData"])
{
NSString *city = [[NSUserDefaults standardUserDefaults] stringForKey:@"City"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:[NSString stringWithFormat:@"http://blackdriver.adappter.de/api/retrieve.php?city=%@",[city stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
{
reply(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error: %@", error);
}];
}
}

编辑 - 正确答案:请参阅评论中来自 mohammed alwaili 的链接

最佳答案

openParentApplication:reply 请求必须立即返回,因此您必须请求额外的时间来完成您的异步 请求(或者运行一个同步 请求,但这是糟糕的做法)。

来自 Apple WatchKit Developer Tips and Best Practices :

If your app on Apple Watch needs to perform longer running background tasks, such as networking calls, you should rely on your iPhone app to do the work. Use the openParentApplication:reply: method in WKInterfaceController to wake up your iPhone app in the background and return the data that your WatchKit extension needs. The UIApplicationDelegate method that handles the WatchKit request must return immediately. If an asynchronous call is required, to perform networking for example, use a background task to make sure your app is not suspended before it has a chance to send its reply.

关于ios - WatchKit openParentApplication :reply,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30691703/

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