NEVPNErrorDomain错误2

时间:2017-10-07 14:26:21

标签: vpn packet tunnel networkextension

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

这是我的Objective-c代码: - (无效)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]];
            }];

        }


    }


 }];

}

- (无效)StartConnect:(NETunnelProviderManager *)手册{     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);

}

1 个答案:

答案 0 :(得分:0)

您需要在启动隧道之前启用管理器。我建议您在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) {