应用程序在后台时如何处理套接字连接的事件? [英] How to handle socket connection's events when app is in background?

查看:17
本文介绍了应用程序在后台时如何处理套接字连接的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使应用在后台,我也想使用以下功能?

I want use the following function even when app is in background?

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
 {
        case NSStreamEventHasBytesAvailable:
        {  NSLog(@"Event:NSStreamEventHasBytesAvailable");
            if (theStream == _inputStream) {

                NSLog(@"NSStreamEventHasBytesAvailable: on Input Stream");
                uint8_t buffer[1024];
                int len;

                while ([_inputStream hasBytesAvailable]) {
                    len = [_inputStream read:buffer maxLength:sizeof(buffer)];
                    if (len > 0) {

                        NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];

                        if (nil != output) {

                            NSLog(@"server said: %@", output);
                             // to get local notification I am calling below method.
 [self scheduleNotification];       
                        }
                    }
                }
            }
            break;
        }

上面的代码是在前台完成的.我已经对苹果文档中给出的所有更改进行了更改,以在后台模式下运行应用程序 - voip.AppDelegate 方法应该写什么?

The above code is working done in foreGround. I have made all the change given in apple document to the run the app in the background mode- voip. What should i write in AppDelegate method?

- (void)applicationDidEnterBackground:(UIApplication *)application
{
}

如何获取stream:handleEvent在后台调用?

How to get the stream:handleEvent called in background?

推荐答案

前段时间我也在处理类似的问题.一些重要的事情要记住:

I was dealing with similiar problem a while ago. Few important things to keep in mind:

  • 后台voip"功能仅适用于设备 - 请勿使用模拟器对其进行测试
  • 如果您的应用注册为 voip 应用而不是真正的 voip 应用,您可能会(经过测试)被拒绝

因此,如果这不是 voip 应用程序,您实际上可能希望使用远程通知来直接提醒用户,而不是显示本地通知.我想这是您的应用通过 App Store 验证的唯一方法.

So if this is not a voip app you might actually want to use remote notifications to alert user directly rather than showing local notification. I guess this is the only way for your app to pass App Store validation.

无论如何,SO 上的两个链接可能对您有所帮助:

Anyway, two links here on SO helped you might find helpful:

iOS 应用程序如何在后台无限期地保持 TCP 连接有效?

我最终使用了 voip(和你一样)并按照这里的建议播放了无声的音频循环 - 它奏效了.不知道这种无声的音频循环仍然是必要的.

I ended up using voip (as you do) and playing silent audio loop as suggested here - it worked. Not sure if this silent audio loop is still neccessary.

会发生什么iOS 应用进入后台时的 TCP 和 UDP(带多播)连接

确保您阅读 开发 VoIP 应用程序的技巧技术说明 TN2277:网络和多任务处理

这篇关于应用程序在后台时如何处理套接字连接的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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