4

I'm trying to save my VPN configuration to the preferences, which already works (I'm able to connect to my VPN). But for some reason each time i run the code again instead of using the last configuration it creates a new one. So, i end up with a bunch of configurations.

Here is my current code, if anyone could let me know what's going wrong with it that would be awesome. Thanks!

// 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]];
                }

            }
        }];
    }
}];
4

2 回答 2

6

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

仅当块中的 manager.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...
}];
于 2017-02-14T02:55:37.020 回答
1

解决方案是先打电话[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];
                    }
                }];
            }
        }];
    }
}
于 2017-02-14T02:54:40.760 回答