iOS NETunnelProviderManager 保存多个配置

iOS NETunnelProviderManager saving multiple configurations

我正在尝试将我的 VPN 配置保存到偏好设置,这已经可以使用了(我能够连接到我的 VPN)。但出于某种原因,每次我 运行 代码再次创建一个新配置而不是使用最后一个配置。所以,我最终得到了一堆配置。

这是我当前的代码,如果有人能告诉我它出了什么问题,那就太棒了。谢谢!

// Initialize Manager
NETunnelProviderManager *manager = [[NETunnelProviderManager alloc] init];

[manager loadFromPreferencesWithCompletionHandler:^(NSError *error) {
    if (error) {
        NSLog(@"Load Error: %@", error.description);
    } else {

        // Create the protocol object
        NETunnelProviderProtocol *protocol = [[NETunnelProviderProtocol alloc] init]; // Create the protocol object

        // Configure the protocol object
        protocol.providerBundleIdentifier = @"com.nfisc.testvpn.ptp";                 // Bundle ID of tunnel provider
        protocol.providerConfiguration = @{};                                         // Currently blank, but will be used later
        protocol.serverAddress = @"0.0.0.0";                                          // Ommited for security reasons
        protocol.username = @"username";                                              // The username for the configuration
        protocol.identityDataPassword = @"password";                                  // The password for the configuration
        protocol.disconnectOnSleep = NO;

        // Configure the manager with the protocol
        manager.protocolConfiguration = protocol;
        manager.enabled = true;

        [manager saveToPreferencesWithCompletionHandler:^(NSError *error) {
            if (error) {
                NSLog(@"Save Error: %@", error.description);
            } else {
                if ([[manager connection] status] != NEVPNStatusConnected) {
                    NSLog(@"Starting VPN");
                    [self start:manager];
                } else {
                    NSLog(@"VPN Already Connected");
                    [_statusLabel setText:@"Connected"];
                    [_statusLabel setTextColor:[UIColor greenColor]];
                }

            }
        }];
    }
}];

解决方案是先调用 [NETunnelProviderManager loadAllFromPreferencesWithCompletionHandler:,然后再进行其他操作。

固定代码:

__block NETunnelProviderManager *manager = [[NETunnelProviderManager alloc] init];

NETunnelProviderProtocol *protocol = [[NETunnelProviderProtocol alloc] init];
protocol.providerBundleIdentifier = @"com.nfisc.testvpn.ptp";   // bundle ID of tunnel provider
protocol.providerConfiguration = @{@"key": @"value"};
protocol.serverAddress = @"0.0.0.0";        // VPN server address
protocol.username = @"username";
protocol.identityDataPassword = @"password";
manager.protocolConfiguration = protocol;
manager.enabled = true;

[NETunnelProviderManager loadAllFromPreferencesWithCompletionHandler:^(NSArray<NETunnelProviderManager *> * _Nullable managers, NSError * _Nullable error) {
    if ([managers count] > 0) {
        manager = [managers objectAtIndex:0];
        [self start:manager];
    } else {
        [manager saveToPreferencesWithCompletionHandler:^(NSError *error) {
            if (error) {
                NSLog(@"Error 1: %@", error.description);
            } else {
                [manager loadFromPreferencesWithCompletionHandler:^(NSError * _Nullable error) {
                    if (error) {
                        NSLog(@"Error 2: %@", error.description);
                    } else {
                        [self start:manager];
                    }
                }];
            }
        }];
    }
}

改用+ (void)loadAllFromPreferencesWithCompletionHandler:(void (^)(NSArray<NEAppProxyProviderManager *> *managers, NSError *error))completionHandlerAPI。

仅当块中 managers.count == 0 时才创建新协议。

[NETunnelProviderManager loadAllFromPreferencesWithCompletionHandler:^(NSArray<NETunnelProviderManager *> * _Nullable managers, NSError * _Nullable error) {
    if (error) {
        NSLog(@"Load Error: %@", error.description);
    }

    NETunnelProviderManager *manager;
    if (managers.count > 0) {
        manager = managers[0];
    }else {
        manager = [[NETunnelProviderManager alloc] init];
        manager.protocolConfiguration = [[NETunnelProviderProtocol alloc] init];
    }

//... your code here...
}];