欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

判斷iOS應(yīng)用是否開放HTTP權(quán)限的方法

 更新時(shí)間:2016年03月24日 15:58:43   作者:郭宇翔  
這篇文章主要為大家詳細(xì)介紹了判斷iOS應(yīng)用是否開放HTTP權(quán)限的方法,感興趣的小伙伴們可以參考一下

從 iOS9 起,新特性要求 App 訪問網(wǎng)絡(luò)請求,要采用 HTTPS 協(xié)議。但是能不能判斷開發(fā)者是否允許 HTTP 的請求,這樣就不會在發(fā)起請求時(shí)候失敗同時(shí)彈出以下信息:

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.

這個(gè)需求其實(shí)是最近在弄 HTTPDNS 相關(guān)的一些東西,只能通過 HTTP 接口請求,但是希望能判斷應(yīng)用是否允許了 HTTP 的訪問,如果允許才開啟 HTTPDNS 相關(guān)的功能。

解決方法比較簡單,其實(shí)就是讀取 info.plist 看看 NSAppTransportSecurity 是否為 YES。

Objective-C 實(shí)現(xiàn)

- (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 實(shí)現(xiàn)

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

相關(guān)文章

最新評論