1
回答
IOS 后台VOIP的问题
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

在做一个IOS模拟测试,后台监听功能,客户端与服务器端建立连接,在服务器创建了 time,每30秒 send 数据给客户端,当手机客户端进入后台时,能够弹出本地通知消息。但是 无奈搞不清 为什么只接收一次。。。

下面是我的 

applicationDidEnterBackground 代码

UIDevice* device = [UIDevice currentDevice];
	
	BOOL backgroundSupported = NO;
	__block  UIBackgroundTaskIdentifier  _bgTask;
	if ([device respondsToSelector:@selector(isMultitaskingSupported)])
	{
		backgroundSupported = device.multitaskingSupported;
	}
	if (backgroundSupported)
	{
//		[[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{
//		}];

		UIApplication *app = [UIApplication sharedApplication];
		_bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
			}];
	dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
		    
	   if  ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground && _bgTask!=UIBackgroundTaskInvalid)
		{
	   [self  EnterBackground];
		}
		[[UIApplication sharedApplication] endBackgroundTask:_bgTask];
		_bgTask = UIBackgroundTaskInvalid;
	});
    }



-(void) EnterBackground
{
	if ( socket_receive == nil  )
	{
		return;
	}
	
	CFReadStreamRef  readStream ;
	CFWriteStreamRef writeStream ;
	CFStreamCreatePairWithSocket(NULL,CFSocketGetNative(socket_receive), &readStream, &writeStream);
	inputStream = (__bridge  NSInputStream *)readStream;
	outputStream = (__bridge NSOutputStream *)writeStream;
	
	[inputStream setProperty:NSStreamNetworkServiceTypeVoIP forKey:NSStreamNetworkServiceType];
	[outputStream setProperty:NSStreamNetworkServiceTypeVoIP forKey:NSStreamNetworkServiceType];

	
	[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
	[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
	
	[inputStream open];
	[outputStream open];
  
}



建立的UDP 通信,手机客户端以 receive 接收。。。急急急

iOS
举报
fans1991
发帖于4年前 1回/1K+阅
顶部