手机
当前位置:查字典教程网 >编程开发 >IOS开发 >判断iOS应用是否开放HTTP权限的方法
判断iOS应用是否开放HTTP权限的方法
摘要:从iOS9起,新特性要求App访问网络请求,要采用HTTPS协议。但是能不能判断开发者是否允许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权限的方法】相关文章:

iOS Webview自适应实际内容高度的4种方法详解

IOS 应用内b显示 AppStore 某个应用的详情

iOS9开放的新API--Spotlight使用指南

iOS应用开发中实现页面跳转的简单方法笔记

实例解析iOS应用多线程开发中NSthread类的用法

iOS应用开发中矢量图的使用及修改矢量图颜色的方法

Unity3d发布IOS9应用时出现中文乱码的解决方法

iOS App开发中修改UILabel默认字体的方法

IOS应用内跳转系统设置相关界面的方法

IOS 应用之间的跳转和数据传递详解

精品推荐
分类导航