iOS項目添加VPN

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權限。








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章