POST with swift 2 on UIWebView does not work

2019-08-11 10:08发布

Before swift 2, the code below used to work. However after upgrading to Swift 2, my code below no longer worked. I had verified with Postman and RESTClient that my web services worked.

let url = NSURL(string: "https://")
        let mutableURL = NSMutableURLRequest(URL:url!)
        mutableURL.HTTPMethod = "POST"
        let str = "user_id=90031963"
        mutableURL.HTTPBody = str.dataUsingEncoding(NSUTF8StringEncoding)
        leaderboardWebView.loadRequest(mutableURL)

Any comments and feedback will be very much appreciated.

Cheers

1条回答
你好瞎i
2楼-- · 2019-08-11 10:56

Swift2 is very strict when it comes to secure online connection. It could be the connection is rejected because your server doesn't have a valid ssh certificate. For test purpose try to add to your info.plist this Dictionary:

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

If it works, it means you need to check your server security setup. You can read more about it over here: https://developer.apple.com/library/prerelease/ios/technotes/App-Transport-Security-Technote/

Note that Apple does not recommend to use this dictionary unless you are not really requiring a secure connection.

查看更多
登录 后发表回答