In my app I am trying to configure VPN settings in App load delegate. I am calling following method in my app delegate
- (void)configureVPN {
NEVPNManager *manager = [NEVPNManager sharedManager];
[manager loadFromPreferencesWithCompletionHandler:^(NSError * _Nullable loadError) {
if (loadError) {
NSLog(#"vpn setup error: %#", loadError);
} else {
[manager setOnDemandEnabled: YES];
NSMutableArray *rules = [[NSMutableArray alloc] init];
NEOnDemandRuleConnect *connectRule = [NEOnDemandRuleConnect new];
[rules addObject:connectRule];
[manager setOnDemandRules:rules];
[manager saveToPreferencesWithCompletionHandler:^(NSError * _Nullable saveError) {
if (saveError) {
NSLog(#"vpn setup error: %#", saveError);
} else {
NSLog(#"vpn config set");
NSError *connError;
[manager.connection startVPNTunnelAndReturnError:&connError];
if (connError) {
NSLog(#"Unable to connect to VPN: %#", connError);
} else {
NSLog(#"VPN connection established");
}
}
}];
}
}];
}
but I am getting error on manager loadFromPreferencesWithCompletionHandler
Error:
Failed to load the configuration: Error Domain=NEVPNErrorDomain Code=5 "permission denied" UserInfo={NSLocalizedDescription=permission denied}
I thought it was because of missing capabilities but Personal VPN is enabled in capabilities.
Go to Xcode -> Project -> Targets -> Capabilities and
Enable VPN and Enable Network Extensions.
To fix the issue, go to Xcode > Project > capabilities and enable personal VPN.
In my case, I had added the Capabilities correctly, but an error is still reported
when I restart my iPhone , then run the project, every thing work fine
Related
When I use NEHotspotConfigurationManager connect to a WiFi in iOS app, it can work fine.
However, I run this app in M1 Mac to connect to a WiFi always get the message:
Domain=NEHotspotConfigurationErrorDomain Code=8 "internal error." UserInfo={NSLocalizedDescription=internal error.}
I choose run destination:
My Mac(Designed for iPhone)
This is code I am use in my project:
NEHotspotConfiguration * hotspotConfig = [[NEHotspotConfiguration alloc] initWithSSID:_wifi_SSID passphrase:_wifi_PASSWORD isWEP:NO];
[[NEHotspotConfigurationManager sharedManager] applyConfiguration:hotspotConfig completionHandler:^(NSError * _Nullable error) {
NSLog(#"=%#", error);
if (!error) {
NSLog(#"Success to connect");
}else{
NSLog(#"Fail to connect or already to connect");
}
}];
I don't understand if NEHotspotConfigurationManager support Mac WiFi setting.
Thanks in advance!
I am trying to do autoconnect in iOS with below code
if([Utils isEmpty:password]){
configuration = [[NEHotspotConfiguration alloc] initWithSSID: wifiSSID];
}else{
configuration = [[NEHotspotConfiguration alloc] initWithSSID: wifiSSID passphrase: password isWEP: NO];
}
configuration.joinOnce = YES;
/* Alert the OS that the user wants to connect to the WiFi */
[[NEHotspotConfigurationManager sharedManager] applyConfiguration: configuration completionHandler: ^ (NSError * _Nullable error) {
if (nil == error) {
DLog (# "Is Connected!!");
[WiFiManager sendCallback:CONNECTED callback:callback];
} else {
DLog (# "Error is:%#", error);
[WiFiManager sendCallback:UNKNOWN callback:callback];
}}];
Here I have joinOnce = YES.
What does this actually do when it is set to NO.
I don't find any real difference between YES and NO.
Anyone please explain.
When joinOnce is set to true, it means that the connection to that hotspot will be disconnected and the configuration will be forgotten as soon as the device sleeps or your application has been in the background for some time.
Sometimes I am getting this error
Error Domain=NSPOSIXErrorDomain Code=65 "No route to host"
UserInfo={NSLocalizedDescription=No route to host,
NSLocalizedFailureReason=Error in send() function.}
when opening udp socket in some routers. I don't know what causes this.
This is my code for opening a udp socket:
- (void)openSocket
{
if(_asyncUdpSocket == nil)
{
_asyncUdpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
}
if(![_asyncUdpSocket isClosed])
{
return;
}
NSError *error;
if (![_asyncUdpSocket enableBroadcast:YES error:&error])
{
NSLog(#"AsyncUdp: enable broadcast failed error: %#", error);
}
if (![_asyncUdpSocket bindToPort:_port error:&error])
{
NSLog(#"AsyncUdp: bind to port %li failed error: %#", (unsigned long)_port, error);
}
if (![_asyncUdpSocket joinMulticastGroup:_address error:&error])
{
NSLog(#"AsyncUdp: join multicast group %# failed error: %#", _address, error);
}
if(![_asyncUdpSocket beginReceiving:&error])
{
NSLog(#"AsynUdp: begin receiving error: %#", error);
}
if(error != nil)
{
NSLog(#"Udp failed to open!");
[_delegate onUdpSocketDidOpenfailed];
}
else
{
NSLog(#"UdpSocket - opend udp socket successful!");
[_delegate onUdpSocketDidOpen];
}
}
Take note that this happens really often in iPhone 7
After updating Xcode to Version 12.5, I got the same issue when run a DLNA App project. I fixed it by downgrading Xcode to Version 12.4.
I had created network manager by using the below code. but as soon the first line of call is run the manager is being nil so any other methods are getting not effected. Can Anyone let me know that what wrong thing I had done ?
NEVPNManager *manager = [NEVPNManager sharedManager];
NEVPNProtocolIPSec *p = [[NEVPNProtocolIPSec alloc] init];
p.username = #"MYUSERNAME";
p.passwordReference = [#"MYPW" dataUsingEncoding:NSUTF8StringEncoding];
p.serverAddress = #"[NetworkIP]";
p.authenticationMethod = NEVPNIKEAuthenticationMethodSharedSecret;
p.sharedSecretReference = [#"SharedSecretHashCode" dataUsingEncoding:NSUTF8StringEncoding];
[manager setProtocol:p];
[manager setOnDemandEnabled:NO];
[manager setLocalizedDescription:#"VIT VPN"];
NSArray *array = [NSArray new];
[manager setOnDemandRules: array];
NSLog(#"Connection desciption: %#", manager.localizedDescription);
NSLog(#"VPN status: %li", (long)manager.connection.status);
[manager saveToPreferencesWithCompletionHandler:^(NSError *error) {
if(error) {
NSLog(#"Save error: %#", error);
}
}];
"manager" is being nil . please help. thanks in advance.
I found Solution for my question for the NEVPNManager instances.
When using this to the app developer must need its related certificates in which The APPID has has enabled the VPN Configuration & controls. and this app must need to run in the device.
So the NEVPNManager will not be nil and it can be adapt from the shared instance of the device. (also this is from NetworkExtension.framework which is available for iOS8 & higher devices)
May be this can help anyone who is having the same issue.
In iOS application is required to sign a certificate request, which had previously been obtained. When I try to run a query catch this error:
kCFURLErrorUserCancelledAuthentication -1012.
The documentation says:
kCFURLErrorUserCancelledAuthentication The connection failed because
the user cancelled required authentication.
Implemented as follows:
- (void)startConnection {
NSString *serverURL = #"host.ru/method";
MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:serverURL customHeaderFields:nil];
MKNetworkOperation *op = [engine operationWithPath:nil params:nil httpMethod:#"GET" ssl:YES];
NSString *thePath = [[NSBundle mainBundle] pathForResource:#"client" ofType:#"p12"];
[op setShouldContinueWithInvalidCertificate:YES];
op.clientCertificate = thePath;
op.clientCertificatePassword = #"1234qwerty";
[op addCompletionHandler:^(MKNetworkOperation *operation) {
NSLog(#"[operation responseData]-->>%#", [operation responseString]);
}errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
NSLog(#"MKNetwork request error : %#", [err localizedDescription]);
}];
[engine enqueueOperation:op];
}
What am I doing wrong?
P.S.
Certificate, which try to sign the request has been received in advance. It tested separately in the browser, it's okay.
An application for android to the same server requests are normally the same scheme.
This can happen when your connection sends a request for an authentication challenge.
A possible cause is that the site's certificate is invalid/untrusted and you have opted not to accept invalid certificates.