【问题标题】:WatchKit openParentApplication:replyWatchKit openParentApplication:回复
【发布时间】:2015-06-07 08:53:18
【问题描述】:

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

我的 Xcode 版本是 6.3.2 和 iOS 8.3。

可能是什么问题?

接口控制器.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);
         }];
    }
}

编辑 - 正确答案: 在 cmets 中查看 mohammed alwaili 的链接

【问题讨论】:

  • 看我的问题。 link
  • 就是这样,伙计!谢谢:)!

标签: ios iphone xcode6 watchkit


【解决方案1】:

openParentApplication:reply 请求必须立即返回,因此您必须请求额外的时间才能完成您的 asynchronous 请求(或者运行 synchronous 请求,但这是糟糕的做法 )。

来自Apple WatchKit Developer Tips and Best Practices

如果您在 Apple Watch 上的应用需要执行更长时间的后台运行 诸如网络通话之类的任务,您应该依靠您的 iPhone 应用程序来完成这项工作。使用 WKInterfaceController 中的 openParentApplication:reply: 方法在后台唤醒您的 iPhone 应用程序并返回您的 WatchKit 扩展所需的数据。处理 WatchKit 请求的 UIApplicationDelegate 方法必须立即返回。 如果需要异步调用,例如执行网络连接,请使用后台任务确保您的应用在有机会发送回复之前没有挂起。

【讨论】:

    【解决方案2】:

    我遇到了类似的问题,必须解决一些问题才能让我的 WatchKit 应用成功调用 iOS 应用,该应用进行了异步 API 调用。

    这是一个工作代码 sn-p

    func application(application: UIApplication, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]?, reply: (([NSObject : AnyObject]!) -> Void)!) {
        let backgroundProcessingToken = application.beginBackgroundTaskWithName("backgroundApiCall", expirationHandler: { () -> Void in
            reply(["response":["error":"SOME_ERROR_CODE_INDICATING_TIMEOUT"]])
        })
    
        request(.GET, "https://api.forecast.io/forecast/[INSERT DARK SKY API CODE]/37.8267,-122.423").responseJSON(options: NSJSONReadingOptions.AllowFragments, completionHandler:{request, response, data, error in
            if(error != nil || data == nil){
                reply(["response":["error":"SOME_ERROR_CODE_INDICATING_FAILURE"]])
            }
    
            if let json = data as? NSDictionary {
                reply(["response":["data":json]])
            }
    
            application.endBackgroundTask(backgroundProcessingToken)
        })
    }
    

    最终,您需要注册为后台任务,以确保您的应用不会被操作系统杀死。

    我在 github FWIW 上还有一个工作示例 here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多