1

我想通过 PacketTunnel 在 iOS 上开发 VPN,但是当我运行此代码时,它会返回错误消息“无法完成操作。(NEVPNErrorDomain 错误 2.)”。找不到这个错误码,有谁知道这个原因吗?

这是我的 Objective-C 代码:-(void)Loadvpn{

[NETunnelProviderManager loadAllFromPreferencesWithCompletionHandler:^(NSArray<NETunnelProviderManager*>
                                                                       * _Nullable managers, NSError * _Nullable error) {
    if (error) {
        DLog("%@",error.localizedDescription)
    }else{
        if([managers count]==0){
            DLog("no conf,to add a conf")

            NETunnelProviderManager *man = [[NETunnelProviderManager alloc] init];
            NETunnelProviderProtocol *protocol = [[NETunnelProviderProtocol alloc] init];
            // bundle ID of tunnel provider
            protocol.providerBundleIdentifier = @"com.xxxx.netp.";
            protocol.providerConfiguration = @{@"key1": @"value"};
            protocol.passwordReference = [STKeyChain searchKeychainCopyMatching:@"123"];
            protocol.serverAddress = @"192.168.1.106";      // VPN server address
            man.protocolConfiguration=protocol;
            man.localizedDescription = @"linly vt";
            man.onDemandEnabled = false;



            [man saveToPreferencesWithCompletionHandler:^(NSError * _Nullable error) {

                if(error){
                    DLog("%@",error.description);
                }else{

                    [man loadFromPreferencesWithCompletionHandler:^(NSError * _Nullable error) {

                        if(error){
                            DLog("load2nd %@",error.description);
                        }
                        DLog("add success");
                        DLog("now status:%ld",man.connection.status);
                        [self StartConnect:man];
                    }];

                }
            }];

        } else{
            DLog("existed conf,%@",managers[0])
            [managers[0] loadFromPreferencesWithCompletionHandler:^(NSError * _Nullable error) {
                if(error){
                    DLog("load2nd %@",error.description);
                }
                DLog("now status:%ld",managers[0].connection.status);
                [self StartConnect:managers[0]];
            }];

        }


    }


 }];

}

-(void)StartConnect:(NETunnelProviderManager*)man{ DLog("开始连接");

NETunnelProviderSession *session = (NETunnelProviderSession*)man.connection;
NSDictionary *options = @{@"key" : @"value"};       // Send additional options to the tunnel provider
NSError *err;
[session startTunnelWithOptions:options andReturnError:&err];
DLog("%@,%@",err.localizedDescription,err.localizedFailureReason);

}

4

1 回答 1

2

您需要在启动隧道之前启用管理器。我建议您在 saveToPreferences 之前添加这样的代码。

 NETunnelProviderManager *man = [[NETunnelProviderManager alloc] init];
        NETunnelProviderProtocol *protocol = [[NETunnelProviderProtocol alloc] init];
        // bundle ID of tunnel provider
        protocol.providerBundleIdentifier = @"com.xxxx.netp.";
        protocol.providerConfiguration = @{@"key1": @"value"};
        protocol.passwordReference = [STKeyChain searchKeychainCopyMatching:@"123"];
        protocol.serverAddress = @"192.168.1.106";      // VPN server address
        man.protocolConfiguration=protocol;
        man.localizedDescription = @"linly vt";
        man.onDemandEnabled = false;

        //Noteice this line
        man.enabled = YES;


        [man saveToPreferencesWithCompletionHandler:^(NSError * _Nullable error) {
于 2019-02-20T03:13:43.147 回答