gpt4 book ai didi

iOS NETunnelProviderManager 保存多个配置

转载 作者:可可西里 更新时间:2023-11-01 05:18:27 35 4
gpt4 key购买 nike

我正在尝试将我的 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]];
}

}
}];
}
}];

最佳答案

使用 + (void)loadAllFromPreferencesWithCompletionHandler:(void (^)(NSArray<NEAppProxyProviderManager *> *managers, NSError *error))completionHandler API。

仅当 block 中的 managers.count == 0 时才创建新协议(protocol)。

[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...
}];

关于iOS NETunnelProviderManager 保存多个配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42102835/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com