判断iOS应用是否开放HTTP权限的方法
2019/7/9 23:20:43
本文主要是介绍判断iOS应用是否开放HTTP权限的方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
从 iOS9 起,新特性要求 App 访问网络请求,要采用 HTTPS 协议。但是能不能判断开发者是否允许 HTTP 的请求,这样就不会在发起请求时候失败同时弹出以下信息:
App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
这个需求其实是最近在弄 HTTPDNS 相关的一些东西,只能通过 HTTP 接口请求,但是希望能判断应用是否允许了 HTTP 的访问,如果允许才开启 HTTPDNS 相关的功能。
解决方法比较简单,其实就是读取 info.plist 看看 NSAppTransportSecurity 是否为 YES。
Objective-C 实现
- (BOOL)isHTTPEnable { if([[[UIDevice currentDevice] systemVersion] compare:@"9.0" options:NSNumericSearch] != NSOrderedAscending){ NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary]; return [[[infoDict objectForKey:@"NSAppTransportSecurity"] objectForKey:@"NSAllowsArbitraryLoads"] boolValue]; } return YES; }
使用方法:
if ([self isHTTPEnable]) { NSLog(@"HTTP enable"); } else { NSLog(@"HTTP disable"); }
Swift 实现
func isHTTPEnable() -> Bool { let flag = UIDevice.currentDevice().systemVersion.compare("9.0.0", options: NSStringCompareOptions.NumericSearch) if (flag != .OrderedAscending) { guard let infoDict = NSBundle.mainBundle().infoDictionary else { return false } guard let appTransportSecurity = infoDict["NSAppTransportSecurity"] else { return false } guard let allowsArbitraryLoads = appTransportSecurity["NSAllowsArbitraryLoads"] else { return false } guard let res = allowsArbitraryLoads else { return false } return res as! Bool } return true }
使用方法:
if self.isHTTPEnable() { print("HTTP enable") } else { print("HTTP disable") }
原文链接:http://blog.yourtion.com/is-ios-app-enable-http.html
这篇关于判断iOS应用是否开放HTTP权限的方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-14system bios shadowed
- 2024-03-14gabios
- 2024-02-07iOS应用提交上架的最新流程
- 2024-02-06打包 iOS 的 IPA 文件
- 2023-12-07uniapp打包iOS应用并通过审核:代码混淆的终极解决方案 ?
- 2023-11-25uniapp IOS从打包到上架流程(详细简单) 原创
- 2023-11-10【iOS开发】iOS App的加固保护原理:使用ipaguard混淆加固
- 2023-09-30最强大的iOS应用源码保护工具:Ipa Guard,保护你的商业机密代码
- 2023-09-07iOS安全加固探讨:代码混淆、类名方法名混淆等方法
- 2023-09-05iOS代码加固与保护方法详解 - 提升iOS应用安全性的关键步骤