【问题标题】:Supplying a custom error to +[WKInterfaceController openParentApplication:reply:]向 +[WKInterfaceController openParentApplication:reply:] 提供自定义错误
【发布时间】:2015-04-01 18:06:50
【问题描述】:

+[WKInterfaceController openParentApplication:reply:]reply 参数是一个接收(NSDictionary *replyInfo, NSError *error) 的块。

replyInfo 参数由-[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:] 方法提供。

但是,第二种方法的reply 参数是一个只接受(NSDictionary *replyInfo) 的块;没有error 参数。

有没有办法从我的 iPhone 应用程序中提供自定义 NSError 作为 +[WKInterfaceController openParentApplication:reply:] 的参数,或者该参数是否为系统保留? (例如,如果手表和手机之间出现通信错误。)


这是我想做的一个例子:

在 WatchKit 中:

WKInterfaceController.openParentApplication(nil, reply: { (response, error) -> Void in
    if error != nil {
        println("Error: \(error)")
    } else {
        doSomethingUseful()
    }
}

UIApplicationDelegate:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply {
    if (inBadState) {
       reply(nil, [NSError errorWithDomain:errorDomain code:errorCode userInfo:nil]);
    } else {
       reply([self response], nil);
    }
}

注意:这是一个精简的示例,没有考虑userInforesponse 参数;实际使用会考虑这些。

【问题讨论】:

    标签: ios watchkit uiapplicationdelegate


    【解决方案1】:

    很遗憾,您无法访问error 参数。我所做的是将任何错误作为字符串传递并使用通用字典键。然后,我检查 WatchKit 端是否存在该密钥并记录它发现的任何错误。可以肯定的是,这并不理想。

    【讨论】:

      猜你喜欢
      • 2020-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-30
      • 2011-06-04
      • 2020-10-14
      相关资源
      最近更新 更多