iOS项目添加VPN

来源:互联网 发布:淘宝微信内部优惠卷群 编辑:程序博客网 时间:2024/06/01 07:11

1、首先到appledeveloper对AppId进行设置开启PersonalVPN 

2、由于现在升级到了Xcode8+,所以要在项目设置->Capabilities->打开PersonalVPN如图所示:

3、成功开启后项目中会有"项目名.entitlements"的文件生成,如果本来就有,其中的权限配置会有所改变。

4、项目中需要添加一些代码:

#import <NetworkExtension/NetworkExtension.h>

- (void)viewDidLoad{   [super viewDidLoad];   ...    //VPN    [self createVPNProfile];    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(VPNStatusDidChangeNotification) name:NEVPNStatusDidChangeNotification object:nil];    }

#pragma mark VPNs- (void)createVPNProfile{    [[NEVPNManager sharedManager] loadFromPreferencesWithCompletionHandler:^(NSError *error) {                if (error) {            NSLog(@"Load config failed [%@]", error.localizedDescription);            return;        }else{            NSLog(@"Load config success");        }                if ([NEVPNManager sharedManager].protocolConfiguration) {            // config exists        }                [self setupIPSec];        [[NEVPNManager sharedManager] saveToPreferencesWithCompletionHandler:^(NSError *error) {            if (error) {                NSLog(@"Save config failed [%@]", error.localizedDescription);            }else{                NSLog(@"Save config success");            }                    }];            }];}- (void)removeVPNProfile{    [[NEVPNManager sharedManager] loadFromPreferencesWithCompletionHandler:^(NSError *error){        if (!error)        {            [[NEVPNManager sharedManager] removeFromPreferencesWithCompletionHandler:^(NSError *error){                if(error)                {                    NSLog(@"Remove error: %@", error);                }                else                {                    NSLog(@"removeFromPreferences");                }            }];        }    }];    }- (void)setupIPSec{    // config IPSec protocol    NEVPNProtocolIPSec *p = [[NEVPNProtocolIPSec alloc] init];    p.username = @"name";    p.serverAddress = @"IP";        // get password persistent reference from keychain    NSString *password = @"password";    NSData *paswordData = [password dataUsingEncoding:NSUTF8StringEncoding];    p.passwordReference = paswordData;        // PSK    p.authenticationMethod = NEVPNIKEAuthenticationMethodSharedSecret;    NSString *secret = @"secret";    NSData *secretData = [secret dataUsingEncoding:NSUTF8StringEncoding];    p.sharedSecretReference = secretData;        p.useExtendedAuthentication = NO;    p.disconnectOnSleep = NO;        [NEVPNManager sharedManager].protocolConfiguration = p;    [NEVPNManager sharedManager].localizedDescription = @"VPN by ibed";}- (void)connect{    [[NEVPNManager sharedManager] loadFromPreferencesWithCompletionHandler:^(NSError *error){        if (!error)        {            //配置IPSec            [self setupIPSec];            [[NEVPNManager sharedManager].connection startVPNTunnelAndReturnError:nil];        }    }];}- (void)disconnect{    [[NEVPNManager sharedManager] loadFromPreferencesWithCompletionHandler:^(NSError *error){        if (!error)        {            [[NEVPNManager sharedManager].connection stopVPNTunnel];        }    }];}#pragma mark - VPN状态切换通知- (void)VPNStatusDidChangeNotification{    switch ([NEVPNManager sharedManager].connection.status)    {        case NEVPNStatusInvalid:        {            NSLog(@"NEVPNStatusInvalid");            break;        }        case NEVPNStatusDisconnected:        {            NSLog(@"NEVPNStatusDisconnected");            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];            break;        }        case NEVPNStatusConnecting:        {            NSLog(@"NEVPNStatusConnecting");            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];            break;        }        case NEVPNStatusConnected:        {            NSLog(@"NEVPNStatusConnected");            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];            break;        }        case NEVPNStatusReasserting:        {            NSLog(@"NEVPNStatusReasserting");            break;        }        case NEVPNStatusDisconnecting:        {            NSLog(@"NEVPNStatusDisconnecting");            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];            break;        }        default:            break;    }}#pragma mark VPN end


5、需要用户开启VPN权限。








0 0
原创粉丝点击