Method to determine whether an iOS application is open to HTTP permissions

  • 2020-05-24 06:13:12
  • OfStack

Starting with iOS9, the new feature requires App to access network requests using the HTTPS protocol. But can you tell if the developer will allow HTTP's request, so that the request does not fail and the following message pops up:

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.

This requirement is actually something related to HTTPDNS, which can only be requested through the HTTP interface, but I hope to be able to judge whether the application allows the access of HTTP, and only turn on the functions related to HTTPDNS if it is allowed.

The easy solution is to read info.plist and see if NSAppTransportSecurity is YES.

Objective - C implementation


- (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;
}

Usage:


if ([self isHTTPEnable]) {
 NSLog(@"HTTP enable");
} else {
 NSLog(@"HTTP disable");
}

Swift implementation


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
}

Usage:


if self.isHTTPEnable() {
 print("HTTP enable")
} else {
 print("HTTP disable")
}

The original link: http: / / blog yourtion. com/is - ios - app - enable - http. html


Related articles: