在非活动状态下保持 iPhone 网络连接处于活动状态?

发布于 2024-12-11 10:59:07 字数 962 浏览 0 评论 0原文

我正在开发一个连接到聊天服务器的聊天客户端应用程序。当我收到任何弹出通知(例如短信)时,iPhone 会将应用程序置于非活动状态 - 太棒了。我遇到的问题是,一旦通知被取消,应用程序就会丢失所有网络连接。我读过其他地方说这不应该发生,但我不确定我错过了什么!

这是我用来创建读取和写入流的代码片段。

- (void) initNetworkCommunication {

//set up read and write streams
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;

//connect to host at this address and this port
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"somewebsite.org", 3333, &readStream, &writeStream);

//open socket connection
inputStream = [(NSInputStream *)readStream retain];
outputStream = [(NSOutputStream *)writeStream retain];
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
}

当应用程序发送到后台时,网络连接也会丢失。我也想解决这个问题,但现在的主要问题是弹出通知断开用户连接。

I am working on a chat client app that connects to a chat server. When I receive any pop-up notifications (for example, a SMS message), the iPhone puts the app into the inactive state - great. The problem I have encountered is that once the notification is dismissed, the app has lost all network connections. I've read other places that this shouldn't happen, but I'm not sure what I'm missing!

Here is a snippet of the code I am using to create the read and write streams.

- (void) initNetworkCommunication {

//set up read and write streams
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;

//connect to host at this address and this port
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"somewebsite.org", 3333, &readStream, &writeStream);

//open socket connection
inputStream = [(NSInputStream *)readStream retain];
outputStream = [(NSOutputStream *)writeStream retain];
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
}

Network connection is also lost when the app is sent to the background. I would like to fix that too, but the main issue right now is the pop-up notifications disconnecting the user.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三岁铭 2024-12-18 10:59:07

您在应用程序的“applicationWillResignActive:”方法中做什么?您可以考虑暂停连接,然后在应用程序再次处于活动状态后恢复连接。无论哪种方式,你都应该用这种方法做一些事情。

希望有帮助。

What are you doing in your application's "applicationWillResignActive:" method? You might consider suspending the connection, then resuming on it once the app is active again. Either way you should be doing something in that method.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文