Swift/https:NSURLSession/NSURLConnection HTTP加载失败

Sag*_*usA 8 https ios xcode7 swift2

不幸的是,今天早上我的XCode更新到版本7,而我用http开发的iOS应用程序现在想要https.因此,在许多教程之后,我配置了我的MAMP服务器,以便使用https/ssl创建虚拟证书.现在在我的iOS应用中,URL如下所示:

static var webServerLoginURL = "https://localhost:443/excogitoweb/mobile/loginM.php"
static var webServerGetUserTasks = "https://localhost:443/excogitoweb/mobile/handleTasks.php"
static var webServerGetUsers = "https://localhost:443/excogitoweb/mobile/handleUsers.php"
static var webServerGetProjects = "https://localhost:443/excogitoweb/mobile/handleProjects.php"
Run Code Online (Sandbox Code Playgroud)

如果我尝试在浏览器中访问它们,它们工作正常.我习惯使用NSURLSession.sharedSession().dataTaskWithRequest()来访问数据库和php文件,现在它会引发标题中的错误.例如,这是引发错误的行:

if let responseJSON: [[String: String]] = (try? NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())) as? [[String: String]] {
...
}
Run Code Online (Sandbox Code Playgroud)

这是完整的错误消息:

2015-09-21 16:41:48.354 ExcogitoWeb[75200:476213] CFNetwork SSLHandshake failed (-9824)
2015-09-21 16:41:48.355 ExcogitoWeb[75200:476213] NSURLSession/NSURLConnection   HTTP load failed (kCFStreamErrorDomainSSL, -9824)
fatal error: unexpectedly found nil while unwrapping an Optional value
Run Code Online (Sandbox Code Playgroud)

我想知道如何解决这个问题.我在这里已经阅读了一些有用的答案,但有许多事情我仍然不明白,如果有人愿意帮助/解释我,我将非常感激.

myc*_*har 10

将其添加到您的应用中 Info.plist

<key>NSAppTransportSecurity</key>  
    <dict>  
    <key>NSAllowsArbitraryLoads</key>  
    <true/>  
    </dict>
Run Code Online (Sandbox Code Playgroud)

  • 这不安全.更好地利用这个:http://stackoverflow.com/a/32331282/1447641 (3认同)