0

我想从后台加载WatchKit界面中的一些信息。REST是否有一些快速的方法来执行主机iOS应用程序传递的 URL 请求,或者我必须利用裸WatchConnectivity功能来编写特定的协议?

4

4 回答 4

0

尽管 Apple 的示例很快见https://developer.apple.com/videos/play/wwdc2015-228/?time=345,但我在 NSProcessInfo 包装器中的 obejctive-c utlis 中粘贴了一个片段。

Apple 的示例也使用了 Semaphore,如果扩展被置于睡眠状态,我在下面省略了。

遵循以下模式,您可以直接从手表中获得所需的服务。

NSProcessInfo *processInfo = [NSProcessInfo processInfo];

[processInfo performExpiringActivityWithReason:@"networkReq" usingBlock:^(BOOL expired) {
    if (!expired){            
        NSLog(@"we have an assertion");        
        NSTimeInterval secondsInThreeHours = 3 * 60 * 60;
        NSURL *forecast = [NSURL URLWithString:@"http::address"];

        NSURLSession *session = [NSURLSession sharedSession];
        [[session dataTaskWithURL:forecast
                completionHandler:^(NSData *data,
                                    NSURLResponse *response,
                                    NSError *error) {
                    // handle response
         }] resume];
    else{

        NSLog(@"Not background assertion or we are out of time");
    }
}];
于 2016-01-31T21:34:43.753 回答
0

您应该直接从 WatchKit 扩展中使用 NSURLSession。

于 2015-10-11T02:47:37.627 回答
0

NSURLSession 可以很好地与 Watchkit 配合使用,但如果相关,您也需要在 Watchkit 扩展上添加 Allows Arbitrary Loads。

请看这个帖子,

NSURLSession 使用 Objective-C 在 Watch OS2 上将数据返回为 Null

于 2015-10-14T11:12:33.707 回答
0

我正在使用 sendMessageData:replyHandler:,但我无法让它正常工作。相关票在: https ://stackoverflow.com/questions/33211760/sendmessagedata-does-not-call-didreceivemessagedata-on-the-counterpart

于 2015-10-19T11:57:30.827 回答