【问题标题】:Save config faild When I configure my iOS 8.0 VPN connection当我配置我的 iOS 8.0 VPN 连接时保存配置失败
【发布时间】:2016-07-01 19:49:27
【问题描述】:

我使用网络扩展框架来配置和管理 VPN 连接。当我运行我的代码时,它会在我完成安装配置文件之前记录错误消息:“Save config failed[(null)]”。我的代码如下:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.vpnManager = [NEVPNManager sharedManager];
[_vpnManager loadFromPreferencesWithCompletionHandler:^(NSError * _Nullable error) {
    if (error) {
        NSLog(@"Load config failed [%@]", error.localizedDescription);
        return ;
    }
    // config IPSec protocol
    NEVPNProtocolIKEv2 *p = _vpnManager.protocol;
    if (p) {

    }else{
        p = [[NEVPNProtocolIKEv2 alloc]init];
    }

    p.username = @"qlvpn";
    p.serverAddress = @"my serverAddress";

    // get password persistent reference from keychain


    p.passwordReference = [self searchKeychainCopyMatching:@"kd2014@"];
    // If password doesn't exist in keychain, should create it beforehand.
    if (!p.passwordReference) {
        [self createKeychainValue:@"kd2014@" forIdentifier:@"kd2014@"];
         p.passwordReference = [self searchKeychainCopyMatching:@"kd2014@"];
    }
    p.authenticationMethod = NEVPNIKEAuthenticationMethodSharedSecret;
    p.sharedSecretReference = [self searchKeychainCopyMatching:@"PSK"];
    if (!p.sharedSecretReference) {
        [self createKeychainValue:@"qlvpn_kd2014@" forIdentifier:@"PSK"];
        p.sharedSecretReference = [self searchKeychainCopyMatching:@"PSK"];
    }

    p.localIdentifier = @"qlvpn.client";
    p.remoteIdentifier = @"qlvpn.server";

    p.useExtendedAuthentication = YES;
    p.disconnectOnSleep = NO;
    _vpnManager.protocol = p;
    _vpnManager.localizedDescription = @"IKEv2 Demo";

    [_vpnManager saveToPreferencesWithCompletionHandler:^(NSError * _Nullable error) {
        NSLog(@"Save config faild[%@]",error.localizedDescription);
    }];

}];

}

keyChain 方法如下:

    static NSString * const serviceName = @"qlvpn.vpn_config";

- (NSMutableDictionary *)newSearchDictionary:(NSString *)identifier {
    NSMutableDictionary *searchDictionary = [[NSMutableDictionary alloc] init];

    [searchDictionary setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass];

    NSData *encodedIdentifier = [identifier dataUsingEncoding:NSUTF8StringEncoding];
    [searchDictionary setObject:encodedIdentifier forKey:(__bridge id)kSecAttrGeneric];
    [searchDictionary setObject:encodedIdentifier forKey:(__bridge id)kSecAttrAccount];
    [searchDictionary setObject:serviceName forKey:(__bridge id)kSecAttrService];

    return searchDictionary;
}

- (NSData *)searchKeychainCopyMatching:(NSString *)identifier {
    NSMutableDictionary *searchDictionary = [self newSearchDictionary:identifier];

    // Add search attributes
    [searchDictionary setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];

    // Add search return types
    // Must be persistent ref !!!!
    [searchDictionary setObject:@YES forKey:(__bridge id)kSecReturnPersistentRef];

    CFTypeRef result = NULL;
    SecItemCopyMatching((__bridge CFDictionaryRef)searchDictionary, &result);

    return (__bridge_transfer NSData *)result;
}

- (BOOL)createKeychainValue:(NSString *)password forIdentifier:(NSString *)identifier {
    NSMutableDictionary *dictionary = [self newSearchDictionary:identifier];

    OSStatus status = SecItemDelete((__bridge CFDictionaryRef)dictionary);

    NSData *passwordData = [password dataUsingEncoding:NSUTF8StringEncoding];
    [dictionary setObject:passwordData forKey:(__bridge id)kSecValueData];

    status = SecItemAdd((__bridge CFDictionaryRef)dictionary, NULL);

    if (status == errSecSuccess) {
        return YES;
    }
    return NO;
}

所以,我真的不知道为什么我无法保存配置..

【问题讨论】:

    标签: objective-c ios8 vpn


    【解决方案1】:

    我无法保存它,除非我同时调用两者

    loadAllFromPreferencesWithCompletionHandler
    loadFromPreferencesWithCompletionHandler
    

    只有这样

    saveToPreferencesWithCompletionHandler
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-30
      • 1970-01-01
      • 1970-01-01
      • 2020-05-19
      • 2022-09-30
      • 1970-01-01
      • 1970-01-01
      • 2016-11-01
      相关资源
      最近更新 更多