Socket connection from WatchOS 2 and CFStream

三世轮回 提交于 2019-12-12 18:31:28

问题


I have to connect a remote server via tcp socket from the Watch.

I have already written a piece of code using CFStream which works perfectly from the Watch simulator.

When I run it on the Watch I get this error:

The operation couldn’t be completed. Can't assign requested address (Code = 49)

when I try to open the connection to the server using CFStreamCreatePairWithSocketToHost.

The Apple documentation declares that CFStreamCreatePairWithSocketToHost is

Available in watchOS 2.0 and later.

so I expected that it had should work but I have found this Apple's engineer answer which confirms that there is no way to open a tcp connection from the Watch:

Socket communication does not work real Apple Watch

Can anyone clarify and help me to understand?

Supposing that I cannot open a tcp connection from the Watch, what kind of alternatives have I, since I cannot use [NSURLSession]?

I could use one of the communication methods provided by [WCSession] but do they work when the iPhone app either is not running or is not in foreground?


回答1:


You could use WCSession's sendMessage APIs to wake the iOS app up in the background and have it do the Stream work for you.



来源:https://stackoverflow.com/questions/33367222/socket-connection-from-watchos-2-and-cfstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!