Xcode - 无法打开,因为不支持URL类型http

时间:2016-04-16 17:24:38

标签: swift amazon-s3 nsdata nsurl

我已将图片上传到amazonS3广告位。我正在尝试下载指定用户的个人资料图片。 Xcode表示文件无法打开,因为URL类型http不受支持..我是否忽略了某些内容?

func retrieveProPic(proPicString: String, userID: String){

    let downloadRequest = AWSS3TransferManagerDownloadRequest()
    downloadRequest.bucket = "profilepicturetest1"
    downloadRequest.key = userID
    let proPicURL = NSURL(string: proPicString)
    if let picURL = proPicURL {
        downloadRequest.downloadingFileURL = picURL
    }

    let transferManager = AWSS3TransferManager.defaultS3TransferManager()
    transferManager.download(downloadRequest).continueWithBlock { (task) -> AnyObject? in
        if let error = task.error {
            print("Failed to download because of (\(error))")
        }
        if task.result != nil {

            if let data = NSData(contentsOfURL: downloadRequest.downloadingFileURL)

            {
                self.proPicImage = UIImage(data: data)
                dispatch_async(dispatch_get_main_queue()){
                    self.tableView.reloadData()
                }
            }

            print("made it to have result")

        } else {

            print ("Unexpected empty result")

        }

        return nil

    }

}

2 个答案:

答案 0 :(得分:0)

将以下内容添加到Info.plist

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

或者如果您想手动输入它们:

  1. App Transport Security Settings作为字典添加到您的媒体资源列表中。
  2. 按新创建的词典上的+,然后选择Allow Arbitrary Loads
  3. 将密钥设置为YES
  4. 这应仅用作临时解决方案,因为建议使用与网络应用程序的安全连接。

答案 1 :(得分:0)

我是愚蠢的。需要创建临时文件路径。

func retrieveProPic(proPicString: String, userID: String){
   let downloadedFilePath = NSTemporaryDirectory().stringByAppendingString("downloaded-myImage.jpg")
   let downloadedFileURL = NSURL(fileURLWithPath: downloadedFilePath)

   let downloadRequest = AWSS3TransferManagerDownloadRequest()
   downloadRequest.bucket = "profilepicturetest1"
   downloadRequest.key = userID
   downloadRequest.downloadingFileURL = downloadedFileURL

let transferManager = AWSS3TransferManager.defaultS3TransferManager()
transferManager.download(downloadRequest).continueWithBlock { (task) -> AnyObject? in
    if let error = task.error {
        print("Failed to download because of (\(error))")
    }
    if task.result != nil {

        if let data = NSData(contentsOfURL: downloadedFileURL)

        {
            self.proPicImage = UIImage(data: data)
            dispatch_async(dispatch_get_main_queue()){
                self.tableView.reloadData()
            }
        }

        print("made it to have result")

    } else {

        print ("Unexpected empty result")

    }

    return nil

}