Mac 解决问题 June 12 2016
之前写过关于在 iOS 应用上判断是非开启 HTTP 请求权限的文章 《判断iOS应用是否开放HTTP权限》 。最近在把 HTTPDNS 的库进行 OSX 上的兼容。发现 Mac 上的 app 也会有 HTTP 权限的问题。
解决方法参考(操作是一致的): 《解决iOS9下blocked cleartext 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.
判断的方法类似,分享之:
NSDictionary *systemVersionDictionary = [NSDictionary dictionaryWithContentsOfFile: @"/System/Library/CoreServices/SystemVersion.plist"]; NSString *systemVersion = [systemVersionDictionary objectForKey:@"ProductVersion"];
if([systemVersion compare:@"10.11" options:NSNumericSearch] != NSOrderedAscending)
权限判断方法跟 iOS 的一致:
NSDictionary *systemVersionDictionary = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"]; if([[systemVersionDictionary objectForKey:@"ProductVersion"] compare:@"10.11" options:NSNumericSearch] != NSOrderedAscending){ NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary]; return [[[infoDict objectForKey:@"NSAppTransportSecurity"] objectForKey:@"NSAllowsArbitraryLoads"] boolValue]; }
想判断系统版本:iOS 大于 9.0 或者 OSX 大于 10.11,再去 info.plist
判断 Gist :
- (BOOL)needTransportSecurity { #if TARGET_OS_IPHONE if([[[UIDevice currentDevice] systemVersion] compare:@"9.0" options:NSNumericSearch] != NSOrderedAscending){ return YES; } #elif TARGET_OS_MAC NSDictionary *systemVersionDictionary = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"]; if([[systemVersionDictionary objectForKey:@"ProductVersion"] compare:@"10.11" options:NSNumericSearch] != NSOrderedAscending){ return YES; } #endif return NO; } - (BOOL)isHTTPEnable { if([self needTransportSecurity]){ NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary]; return [[[infoDict objectForKey:@"NSAppTransportSecurity"] objectForKey:@"NSAllowsArbitraryLoads"] boolValue]; } return YES; }