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
- iOS项目添加VPN
- iOS项目添加CocoaPods
- Ios项目添加Pods
- iOS 项目添加背景音乐
- IOS OpenSSL项目添加
- iOS vpn
- ios添加GPUImage到项目
- iOS 项目中添加字体库
- openwrt 添加 VPN 拨号
- vpn添加日志
- iOS Undocumented VPN API
- iOS - VPN强烈推荐
- iOS - 开发之VPN
- iOS开发之VPN
- 添加 core data 支持 ios 项目
- ios 项目添加AdMob 错误记录
- 添加 core data 支持 ios 项目
- 为iOS项目添加Daily Build
- android 将手机号中间隐藏为星号(*)
- Linux 服务器模型
- SQLServer解决孤立用户问题
- Linux常用命令-文件处理命令
- weblogic发送https请求的证书错误的解决办法
- iOS项目添加VPN
- JNI完全指南(二)——类与异常
- spacemacs c++ 智能提示
- Ubuntu15.04源码安装Node.js
- php去除空数组且数组键名重置
- DBMS_REDEFINITION用法
- mysql安装与配置
- rabbitmq http.api
- 精心整理python自动化测试小技巧二