国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

判斷iOS應用是否開放HTTP權限的方法

2020-07-26 03:24:45
字體:
來源:轉載
供稿:網友

從 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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 塘沽区| 建平县| 淅川县| 辉南县| 沂南县| 汪清县| 康保县| 大竹县| 洛隆县| 柳河县| 林甸县| 清新县| 玛曲县| 阳泉市| 永城市| 荥阳市| 烟台市| 桂东县| 横峰县| 通化县| 色达县| 东阳市| 克拉玛依市| 淮北市| 乌兰察布市| 乌什县| 雅江县| 宿松县| 东方市| 抚顺市| 沅陵县| 贵定县| 方山县| 安阳县| 东方市| 兴化市| 循化| 怀来县| 伊宁县| 上林县| 揭西县|