iOS14新增加本地網(wǎng)絡權(quán)限Privacy - Local Network Usage Description
如有本地網(wǎng)絡使用場景需要在info.plist
中增加 Bonjour services
字段(如投屏加入_leboremote._tcp
)
查看使用本地網(wǎng)絡的三方庫方法:在項目目錄下使用 grep -r SimplePing .
命令即可
Apple官方無具體API查詢Local Network權(quán)限,這里采用建立定時器對本地網(wǎng)絡請求,如果請求不通則無Local Network權(quán)限。需要使用Ping庫(https://github.com/yluo8090/Common/tree/main/Ping)具體見下:
#import "SimplePing.h"
#import "LDSRouterInfo.h"
@interface LocalNetManager ()<SimplePingDelegate>
{
dispatch_source_t _timer;
}
@property (nonatomic, strong) SimplePing *pinger;
@end
- (void)stop{
if (_pinger) {
[_pinger stop];
}
if (_timer) {
dispatch_source_cancel(_timer);
_timer = nil;
}
}
- (void)checkLocalNetStatus{
NSDictionary *router = [LDSRouterInfo getRouterInfo];
_pinger = [[SimplePing alloc] initWithHostName:router[@"ip"]];
_pinger.delegate = self;
[_pinger start];
}
- (void)simplePing:(SimplePing *)pinger didStartWithAddress:(NSData *)address{
if (_timer) {
return;
}
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
dispatch_source_set_timer(_timer, DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
dispatch_source_set_event_handler(_timer, ^{
[pinger sendPingWithData:nil];
});
dispatch_resume(_timer);
}
- (void)simplePing:(SimplePing *)pinger didSendPacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber{
FF_DLog(@"**可以使用局域網(wǎng)**");
[self stop];
}
- (void)simplePing:(SimplePing *)pinger didFailToSendPacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber error:(NSError *)error{
if (error.code == 65) {
[self stop];
FF_DLog(@"**不可以使用局域網(wǎng)**");
if (APPDELEAGTE.alertLocalNetView) {
//申請權(quán)限不提示
return;
}
}
}