OpenVPNAdapter
OpenVPNAdapter copied to clipboard
VPN not Connecting on iOS
I don't know where I am making mistake. Below is the code I am using to connect VPN in iOS app.
@objc private func establishVPNConnection() {
let callback = { (error: Error?) -> Void in
self.manager?.loadFromPreferences(completionHandler: { (error) in
guard error == nil else {
print("\(error!.localizedDescription)")
return
}
let options: [String : NSObject] = [
"username": "" as NSString,
"password": "" as NSString
]
do {
try self.manager?.connection.startVPNTunnel(options: options)
} catch {
print("\(error.localizedDescription)")
}
})
}
configureVPN(callback: callback)
}
extension ViewController {
// MARK: -
func configureVPN(callback: @escaping (Error?) -> Void) {
NETunnelProviderManager.loadAllFromPreferences { (managers, error) in
guard error == nil else {
print("\(error!.localizedDescription)")
callback(error)
return
}
let passwordKey = "1111"
do {
try self.keychain.set("sohil", key: passwordKey)
} catch {
print("\(error.localizedDescription)")
callback(error)
return
}
guard let passwordReference = self.keychain[attributes: passwordKey]?.persistentRef else {
fatalError()
}
self.manager = managers?.first ?? NETunnelProviderManager()
self.manager?.loadFromPreferences(completionHandler: { (error) in
guard error == nil else {
print("\(error!.localizedDescription)")
callback(error)
return
}
let configurationFile = Bundle.main.url(forResource: "[email protected]", withExtension: "ovpn")
let configurationContent = try! Data(contentsOf: configurationFile!)
let tunnelProtocol = NETunnelProviderProtocol()
tunnelProtocol.serverAddress = ""
tunnelProtocol.providerBundleIdentifier = "com.open.demo.tunnelprovider"
tunnelProtocol.providerConfiguration = ["configuration": configurationContent]
tunnelProtocol.username = "sohil"
tunnelProtocol.passwordReference = passwordReference
tunnelProtocol.disconnectOnSleep = false
self.manager?.protocolConfiguration = tunnelProtocol
self.manager?.localizedDescription = "OpenVPN iOS Client"
self.manager?.isEnabled = true
self.manager?.saveToPreferences(completionHandler: { (error) in
guard error == nil else {
print("\(error!.localizedDescription)")
callback(error)
return
}
callback(nil)
})
})
}
}
}
I am using profile with username = sohil & password = sohil.
No luck, what am I doing wrong can anybody pls help?
Hi @sohilmemon,
Have you properly configured the packet tunnel provider extension and have you tried to debug it step by step?
Yes I have but under extension I cannot see any debug is going! and under main Controller there is no error responding.
Should I send the app to check at your end?
I'm afraid I don't have time to check your app right now. But you can try this approach to debug, and also check system/device log. If something is wrong with the extension configuration you will see a error there.
Sure, and I will update you with the log and errors if I am unable to solve.
@sohilmemon Any luck?
Yes, I managed to get it connected 👯♂️
@sohilmemon so how did you manage to get it connected?
@sohilmemon could you please share pointers / sample code that could help us get the connection up and running.
@sohilmemon have you found the solution iam also facing same issue please help me out