Socket communication does not work real Apple Watch (wOS 2)

Hello,

I developed an App, which sends and receives Data from a Server.

The App works fine with WatchOS 2 (On Simulator Watch) and the iPhone (iOS 9), which is coppled to the Apple Watch, BUT not on the real Watch with WatchOS 2.

I think, its an configuration problem of the projekt or the watch, because the communication works fine on the Simulator and the iPhone.


Thanks,

Chromo

Answered by DTS Engineer in 61690022

In watchOS 2 your watch extension is running on the watch itself. It’s typical for the watch not to have any active network interfaces, and thus BSD Sockets communication won’t work. [The watch has a Wi-Fi interface and can activate that interface, but that’s not something that’s under your control.]

Watch extensions are expected to access the network using NSURLSession. This will typically pass the request to the iPhone, allowing the watch to save power by not running the request locally. [Under some circumstances the watch will run the request itself but, again, this isn’t under your control.]

You have a couple of options here:

  • reformulate your networking in terms of NSURLSession

  • if that’s not possible (say, you’re talking to a server that requires a custom TCP-based protocol), do that work on the iPhone

With regards the last point, the Watch Connectivity framework makes it easy to pass work back and forth between your watch extension and your iPhone app.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Accepted Answer

In watchOS 2 your watch extension is running on the watch itself. It’s typical for the watch not to have any active network interfaces, and thus BSD Sockets communication won’t work. [The watch has a Wi-Fi interface and can activate that interface, but that’s not something that’s under your control.]

Watch extensions are expected to access the network using NSURLSession. This will typically pass the request to the iPhone, allowing the watch to save power by not running the request locally. [Under some circumstances the watch will run the request itself but, again, this isn’t under your control.]

You have a couple of options here:

  • reformulate your networking in terms of NSURLSession

  • if that’s not possible (say, you’re talking to a server that requires a custom TCP-based protocol), do that work on the iPhone

With regards the last point, the Watch Connectivity framework makes it easy to pass work back and forth between your watch extension and your iPhone app.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hello Eskimo,

Thanks for your quick answer, it helped me a lot.


I was confused by the fakt, that the code works on the simulator and not on the real watch.


Thanks,

Chromo

I just wanted to clarify a point from my previous post. In that post I skirted around the issue of whether BSD Sockets would work if the Watch’s Wi-Fi interface was up. I was vague because I didn’t have the details. Since then I’ve clarified this situation with watchOS Engineering:

  • The sandbox prevents your code running on the Watch from accessing the network via BSD Sockets.

  • This restriction also applies to APIs that use BSD Sockets internally, such as NSURLSessionStreamTask, NSStream (when doing networking), and CFSocketStream.

  • The Foundation-level APIs affected by this are flagged as

    __WATCHOS_PROHIBITED
    . For example, here’s the declaration of of
    +getStreamsToHostWithName:port:inputStream:outputStream:
    in the watchOS 2.0 SDK.

    (void)getStreamsToHostWithName:(NSString *)hostname port:(NSInteger)port inputStream:(NSInputStream * _nullable * _nullable)inputStream outputStream:(NSOutputStream * _nullable * _nullable)outputStream NSAVAILABLE(1010, 80) _WATCHOS_PROHIBITED;

  • The simulator doesn’t apply sandbox restrictions, which is why things were working for you on the simulator.

Finally, I want to reiterate my (correct) high-level advice from the previous post:

  • Use NSURLSession for HTTP[S].

  • If you need some other protocol, use Watch Connectivity to ask the iPhone to do that work for you.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Eskimo,

Hello, I am very beginner at Swift and I am trying to make NSURLSession with a watch extension. The communication works fine when the watch is paired with an iPhone via wifi. However, once the connection between the phone and watch is interrupted. The watch stops sending data to the server even when it is still connected to the wifi. I would like to make the watch independent in sending the data without pairing it to an iPhone. is it possible to do it and how?

I am trying to make NSURLSession with a watch extension

You should start a new thread for this (this thread has been dormant for 5 years and a bunch of stuff has changed since then). Tag it with Foundation and CFNetwork so that I see it. Feel free to reference any relevant details from this thread. Also, make sure you read Matt’s Low-Level Networking on watchOS for some important background.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Greetings,

I started a new thread here is the link : https://developer.apple.com/forums/thread/689544

Socket communication does not work real Apple Watch (wOS 2)
 
 
Q