如何处理套接字连接的事件,当应用程序在后台? [英] How to handle socket connection's events when app is in background?
问题描述
我想用下面的函数,即使应用程序是在后台?
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;
}
以上code在前台工作的完成。我做了苹果文件中给出的在后台运行模式 - 网络电话应用程序的所有变化。
我应该在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
{
}
如何获得流:为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应用程序。我想这是你的应用程序通过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:
<一个href=\"http://stackoverflow.com/questions/5840365/how-can-an-ios-app-keep-a-tcp-connection-alive-indefinitely-while-in-the-backgro\">How可以在iOS应用程序保持一个TCP连接无限期地活着,而在后台?
我结束了使用VoIP(你做),并扮演建议在这里无声音频循环 - 它的工作。不知道
这沉默的音频回路仍然neccessary。
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.
<一个href=\"http://stackoverflow.com/questions/9047147/what-happens-to-tcp-and-udp-with-multicast-connection-when-an-ios-application\">What恰好TCP和UDP(组播)连接时,iOS应用程序确实进入后台
请务必阅读<一个href=\"http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/AdvancedAppTricks/AdvancedAppTricks.html#//apple_ref/doc/uid/TP40007072-CH7-SW18\"相对=nofollow>为开发一个VoIP的应用提示和的技术说明TN2277:网络和多任务
这篇关于如何处理套接字连接的事件,当应用程序在后台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!