ios - WatchKit openParentApplication :reply

标签 ios iphone xcode6 watchkit

我目前有一个问题。 我想要的行为:如果我打开我的 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/

相关文章:

ios - 如何使用音乐播放器的 UI 在 iOS 中播放音频?

ios - Xcode 7 需要很长时间才能生成构建。它几乎卡在管理器窗口中

ios - 收到错误 "ReactiveCocoa/ReactiveCocoa.h"找不到文件

ios - 在复杂的 View 层次结构中查找 CGRect

ios - 不兼容的操作数类型 ('NSUInteger'(又名 'unsigned long')和 'id _Nullable')

ios - 如何在用户重启手机后继续跟踪位置?

ios - 有没有办法获取我的 iOS 应用程序屏幕截图列表?

iphone - 奇怪的行为 : Message Sent to deallocated instance

iphone - 处理二进制 Plist

Swift,sprite kit 游戏 : Have circle disappear in clockwise manner? 定时器?