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

查看:19
本文介绍了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.

所以我希望它应该可以工作,但我找到了这个 Apple 工程师的回答,它确认无法从 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:

Socket 通信不适用于真正的 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?

推荐答案

你可以使用 WCSession 的 sendMessage API 在后台唤醒 iOS 应用程序并让它执行流工作你.

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天全站免登陆