菜鸟教程小白 发表于 2022-12-12 21:46:21

ios - 为什么将 .getDataInBackgroundWithBlock 快速解析为明文 http 请求?


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

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

<pre><code>override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {

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

      let imageFile = feed.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.username
      cell.postCaption.text = feed.caption
      return cell
    }
</code></pre>

<p>违规行与 <code>imageFile.getDataInBackgroundWithBlock({ ... })</code> 隔离,因为如果将其注释掉,应用程序不会在 iphone 上崩溃。</p>

<p>控制台中的错误是:</p>

<pre><code>2016-08-18 18:51:56.074 ParseStarterProject-Swift App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app&#39;s Info.plist file.
2016-08-18 18:51:56.081 ParseStarterProject-Swift : 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 : Network connection failed. Making attempt 1 after sleeping for 1.373388 seconds.
2016-08-18 18:51:56.084 ParseStarterProject-Swift : 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)
</code></pre>

<p>奇怪的是 <a href="https://stackoverflow.com/q/36232317/1827488" rel="noreferrer noopener nofollow">this poster</a>有一种相反的问题。任何帮助将不胜感激。</p>

<p><strong>其他观察结果</strong>:我实际上只是在模拟器上看到它崩溃了。最初上传(即发布)到 Parse 的所有图像都是来自模拟器本身的照片。当在实际设备上运行的应用程序尝试下载这些图像时,它会按上述方式崩溃。自从使用该应用程序后,我已经从实际设备上发布了几张照片到 Parse。当模拟器上运行的应用程序尝试下载这些照片时,模拟器崩溃并出现与上述相同的错误。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>感谢 GitHub 上的 @luizmb,解决方案很简单。显然,有一个鲜为人知的(至少对我而言)解析服务器变量 <code>publicServerURL</code>,它需要包含与 <code>serverURL</code> 相同的值。如果您依赖 Heroku Parse 控制台来管理您的实例,您可以通过定义环境变量 <code>PARSE_PUBLIC_SERVER_URL</code> 来设置它。如果您有自定义实例,则需要在实例化 Parse 时将这一行添加到 <code>index.js</code> 中:</p>

<pre><code>publicServerURL: process.env.PARSE_PUBLIC_SERVER_URL || process.env.PARSER_SERVER_URL || process.env.SERVER_URL,
</code></pre>

<p>但是有两个注意事项:1)如果您在像我一样实现此解决方案之前上传了任何图像文件,您可能仍然会遇到 ATS 错误。要解决此问题,您需要像我一样从您的实例中删除所有此类图像。 2)我只能让它在实际设备(iOS 9.3.5)上运行,但在模拟器(iOS 9.3)上运行完全相同的应用程序,ATS错误没有出现,但这些我还没有找到方法绕过:</p>

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

<p>此解决方案使您的应用可以从 Parse 下载图像文件,而无需通过 <code>info.plist</code> 中的 <code>AllowArbitraryLoads</code> 关闭 ATS。请参阅 <a href="https://github.com/ParsePlatform/Parse-SDK-iOS-OSX/issues/1022" rel="noreferrer noopener nofollow">here for details</a> .</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 为什么将 .getDataInBackgroundWithBlock 快速解析为明文 http 请求?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39029619/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39029619/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 为什么将 .getDataInBackgroundWithBlock 快速解析为明文 http 请求?