判断Mac应用是否开放HTTP权限

Yourtion 创作于:2016-06-12     全文约 2018 字, 预计阅读时间为 6 分钟

之前写过关于在 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.

判断的方法类似,分享之:

获取 Mac 版本

NSDictionary *systemVersionDictionary =
    [NSDictionary dictionaryWithContentsOfFile:
        @"/System/Library/CoreServices/SystemVersion.plist"];
NSString *systemVersion =
    [systemVersionDictionary objectForKey:@"ProductVersion"];

判断 OSX 版本是否大于 10.11

if([systemVersion compare:@"10.11" options:NSNumericSearch] != NSOrderedAscending)

判断 HTTP 权限

权限判断方法跟 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 与 OSX 统一的判断方法

想判断系统版本: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;
}

原文链接:https://blog.yourtion.com/is-osx-app-enable-http.html