POST with swift 2 on UIWebView does not work

半世苍凉 提交于 2019-12-11 09:18:18

问题


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:


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.



来源:https://stackoverflow.com/questions/32834393/post-with-swift-2-on-uiwebview-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!