为什么swift解析.getDataInBackgroundWithBlock被阻止为明文http请求?

时间:2016-08-19 00:58:12

标签: ios swift heroku parse-platform app-transport-security

我正在编写一个使用Heroku托管的Parse的快速iOS应用程序。据我所知,所有数据传输都是通过HTTPS进行的,我没有对info.plist进行App Transport Security变通办法(并打算保持这种方式)。到目前为止,所有Parse查询都在模拟器和运行9.3.3或9.3.5的实际iphone上执行时没有错误。

直到现在,我添加了这个代码,它在模拟器上完美运行但由于通过HTTP发出的明文请求而在iphone上崩溃。但是为什么要通过HTTP发出请求?

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier(idInstagramFeedCell, forIndexPath: indexPath) as! InstagramFeedCell

        let imageFile = feed[indexPath.row].imageFile as PFFile
        imageFile.getDataInBackgroundWithBlock({ (data, error) in
            if let image = UIImage(data: data!) {
                cell.postImage.image = image
            } else {
                cell.postImage.image = UIImage(named: defaultImageFile)
            }
        })

        cell.postUsername.text = feed[indexPath.row].username
        cell.postCaption.text = feed[indexPath.row].caption
        return cell
    }

违规行被隔离到imageFile.getDataInBackgroundWithBlock({ ... }),因为如果这被注释掉,应用程序就不会在iPhone上崩溃。

控制台中的错误是:

2016-08-18 18:51:56.074 ParseStarterProject-Swift[3694:2189084] App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
2016-08-18 18:51:56.081 ParseStarterProject-Swift[3694:2189342] [Error]: The resource could not be loaded because the App Transport Security policy requires the use of a secure connection. (Code: 100, Version: 1.12.0)
2016-08-18 18:51:56.081 ParseStarterProject-Swift[3694:2189342] [Error]: Network connection failed. Making attempt 1 after sleeping for 1.373388 seconds.
2016-08-18 18:51:56.084 ParseStarterProject-Swift[3694:2189342] [Error]: The resource could not be loaded because the App Transport Security policy requires the use of a secure connection. (Code: 100, Version: 1.12.0)

奇怪this poster有一个相反的问题。任何帮助将不胜感激。

其他观察:我实际上只是在模拟器上看到它崩溃了。最初上传(即发布)到Parse的所有图像都是来自模拟器本身的照片。当在实际设备上运行的应用程序尝试下载这些图像时,它会如上所述崩溃。我已经使用该应用程序发布了从实际设备到Parse的几张照片。当在模拟器上运行的应用程序尝试下载这些照片时,模拟器会因上述错误而崩溃。

2 个答案:

答案 0 :(得分:2)

在GitHub上提供@luizmb,解决方案很简单。显然,这个鲜为人知的(至少对我来说)Parse服务器变量publicServerURL需要包含与serverURL相同的值。如果依靠Heroku Parse控制台来管理实例,可以通过定义环境变量PARSE_PUBLIC_SERVER_URL来设置它。如果您有自定义实例,则需要在实例化Parse时将此行添加到index.js

publicServerURL: process.env.PARSE_PUBLIC_SERVER_URL || process.env.PARSER_SERVER_URL || process.env.SERVER_URL,

但是有两点需要注意:1)如果您在实施此解决方案之前上传了任何图像文件,您可能仍会遇到ATS错误。要解决此问题,您需要从我的实例中删除所有此类图像。 2)我只能使它在实际设备(iOS 9.3.5)上工作,但在模拟器(iOS 9.3)上运行完全相同的应用程序,ATS错误没有显示,但这些我没有找到的方法旁通:

NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
[Error]: An SSL error has occurred and a secure connection to the server cannot be made. (Code: 100, Version: 1.14.2)
[Error]: Network connection failed. Making attempt 4 after sleeping for 15.848020 seconds.

此解决方案可让您的应用从Parse下载图像文件,而无需通过AllowArbitraryLoads中的info.plist关闭ATS。请参阅here for details

答案 1 :(得分:0)

您似乎正在使用旧的Parse SDK,将其更新到最新版本(v1.14.2),问题可能会消失。