来自WatchOS 2和CFStream的套接字连接 [英] Socket connection from WatchOS 2 and CFStream

查看:112
本文介绍了来自WatchOS 2和CFStream的套接字连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须通过Watch的tcp套接字连接远程服务器.

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

我已经使用CFStream编写了一段代码,该代码可以在Watch模拟器中完美运行.

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

当我在Watch上运行它时,出现此错误:

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

操作无法完成.无法分配请求的地址(代码= 49)

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

当我尝试使用CFStreamCreatePairWithSocketToHost打开与服务器的连接时.

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

Apple文档声明CFStreamCreatePairWithSocketToHost

The Apple documentation declares that CFStreamCreatePairWithSocketToHost is

在watchOS 2.0和更高版本中可用.

Available in watchOS 2.0 and later.

所以我希望它能正常工作,但是我发现了苹果公司的工程师答案,该答案证实无法从Watch中打开tcp连接:

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:

套接字通信在真正的Apple Watch中无法正常工作

任何人都可以澄清并帮助我理解吗?

Can anyone clarify and help me to understand?

假设我无法通过Watch打开TCP连接,由于我无法使用[NSURLSession],我有什么选择?

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

我可以使用[WCSession]提供的一种通信方法,但是当iPhone应用程序未运行或不在前台时,它们是否可以工作?

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?

推荐答案

您可以使用

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.

这篇关于来自WatchOS 2和CFStream的套接字连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆