Tag: alamofire

如何检查alamofire的互联网连接?

我正在使用下面的代码在服务器中发出HTTP请求。现在我想知道它是否连接到互联网或不。下面是我的代码 let request = Alamofire.request(completeURL(domainName: path), method: method, parameters: parameters, encoding: encoding.value, headers: headers) .responseJSON { let resstr = NSString(data: $0.data!, encoding: String.Encoding.utf8.rawValue) print("error is \(resstr)") if $0.result.isFailure { self.failure("Network") print("API FAILED 4") return } guard let result = $0.result.value else { self.unKnownError() self.failure("") print("API FAILED 3") return } self.handleSuccess(JSON(result)) }

无法从软件包加载Info.plist

无法运行应用程序。 发生捆绑错误时无法加载Info.plist。 当我CMD+K清理项目,它运行良好。 但每次我进行更改并运行应用程序时,我都必须清理该项目。 以下是我的podfile的截图:

具有嵌套embedded式框架的iOS 8+框架

我创build了一个自定义的iOS框架LoginKit。 它反过来在下面embeddedAlamofire框架。 在模拟器中,一切工作正常,但试图在设备上运行时,我得到的错误: Dyld错误消息: Library not loaded: @rpath/Alamofire.framework/Alamofire Referenced from: /Users/USER/Library/Developer/CoreSimulator/Devices/506B47DE-804F-477F-AA90-69DF039E07FA/data/Containers/Bundle/Application/26D0CA8F-7284-42B5-8091-E5915736DCDB/Bingo.app/Bingo Reason: image not found 在父应用程序的“embedded式二进制文件”部分,我只有LoginKit.framework。 现在,如果我也embeddedAlamofire.framework(从LoginKit下取),错误消失。 这是行为吗? 这不是破坏封装的全部目的吗?

Alamofire 4.0上传MultipartFormData标题

我们如何添加一个身份validation头到Alamofire 4.0的上传function? 下面是示例代码,但是我看不出为函数添加标题。 Alamofire.upload( multipartFormData: { multipartFormData in multipartFormData.append(unicornImageURL, withName: "unicorn") multipartFormData.append(rainbowImageURL, withName: "rainbow") }, to: "https://httpbin.org/post", encodingCompletion: { encodingResult in switch encodingResult { case .success(let upload, _, _): upload.responseJSON { response in debugPrint(response) } case .failure(let encodingError): print(encodingError) } } ) 之前的alamofire版本支持直接添加标题,但不支持添加标题。 有任何想法吗?

使用重试时根据http错误代码更新令牌

我发现这个例子如何刷新oauth令牌使用moya和rxswift ,我不得不稍微改变,以编译。 此代码为我的scheme工作80%。 它的问题是,它会运行所有的HTTP错误,而不仅仅是401错误。 我想要的是把所有其他的http错误作为错误传递,以便我可以在别处处理它们,而不是在这里吞下它们。 有了这个代码,如果我得到一个HttpStatus 500 ,它将运行validation码3次,这显然不是我想要的。 我试图改变这个代码来处理只处理401错误,但似乎不pipe我做了什么,我不能得到的代码编译。 它总是抱怨错误的返回types, "Cannot convert return expression of type Observable<Response> to return type Observable<Response>"这对我来说没有任何意义.. 我想要的:处理401,但停止所有其他错误 import RxSwift import KeychainAccess import Moya public extension ObservableType where E == Response { /// Tries to refresh auth token on 401 errors and retry the request. /// If the refresh fails, the […]

如何在Alamofire中暂停/恢复/取消我的下载请求

我正在下载一个文件使用Alamofire下载进度,但我不知道如何暂停/恢复/取消特定的请求。 @IBAction func downloadBtnTapped() { Alamofire.download(.GET, "http://httpbin.org/stream/100", destination: destination) .progress { (bytesRead, totalBytesRead, totalBytesExpectedToRead) in println(totalBytesRead) } .response { (request, response, _, error) in println(response) } } @IBAction func pauseBtnTapped(sender : UIButton) { // i would like to pause/cancel my download request here }

Alamofire接受和内容types的JSON

我试图在Swift中用Alamofire做一个GET请求。 我需要设置以下标题: Content-Type: application/json Accept: application/json 我可以绕过它,并直接指定请求的标题,但我想要使用ParameterEncoding ,如库中所build议的。 到目前为止,我有这样的: Alamofire.request(.GET, url, encoding: .JSON) .validate() .responseJSON { (req, res, json, error) in if (error != nil) { NSLog("Error: \(error)") println(req) println(res) } else { NSLog("Success: \(url)") var json = JSON(json!) } } Content-Type设置,但不Accept 。 我怎样才能正确地做到这一点?

Alamofire无法正常工作(Swift / Xcode 8)

我试图导入Alamofire到我的项目时(Cocoapods不为我工作,所以我必须手动导入它)时出现以下错误。 无论如何,我正在使用XCode 8和Swift 2.3,我得到这些错误: 更新:我清理XCode,下载最新版本的Alamofire,并重新启动我的电脑。 现在,XCode似乎给了我相互矛盾的错误(图片供参考) 谢谢!

如何使用Swift在iOS上同步运行两个函数

让我们提出这个场景 一个asynchronousnetworking操作的方法 func asyncMethodA() -> String? { result : String? Alamofire.manager.request(.POST, "https://www.apiweb.com/apimethod", parameters: parameters, encoding:.JSON) .response { (request, response, rawdata, error) in if (response?.statusCode == 200) { //DO SOME HEAVY LIFTING } } return result //string } 另一种asynchronousnetworking操作的方法 func asyncMethodB() -> String? { result : String? Alamofire.manager.request(.POST, "https://www.yetanotherapiweb.com/apimethod", parameters: parameters, encoding:.JSON) .response { (request, response, […]

Alamofire无法访问JSON响应的键

我新使用Alamofire并遇到问题。 我可以运行下面的代码来打印出API端点的所有数据。 Alamofire.request("http://codewithchris.com/code/afsample.json").responseJSON { response in if let JSON = response.result.value { print(JSON) } } 问题是,当我运行这个: Alamofire.request("http://codewithchris.com/code/afsample.json").responseJSON { response in if let JSON = response.result.value { print(JSON["firstkey"]) } } 我得到的错误: types“任何”没有下标成员 我不知道为什么这个错误发生,似乎我正确访问数据。 任何帮助将是伟大的,谢谢! 我已经尝试使用两种格式: 打印(JSON [“firstkey”]作为string) 和 打印(JSON [“firstkey”]为[String:Any] 但他们仍然给出同样的错误。 这是我的端点上的JSON: { "firstkey":"it worked!", "secondkey":["item1", "item2", "item3"] }