the code of my app
@interface VPNManager()
@property (nonatomic, strong) VPNManagerModel *vpnConfigurationModel;
@property (nonatomic, strong) NEVPNManager *manager;
@property (nonatomic, assign) NSInteger writeAmount;
@end
@implementation VPNManager
- (BOOL)startVPN {
[[NSNotificationCenter defaultCenter] postNotificationName:@"showError" object:self userInfo:@{@"info": @"configManagerWithModel"}];
if (self.vpnManager.connection.status == NEVPNStatusDisconnected) {
NSError *error;
[self.vpnManager.connection startVPNTunnelAndReturnError:&error];
if (error != 0) {
const char *errorInfo = [NSString stringWithFormat:@"%@",error].UTF8String;
[[NSNotificationCenter defaultCenter] postNotificationName:@"showError" object:self userInfo:@{@"info": [NSString stringWithFormat:@"Start VPN Failed -%@",error]}];
}else {
//point A
[[NSNotificationCenter defaultCenter] postNotificationName:@"showError" object:self userInfo:@{@"info": @"Start VPN Success !"}];
return YES;
}
}else {
[[NSNotificationCenter defaultCenter] postNotificationName:@"showError" object:self userInfo:@{@"info":[NSString stringWithFormat:@"Start VPN - The current connect status isn't NEVPNStatusDisconnected ! %ld",self.vpnManager.connection.status] }];
}
return NO;
}
and PacketTunnelProvider Network Extension
#import "PacketTunnelProvider.h"
#include
#include
#include
#define _NET_MTU 1400
#define _NET_REMOTEADDRESS "10.10.10.1"
#define _NET_SUBNETMASKS "255.255.255.255"
#define _NET_DNS "8.8.8.8"
#define _LOCAL_ADDRESS "127.0.0.1"
#define _NET_TUNNEL_IPADDRESS "10.8.0.2"
@interface PacketTunnelProvider ()
@property NWTCPConnection *connection;
@property (strong) void (^pendingStartCompletion)(NSError *);
@end
@implementation PacketTunnelProvider
- (void)startTunnelWithOptions:(NSDictionary *)options completionHandler:(void (^)(NSError *))completionHandler{
//point B
[[NSNotificationCenter defaultCenter] postNotificationName:@"showError" object:nil userInfo:nil]; //send message to notify has been triggered
NEPacketTunnelNetworkSettings *tunnelNetworkSettings = [[NEPacketTunnelNetworkSettings alloc] initWithTunnelRemoteAddress:@_NET_REMOTEADDRESS];
tunnelNetworkSettings.MTU = [NSNumber numberWithInteger:_NET_MTU];
tunnelNetworkSettings.IPv4Settings = [[NEIPv4Settings alloc] initWithAddresses:[NSArray arrayWithObjects:@_NET_TUNNEL_IPADDRESS, nil] subnetMasks:[NSArray arrayWithObjects:@_NET_SUBNETMASKS, nil]];
tunnelNetworkSettings.IPv4Settings.includedRoutes = @[[NEIPv4Route defaultRoute]];
NEIPv4Route *excludeRoute = [[NEIPv4Route alloc] initWithDestinationAddress:@"10.12.23.90" subnetMask:@"255.255.255.255"];
tunnelNetworkSettings.IPv4Settings.excludedRoutes = @[excludeRoute];
[self setTunnelNetworkSettings:tunnelNetworkSettings completionHandler:^(NSError * _Nullable error) {
if (error == nil) {
log4cplus_info("VPNManager", "PacketTunnelManager - Start Tunel Success !");
completionHandler(nil);
}else {
log4cplus_error("VPNManager", "PacketTunnelManager - Start Tunel Failed - %s !",error.debugDescription.UTF8String);
completionHandler(error);
return;
}
}];
[self readPakcets];
}
point A can be triggered, start VPN and show VPN icon on left top of iPhone correctly.
but point B never be triggered
your comment welcome