iOS 8 VPN - 网络扩展 - __connection_block_invoke_2中的错误

时间:2015-08-04 06:37:49

标签: objective-c ios8 vpn

我使用以下代码配置VPN

- (void)setupConfiguration
{
    NEVPNManager *manager = [NEVPNManager sharedManager];
    int status = manager.connection.status;

    if (status == NEVPNStatusConnected) {
        [manager.connection stopVPNTunnel];
    }
    else
    {
        [manager loadFromPreferencesWithCompletionHandler:^(NSError *error)
        {
            if (error) {
                NSLog(@"Load config failed [%@]", error.localizedDescription);
                return;
            }

            NEVPNProtocolIPSec *p = (NEVPNProtocolIPSec *)manager.protocol;
            if (!p) {
                p = [[NEVPNProtocolIPSec alloc] init];
            }

            NSString *username = [Username];
            NSString *url = @"Server URL";
            p.username = username;
            p.serverAddress = url;

            p.passwordReference = [Password from Keychain];
            p.authenticationMethod = NEVPNIKEAuthenticationMethodSharedSecret;
            p.sharedSecretReference = [Shared secret from Keychain];
            //p.localIdentifier = @"";
            //p.remoteIdentifier = @"";

            p.disconnectOnSleep = NO;
            p.useExtendedAuthentication = YES;

            [manager setProtocol:p];
            [manager setOnDemandEnabled:NO];
            [manager setLocalizedDescription:@"VIT VPN"];
            NSLog(@"Connection desciption: %@", manager.localizedDescription);
            NSLog(@"VPN status:  %li", (long)manager.connection.status);

            [manager saveToPreferencesWithCompletionHandler:^(NSError *error) {
                if(error) {
                    NSLog(@"Save error: %@", error);
                } else {
                    NSLog(@"Saved!");
                }
            }];
        }];
    }
}

我在viewDidLoad上调用此方法。
当用户在完成配置文件安装后从设备VPN设置导航到我们的应用程序时,我在日志中收到此错误 - "error in __connection_block_invoke_2: Connection interrupted"。之后,当我尝试连接到VPN服务器时,没有任何反应。
任何人都可以帮我解决这个问题,为什么我会收到这个错误?

注意: - 我已经遵循了这些教程:
1. http://ramezanpour.net/post/2014/08/03/configure-and-manage-vpn-connections-programmatically-in-ios-8/
2。 https://disqus.com/home/discussion/ramezanpour/configure_and_manage_vpn_connections_programmatically_in_ios_8/newest/

1 个答案:

答案 0 :(得分:0)

试试这个

struct Data {};

struct S {
    v1: Option<Data>,
    v2: Option<Data>
}

...

fn update_v1(diffs: &mut HashMap<u64, Data>, key: u64, data: Data) {
    match diffs.entry(key) {
        Entry::Vacant(v) => {
            let variant = S {
                v1: Some(data),
                v2: None
            };
            v.insert(variant);
        },
        Entry::Occupied(e) => {
            let new_variant = Some(data);
            if e.get().v2 == new_variant {
                e.remove();
            } else {
                let existing = e.into_mut();
                existing.v1 = new_variant;
            }
        }
    }
}

fn update_v2(diffs: &mut HashMap<u64, Data>, key: u64, data: Data) {
    match diffs.entry(key) {
        Entry::Vacant(v) => {
            let variant = S {
                v2: Some(data),
                v1: None
            };
            v.insert(variant);
        },
        Entry::Occupied(e) => {
            let new_variant = Some(data);
            if e.get().v1 == new_variant {
                e.remove();
            } else {
                let existing = e.into_mut();
                existing.v2 = new_variant;
            }
        }
    }
}