简体   繁体   中英

Call iOS webview HTTPS (self-signed certificate)

I want to call HTTPS url (self-signed certificate) in my iOS App. it works on the mobile browser but not in the App Webview.

Is there a solution for Xcode 7 and Swift 2.2?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let url = NSURL(string: "https://xxx.xxx.x.xxx:xxxx")

        let request = NSURLRequest(URL: url!)
        Webview.loadRequest(request)
    }

You have to install the Self Signed Certificate or CA on the device in order for the device to trust it then only device trusts the SSL connection.

In the case of installing self signed certificate make sure domain name of the URL is same as Common name of certificate.

If there is no domain name then IP address is fine.

Certificate installation:

You can just host it on the web server and try to access it from safari then iOS will prompt for the certificate installation in the iOS Device

Certificate Creation:

Here is the way to create self signed certificate so that you can fill all the details and host in web server.

openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 1001 -nodes

(Pay attention while entering the value for Common Name)

try to add below code in your plist file.

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM