hi eskimo:
I have encountered -loadAllFromPreferencesWithCompletionHandler failed to load the already configured VPN, the following is the method I use:
create:
- (NETunnelProviderManager *)createProviderManager {
NETunnelProviderManager *manager = [[NETunnelProviderManager alloc] init];
NETunnelProviderProtocol *conf = [[NETunnelProviderProtocol alloc] init];
conf.serverAddress = @"123.321.123.123";
manager.protocolConfiguration = conf;
manager.localizedDescription = @"xVPN";
return manager;
}
save:
- (void)saveConfigWithManger:(NETunnelProviderManager *)manager parameters:(NSDictionary *)parameters Complete:(void(^)(void))complete {
NSString *ip = parameters[@"ip"];//ip
NSString *port = parameters[@"port"];//port
if (!ip || !port) {
return;
}
NSArray<Router *> *routers = parameters[@"routers"];
BOOL isOverseas = [parameters[@"isOverseas"] boolValue];
NSString *configurationFileURL = [[NSBundle mainBundle] pathForResource:@"client" ofType:@"config"];
NSString *config = [NSString stringWithContentsOfFile:configurationFileURL encoding:NSUTF8StringEncoding error:nil];
config = [config stringByReplacingOccurrencesOfString:@"addrIP" withString:ip];
config = [config stringByReplacingOccurrencesOfString:@"portID" withString:port];
NETunnelProviderProtocol *tunnelProtocol = (NETunnelProviderProtocol *)manager.protocolConfiguration;
NSMutableDictionary *providerConfiguration = [NSMutableDictionary dictionary];
providerConfiguration[@"config"] = [config dataUsingEncoding:NSUTF8StringEncoding];
providerConfiguration[@"authUser"] = [[NSUserDefaults standardUserDefaults] stringForKey:@"kAuthorization"];
tunnelProtocol.serverAddress = @"123.321.123.123";
providerConfiguration[@"routers"] = routes;
providerConfiguration[@"nodeIP"] = ip;
providerConfiguration[@"isOverseas"] = isOverseas ? @"1" : @"0";//
tunnelProtocol.providerConfiguration = providerConfiguration;
manager.protocolConfiguration = tunnelProtocol;
// __weak typeof(self) weakSelf = self;
[manager saveToPreferencesWithCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(error);
}
}];
}
load:
[NETunnelProviderManager loadAllFromPreferencesWithCompletionHandler:^(NSArray<NETunnelProviderManager *> * _Nullable managers, NSError * _Nullable error) {
NETunnelProviderManager *manager = nil;
if (managers.count > 0) {
manager = managers.firstObject;
NSLog(@"manager loaded:%@", manager);
} else {
manager = [self createProviderManager];
NSLog(@"create new manager:%@", manager);
}
manager.enabled = YES;
complete(manager);
}];
My use scenario is to kill the APP process after connecting to the VPN, and then restart the APP. In most cases, it can be loaded into the manage(log in consol manager loaded:). Occasionally, the load cannot be loaded(log in consol: create new manager:). As a result, I cannot obtain the VPN status after starting the APP.
How can I solve this problem?