Swift中Watchkit扩展NSURLSession的问题

时间:2022-12-03 00:26:56

I'm trying to call my local API to turn off my house lighting from my watch, but I'm having trouble with calling a url from within the watchkit extension.

我正在尝试调用我的本地API来关闭我手表中的房屋照明,但是我在watchkit扩展中调用url时遇到了麻烦。

In my ViewController (iPhone app) I've got exactly the same code (which works), but somehow it doesn't work when I call it in the InterfaceController from the Watchkit Extension.

在我的ViewController(iPhone应用程序)中,我有完全相同的代码(可以工作),但是当我从Watchkit扩展中的InterfaceController中调用它时,它不知何故。

@IBAction func confirmTapped() {

    let url = NSURL(string: "http://homeserver.local/api/lights/4/0")
    let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
        print(NSString(data: data!, encoding: NSUTF8StringEncoding))
    }

    task!.resume()
}

I've also attached a gist: https://gist.github.com/f72125cd4678069af7af.git

我还附上了一个要点:https://gist.github.com/f72125cd4678069af7af.git

Am I missing something obvious?

我错过了一些明显的东西吗

2 个解决方案

#1


1  

I don't know if it's a good thing to start a NSURLSession from within the Watch extension. If the request takes to long, it will be cancelled.

我不知道从Watch扩展中启动NSURLSession是否是一件好事。如果请求需要很长时间,它将被取消。

Maybe you could ping the watch via openParentApplication:reply and handle the NSURLSession on your iPhone app.

也许您可以通过openParentApplication ping手表:回复并处理iPhone应用程序上的NSURLSession。

#2


2  

While Frederick's advice was solid advice for WatchKit 1, openParentApplication:reply is no longer available in WatchKit 2. With WatchKit 2, you would instead definitely open the NSURLSession in the WatchKit App Extension itself, because using a WCSession means that the URL would only be opened if and when the iPhone application itself is running, either in the foreground or if it happens to still be running in the background because the system has not closed it.

虽然Frederick的建议是针对WatchKit 1的可靠建议,但在WatchKit 2中不再提供openParentApplication:reply。使用WatchKit 2,你肯定会在WatchKit App Extension中打开NSURLSession,因为使用WCSession意味着URL只会是如果和当iPhone应用程序本身正在运行时打开,无论是在前台还是由于系统尚未关闭它仍然在后台运行。

It is possible that the problems you were experiencing were because you needed to add the 'Allows Arbitrary Loads' property on the WatchKit App Extension. See discussion in this possibly related thread: NSURLSession returns data as Null on Watch OS2 using Objective-C

您遇到的问题可能是因为您需要在WatchKit App Extension上添加“允许任意载入”属性。请参阅此可能相关的线程中的讨论:NSURLSession使用Objective-C在Watch OS2上将数据返回为Null

#1


1  

I don't know if it's a good thing to start a NSURLSession from within the Watch extension. If the request takes to long, it will be cancelled.

我不知道从Watch扩展中启动NSURLSession是否是一件好事。如果请求需要很长时间,它将被取消。

Maybe you could ping the watch via openParentApplication:reply and handle the NSURLSession on your iPhone app.

也许您可以通过openParentApplication ping手表:回复并处理iPhone应用程序上的NSURLSession。

#2


2  

While Frederick's advice was solid advice for WatchKit 1, openParentApplication:reply is no longer available in WatchKit 2. With WatchKit 2, you would instead definitely open the NSURLSession in the WatchKit App Extension itself, because using a WCSession means that the URL would only be opened if and when the iPhone application itself is running, either in the foreground or if it happens to still be running in the background because the system has not closed it.

虽然Frederick的建议是针对WatchKit 1的可靠建议,但在WatchKit 2中不再提供openParentApplication:reply。使用WatchKit 2,你肯定会在WatchKit App Extension中打开NSURLSession,因为使用WCSession意味着URL只会是如果和当iPhone应用程序本身正在运行时打开,无论是在前台还是由于系统尚未关闭它仍然在后台运行。

It is possible that the problems you were experiencing were because you needed to add the 'Allows Arbitrary Loads' property on the WatchKit App Extension. See discussion in this possibly related thread: NSURLSession returns data as Null on Watch OS2 using Objective-C

您遇到的问题可能是因为您需要在WatchKit App Extension上添加“允许任意载入”属性。请参阅此可能相关的线程中的讨论:NSURLSession使用Objective-C在Watch OS2上将数据返回为Null