无法保存NETunnelProviderManager的配置

时间:2017-01-31 12:14:24

标签: ios ios9 ios-app-extension tunnel networkextension

我收到错误:

Error Domain=NEVPNErrorDomain Code=5 "permission denied"

当我尝试在NETunnelProviderManager上保存配置时。这是我的代码:

NETunnelProviderManager * man = [[NETunnelProviderManager alloc] init];
NETunnelProviderProtocol *protocol = [[NETunnelProviderProtocol alloc] init];

[protocol setServerAddress:@"aaa.bbb.ccc.ddd"]; // not actual value
[protocol setUsername:@"testuser"];

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];

NSData *data = [[NSData alloc] init];
[dictionary setObject:@"UUID" forKey:(id)kSecAttrService];
[dictionary setObject:data forKey:(id)kSecValueData];
[dictionary setObject:(__bridge id)kSecAttrAccessibleAlways forKey:(id)kSecAttrAccessible];
[dictionary setObject:(__bridge id)kSecClassGenericPassword forKey:(id)kSecClass];
[dictionary setObject:(__bridge id)kCFBooleanTrue forKey:(id)kSecReturnPersistentRef];

CFTypeRef passwordRef = nil;

OSStatus delStatus = SecItemDelete((__bridge CFDictionaryRef)dictionary);
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)dictionary, &passwordRef);

[protocol setPasswordReference:(__bridge NSData * _Nullable)(passwordRef)];

man.protocolConfiguration = protocol;
man.localizedDescription = @"My VPN";
man.onDemandEnabled = NO;
man.enabled = YES;

[man saveToPreferencesWithCompletionHandler:^(NSError *err) {
    NSLog(@"saved preferences: error = %@", err);
}];

我在功能中启用了个人VPN,并在应用和网络扩展中都有.entitlements文件。

2 个答案:

答案 0 :(得分:2)

听起来您的应用无法(无权限)阅读或编写网络扩展偏好设置。

在应用的ID上检查developer.apple.com,它使用的是Network Extensions和Personal VPN。在您的Xcode项目目标中,不要使用自动配置文件处理。而是在Provisioning Profiles部分手动创建它们。希望这有帮助!

答案 1 :(得分:0)

转到 Xcode - >项目 - >目标 - >能力和 启用VPN并启用网络扩展。enter image description here