【问题标题】:can we add Custom headers to Alamofire file download : IOS/SWIFT我们可以在 Alamofire 文件下载中添加自定义标头吗:IOS/SWIFT
【发布时间】:2015-04-21 17:22:23
【问题描述】:

我知道如何向 alamofire 请求添加自定义标头,但是是否可以向 alamofire 下载添加自定义标头?另外,如何更改保存在文件路径中的文件扩展名?

Alamofire.download(.GET, myURL, { (temporaryURL, response) in
      if let directoryURL = NSFileManager.defaultManager()
        .URLsForDirectory(.DocumentDirectory,
          inDomains: .UserDomainMask)[0]
        as? NSURL {
          let pathComponent = response.suggestedFilename

          return directoryURL.URLByAppendingPathComponent(pathComponent!)
      }
      return temporaryURL
    }).response{ (_,_, data, err) -> Void in
      println(data)
    }

【问题讨论】:

  • 如果您将两个问题作为单独的问题提出,您可能会得到更好的结果。
  • 因为两者都与 alamofire 下载有关,我想我可以在一个问题中问他们。在查看结果后,我将尝试创建另一个问题。谢谢

标签: ios file swift download alamofire


【解决方案1】:

这是一个与您的代码示例相匹配的代码示例,使用 @mattt 的建议,即使用已附加标头的 NSURLRequest

let URLString = "whatever/floats/your/boat"
let URLRequest = NSMutableURLRequest(URL: NSURL(string: URLString)!)
URLRequest.setValue("Header Value", forHTTPHeaderField: "Header Name")

let request = Alamofire.download(URLRequest) { temporaryURL, response in
    let fileManager = NSFileManager.defaultManager()

    if let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as? NSURL {
        let pathComponent = response.suggestedFilename
        return directoryURL.URLByAppendingPathComponent(pathComponent!)
    }

    return temporaryURL
}

request.response { _, response, data, error in
    if let data = data as? NSData {
        println(NSString(data: data, encoding: NSUTF8StringEncoding))
    } else {
        println(response)
        println(error)
    }
}

【讨论】:

    【解决方案2】:

    我找到了解决方案。

    var headers = Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders ?? [:]
        headers = [ "X-ApiKey" : "someValue",
          "Content-Type" : "application/json; charset=utf-8"]
    
        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        configuration.HTTPAdditionalHeaders = headers
    
        let manager = Alamofire.Manager(configuration: configuration)
        let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)
        manager.download(.GET, "place your URL here", destination: destination)
          .progress { (bytesRead, totalBytesRead, totalBytesExpectedToRead) in
            println(totalBytesRead)
          }
          .response { (request, response, data, error) in
            fileName = response?.suggestedFilename
            completionHandler(fileName: fileName as String?, error: error)
            println(data)
        }
    

    希望这对某人有所帮助。谢谢

    【讨论】:

    • 一个更简单的方法是调用download 传递一个带有所需标头的NSURLRequest 对象。
    • 代码中的第二行,不应该是 headers["X-ApiKey"] = "some value";或标题 += ["X-ApiKey": "some value"]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 2021-09-10
    • 1970-01-01
    • 2014-07-12
    相关资源
    最近更新 更多