Skip to content

Commit

Permalink
[描述]:适配iOS14本地网络权限
Browse files Browse the repository at this point in the history
  • Loading branch information
cocoa-chen committed Sep 18, 2020
1 parent 7a6dc48 commit bd6eab4
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
2 changes: 1 addition & 1 deletion EchoSDK.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'EchoSDK'
s.version = '0.0.4'
s.version = '0.0.5'
s.summary = 'Echo调试工具SDK.'

# This description is used to generate tags and improve search results.
Expand Down
Binary file added Images/ios14permission.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,22 @@
pod 'EchoSDK', :configurations => ["Debug"]
```

2、在App启动时添加以下代码
2、由于iOS14系统本地网络权限限制,需在工程的Info.plist文件中添加NSLocalNetworkUsageDescription和NSBonjourServices配置。在Xcode中选中Info.plist文件,右键选择Open As Source Code,并添加如下内容:

```
<key>NSLocalNetworkUsageDescription</key>
<string></string>
<key>NSBonjourServices</key>
<array>
<string>_ECHO._tcp</string>
</array>
```

在Xcode中显示效果如下图:

![img](https://github.com/didi/echo/raw/master/Images/ios14permission.jpg)

3、在App启动时添加以下代码

```
#ifdef DEBUG
Expand All @@ -117,10 +132,29 @@ pod 'EchoSDK', :configurations => ["Debug"]
}
```

3、启动Echo的Mac端
4、启动Echo的Mac端

手动build工程的话,可以在/Mac目录下执行`pod install`之后,启动`Echo.xcworkspace`并运行。

## iOS14 适配

iOS14系统对本地网络权限进行了更严格的限制,鉴于Echo底层用到了Bonjour服务,在Xcode12之后需要在工程的Info.plist文件中添加NSLocalNetworkUsageDescription和NSBonjourServices配置。在Xcode中选中Info.plist文件,右键选择Open As Source Code,并添加如下内容:

```
<key>NSLocalNetworkUsageDescription</key>
<string></string>
<key>NSBonjourServices</key>
<array>
<string>_ECHO._tcp</string>
</array>
```

添加之后在Xcode中显示效果如下图:

![img](https://github.com/didi/echo/raw/master/Images/ios14permission.jpg)

> 注:即使不进行上述适配,Echo的自动连接功能仍会生效:你可以通过USB连接真机或者直接在同一台电脑上运行模拟器来实现自动连接。
## 感谢

在开发过程中,`Echo`使用和参考了以下优秀项目的部分代码
Expand Down
19 changes: 19 additions & 0 deletions Sources/Core/Channel/Bonjour/ECONetServiceBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ - (void)netServiceBrowser:(NSNetServiceBrowser *)browser didRemoveService:(NSNet
- (void)netServiceBrowser:(NSNetServiceBrowser *)browser didNotSearch:(NSDictionary<NSString *, NSNumber *> *)errorDict {
NSLog(@"%s",__func__);
//重试
if (@available(iOS 14.0, *)) {
NSNetServicesError errorCode = [errorDict[@"NSNetServicesErrorCode"] integerValue];
if (errorCode == NSNetServicesMissingRequiredConfigurationError) {
//iOS14新增本地网络隐私权限,提示用户如何设置并忽略
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSString *title = @"Echo 连接提示";
NSString *message = @"由于iOS14本地网络权限限制,请在Info.plist中设置NSLocalNetworkUsageDescription和NSBonjourServices,详细内容见:https://github.com/didi/echo";
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];
[alertController addAction:confirmAction];
UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootVC presentViewController:alertController animated:YES completion:nil];
});
NSLog(@">>Echo Warning:Bonjour服务错误,由于iOS14本地网络权限限制,请在Info.plist中设置NSLocalNetworkUsageDescription和NSBonjourServices,详细内容见:https://github.com/didi/echo");
return;
}
}
[self resetBrowserService];
}
#pragma mark - NSNetServiceDelegate methods
Expand Down
6 changes: 6 additions & 0 deletions iOS/Example/EchoDemo/EchoDemo-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
<string>1.0</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSBonjourServices</key>
<array>
<string>_ECHO._tcp</string>
</array>
<key>NSLocalNetworkUsageDescription</key>
<string></string>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
Expand Down

0 comments on commit bd6eab4

Please sign in to comment.