alamofire

Get JSON result with GET request and parameters with Alamofire

末鹿安然 提交于 2019-12-01 05:34:15
问题 This is my url String with paramaters. http://api.room2shop.com/api/product/GetProducts?categoryId=22&filter=2&pageNumber=1 through which I am getting my JSON data. I have AFWrapper.swift file in which I have defined function for GETrequest. import UIKit import Alamofire import SwiftyJSON class AFWrapper: NSObject { class func requestGETURL(strURL: String, params : [String : AnyObject]?, success:(JSON) -> Void, failure:(NSError) -> Void) { Alamofire.request(.GET, strURL, parameters: params,

对于AlamoFire几点思考

只愿长相守 提交于 2019-12-01 04:42:18
本文由 CocoaChina --BYB_1132( 论坛ID )翻译 原文: Thoughts On AlamoFire--Swift’s AFNetworking Implementation HTTP协议就是现代开发的同义词,对于有经验的iOS开发者来说, 熟悉并尽可能使用这些流行的协议是日常工作的基础。 不出意料,iOS 应用在这一点上没有什么不同,成千上万的app和工程师都依靠广受欢迎的AFNetworking 库实现与服务器的交互,JSON解析, 以及提供占位符图片等多个功能。 简言之,做到这些并不容易。这篇文章中我们了要解的是Alamofire库。 基础 Alamofire的核心主要是试图简化iOS中HTTP网络连接, 它通过使用NSURLSession以及Foundation URL Loading System来创建一个Swift本地的网络访问接口,从而实现令人难以置信效率的任务。 Swift放弃了代理模式取而代之的使用了回调。对于我而言,我喜欢这个选择。然而基础承诺机制模式( promise-based patterns )也能起到一定作用, 它们也隐藏了一些坏代码味道,对于一些人有点太神奇了。 进一步看,它是异步实现的。你也许听说过,但是 在主线程上执行网络调用并不是一个好主意 ,不过Alamofire采用了许多创造性的最优方法。 例如

Swift Alamofire add custom header to all requests

白昼怎懂夜的黑 提交于 2019-12-01 04:12:26
I tried to add custom header with this: let manager = Manager.sharedInstance manager.session.configuration.HTTPAdditionalHeaders = [ "Authorization": "Token \(token)" ] But it doesn't work, what am i doing wrong? I need to add this after login, so that header will be used in all requests. I don't know where you do that but my AlomoFire requests look like : Alamofire.request(.GET, urlPath, parameters: parameters, headers: ["X-API-KEY": apiKey, "Content-type application":"json", "Accept application" : "json"]).responseJSON() { (req,res, data, error) in //blah blah } My guess is that you can put

POST multiple json objects in Alamofire POST method - Swift/IOS

此生再无相见时 提交于 2019-12-01 04:08:42
Sorry if my question is not clear, I'll try to make myself clear with an explanation. So here is exactly what I'm trying to do, I'm trying to use Alamofire to post more than one comment (Something that my app implements and will be stored as a JSON object whenever user writes a new comment). I'm passing these JSON comments to my post routine, where I can use SwiftyJSON to extract each value. Noe the thing is I know how to set the parameters if I'm trying to authorize the user as follows, var parameters = [ "userName": userName, "password": passwordSalt, "somethingElse": somethingElse ] var err

Xcode 9 fails to build Swift 4 project with pod

拟墨画扇 提交于 2019-12-01 04:05:14
问题 When attempting to build my Swift 4 project in Xcode 9 beta 2, some code signing error occurs after which the build of my app is discontinued. The error that is shown in Xcode: /usr/bin/codesign --force --sign - --preserve-metadata=identifier,entitlements '/Users/nickgaens/Library/Developer/Xcode/DerivedData/Emprova_Swift-bbvmoytlhlbqpueyftstselvwhdj/Build/Products/Debug-iphonesimulator/Emprova GPS Swift.app/Frameworks/Alamofire.framework' /Users/nickgaens/Library/Developer/Xcode/DerivedData

Alamofire 4 and special characters in JSON

流过昼夜 提交于 2019-12-01 03:20:07
I've got a problem with special characters with Alamofire 4. The JSON contains æ, ø and å and the browser shows them fine, also my previous solution using SwiftyJSON did. Alamofire 4 shows something like this instead: U00e6 Using this call: Alamofire.request(specificURL, method: .get, parameters: param, encoding: URLEncoding.default, headers: nil).responseJSON { (response: DataResponse<Any>) in print(response) } What to do to solve this? Edit: Alamofire.request(url, method: .get, parameters: param, encoding: JSONEncoding.default) .responseJSON { response in switch response.result { case

numberOfRowsInSection is called before Alamofire connection

南楼画角 提交于 2019-12-01 00:08:50
I get data by Alamofire in viewDidLoad , then put it into answerArray. However, before Alamofire connection, the numberOfRowsInSection is invoked and returns 0. How can I get data by Alamofire first, then get eventArray.count at numberOfRowsInSection ? var answerArray = NSMutableArray() override func viewDidLoad() { let parameters = [ "id":questionNum ] Alamofire.request(.POST, "http://localhost:3000/api/v1/questions/question_detail",parameters: parameters, encoding: .JSON) .responseJSON { (request, response, JSON, error) in println(JSON!) // Make models from JSON data self.answerArray = (JSON

Creating a generic method with AlamoFire in Swift

和自甴很熟 提交于 2019-12-01 00:02:44
I am using AlamoFire for my API calls, and have something like this all over my project: static func login(userName: String, password: String) -> User { let parameters = ["userName": userName , "password": password] let user = User() Alamofire.request(.POST, "myserver.com/login", parameters: parameters, encoding: .JSON) .validate() .responseObject { (response: Response<User, NSError>) in switch response.result { case .Success(let value): user.valueHandle?(value) case .Failure(let error): user.errorHandle?(error) } } return user } (see https://stackoverflow.com/a/37949671/406322 on how I came

How do I use JSON arrays with Alamofire parameters?

怎甘沉沦 提交于 2019-11-30 20:54:23
I'm having a bit of trouble structuring my parameters so that our server API would be able to read it as valid JSON. Alamofire uses parameters like this in swift language let parameters : [String: AnyObject] = [ "string": str "params": HOW I INSERT A VALID JSON ARRAY HERE ] The problem is that AnyObject does not seem to accept JSON so how would I send / create a structure like this with swift? { "string": str, "params" : [ { "param1" : "something", "param2" : 1, "param3" : 2, "param" : false }, { "param1" : "something", "param2" : 1, "param3" : 2, "param" : false }] } Taken from Alamofire 's

Understanding swift Alamofire completionHandler

↘锁芯ラ 提交于 2019-11-30 20:44:38
I have these two methods in my API class to get data from an API: func authenticateUser(completionHandler: (responseObject: String?, error: NSError?) -> ()) { makeAuthenticateUserCall(completionHandler) } func makeAuthenticateUserCall(completionHandler: (responseObject: String?, error: NSError?) -> ()) { Alamofire.request(.GET, loginUrlString) .authenticate(user: "a", password: "b") .responseString { request, response, responseString, responseError in completionHandler(responseObject: responseString as String!, error: responseError) } } Then in another class i use the following code to access