2019独角兽企业重金招聘Python工程师标准>>>
其实很简单,就是在锁屏后AsyncUdpSocket会失效,没有办法继续receiveData。将原来的关闭,重新开启一个新的就好了。
具体的代码分两部分完成,首先是在AppDelegate里注明方法
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[Discover_UDP sharedDiscover_UDP]stopRun];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[[Discover_UDP sharedDiscover_UDP]startReceive];
}
看得出我的DisCover_UDP是自己封装的一个控制AsyncUdpSocket的单例方法,里面的stop和startReceive是这样的。
-(void)stopRun
{
[_timer invalidate];
_timer = nil;
[_sendSocket close];
_sendSocket = nil;
[_receiveSocket close];
_receiveSocket = nil;
}
-(void)startReceive
{
if (_sendSocket == nil) {
//发送端
_sendSocket = [[AsyncUdpSocket alloc]initWithDelegate:self];
[_sendSocket bindToPort:5678 error:nil];
}
if (_receiveSocket == nil) {
//接收端
_receiveSocket = [[AsyncUdpSocket alloc]initWithDelegate:self];
[_receiveSocket bindToPort:43708 error:nil];
}
if (_receiveSocket.isClosed) {
[_receiveSocket bindToPort:43708 error:nil];
}
[_receiveSocket receiveWithTimeout:-1 tag:0];
}
这里只贴出了关于锁屏失效的处理部分,其余的逻辑和问题,各位大牛自行解决咯。