Giter Site home page Giter Site logo

ios8-personalvpn-demo's Introduction

iOS8的Personal VPN 测试用例

注意事项

1.工程配置中确认Capabilities中的Personal VPN开启

2.导入NetworkExtension.framework(貌似开启就自动导入了)

3.@import NetworkExtension;

创建VPN描述文件

        [[NEVPNManager sharedManager] loadFromPreferencesWithCompletionHandler:^(NSError *error){
            if(error)
            {
                NSLog(@"Load error: %@", error);
            }
            else
            {
                //配置IPSec
                [self setupIPSec];
                
                //保存VPN到系统->通用->VPN->个人VPN
                [[NEVPNManager sharedManager] saveToPreferencesWithCompletionHandler:^(NSError *error){
                    if(error)
                    {
                        ALERT(@"saveToPreferences", error.description);
                        NSLog(@"Save error: %@", error);
                    }
                    else
                    {
                        NSLog(@"Saved!");
                        ALERT(@"Saved", @"Saved");
                    }
                }];
            }
        }];

删除VPN描述文件

        [[NEVPNManager sharedManager] loadFromPreferencesWithCompletionHandler:^(NSError *error){
            if (!error)
            {
                [[NEVPNManager sharedManager] removeFromPreferencesWithCompletionHandler:^(NSError *error){
                    if(error)
                    {
                        NSLog(@"Remove error: %@", error);
                        ALERT(@"removeFromPreferences", error.description);
                    }
                    else
                    {
                        ALERT(@"removeFromPreferences", @"删除成功");
                    }
                }];
            }
        }];

连接VPN

        [[NEVPNManager sharedManager] loadFromPreferencesWithCompletionHandler:^(NSError *error){
            if (!error)
            {
                //配置IPSec
                [self setupIPSec];
                [[NEVPNManager sharedManager].connection startVPNTunnelAndReturnError:nil];
            }
        }];

断开VPN

        [[NEVPNManager sharedManager] loadFromPreferencesWithCompletionHandler:^(NSError *error){
            if (!error)
            {
                [[NEVPNManager sharedManager].connection stopVPNTunnel];
            }
        }];

配置IPSec

NEVPNProtocolIPSec *p = [[NEVPNProtocolIPSec alloc] init];
p.username = kVPNName;
p.passwordReference = [self searchKeychainCopyMatching:kPasswordReference];
p.serverAddress = kServerAddress;
p.authenticationMethod = NEVPNIKEAuthenticationMethodSharedSecret;
p.sharedSecretReference = [self searchKeychainCopyMatching:kSharedSecretReference];
p.disconnectOnSleep = NO;

//需要扩展鉴定(群组)
p.localIdentifier = kLocalIdentifier;
p.remoteIdentifier = kRemoteIdentifier;
p.useExtendedAuthentication = YES;

[[NEVPNManager sharedManager] setProtocol:p];
[[NEVPNManager sharedManager] setOnDemandEnabled:NO];
[[NEVPNManager sharedManager] setLocalizedDescription:@"个人-VPN测试"];//VPN自定义名字
[[NEVPNManager sharedManager] setEnabled:YES];

ios8-personalvpn-demo's People

Contributors

qixin1106 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.