alamofire

swift JSON login REST with post and get response example

你离开我真会死。 提交于 2019-11-28 18:29:57
It's my first experience with REST in iOS development with swift. I couldn't find any working or straight (simple) example for doing what i need here. I have a login backend ( https://myaddress.com/rest/login ), where I need to pass 2 params: login and password. When I pass good values (user exists in database) I get 2 variables as a result: token (string) and firstLogin (bool). So when I get those values I know that login is successful and I can log in into my app. So I am begging you for an example (just a simple function) of how to achieve that. If I get working code example I will know how

Alamofire network calls not being run in background thread

↘锁芯ラ 提交于 2019-11-28 18:24:26
It is my understanding that by default, Alamofire requests run in a background thread. When I tried running this code: let productsEndPoint: String = "http://api.test.com/Products?username=testuser" Alamofire.request(productsEndPoint, method: .get) .responseJSON { response in // check for errors guard response.result.error == nil else { // got an error in getting the data, need to handle it print("Inside error guard") print(response.result.error!) return } // make sure we got some JSON since that's what we expect guard let json = response.result.value as? [String: Any] else { print("didn't get

Swift 3 Alamofire multipart upload

左心房为你撑大大i 提交于 2019-11-28 18:09:12
Thanks to migration to Swift 3, I find it difficult to compile my project that uses Alamofire. The problem occurs when uploading multipartFormData: Alamofire.upload(.POST, URL, headers: headers, multipartFormData: { multipartFormData in . . . }) Ambiguous reference to member 'upload(_:to:method:headers:)' Any help much appreciated, thanks in advance! RESOLVED: Alamofire.upload(multipartFormData: { (multipartFormData) in multipartFormData.append(fileData, withName: "file_pack", fileName: "file_pack", mimeType: "text/plain") for (key, value) in self.parameters { multipartFormData.append(value

How to use Alamofire with custom headers for POST request

二次信任 提交于 2019-11-28 17:51:46
I implemented a POST request with Alamofire with a custom header, because we work with OAuth2 and we have to send an access token in every request inside the header. In this case I have to use a custom header. The access token value for the HTTP header field Authorization does not work for me. The server generates an error because the header information for OAuth with the access token is not available. But what is the mistake in my code? Here is my current code: let URL = NSURL(string: url + "/server/rest/action") var mutableURLRequest = NSMutableURLRequest(URL: URL!) mutableURLRequest

Unit Testing HTTP traffic in Alamofire app

限于喜欢 提交于 2019-11-28 16:42:08
I'm struggling a bit to figure out how to best test an app that uses Alamofire to help sync with server data. I want to be able to test my code that uses Alamofire and processes JSON responses from a server. I'd like to mock those tests so that I can feed the expected response data to those tests without incurring real network traffic. This blog post ( http://nshipster.com/xctestcase/ ) describes how easy it is to Mock an object in Swift - but I'm not sure how to do that with Alamofire and its chained responses. Would I mock the Manager? the Request? Response? Any help would be appreciated!

Best way to Cache JSON from API in SWIFT?

限于喜欢 提交于 2019-11-28 16:33:35
问题 I need to cache json data from API in swift. So I researched a Lot & get to this Post. I tried to implement the Option 1 in my App. But the Custom manager always returned nil. I don't know why? After that I got AwesomeCache. It says that it an do Awesome API Caching. But I don't know how to implement this? I referred this Issue. Still I can't figure it Out. This is how my Current implementation Looks without Cache: Alamofire.request(.GET, "http://api.androidhive.info/volley/person_array.json"

Alamofire 关于Response的思考

偶尔善良 提交于 2019-11-28 16:09:06
Alamofire 关于Response的思考。 你好,我是Emma,今天我们开启对Alamofire 关于Response的思考。主要的思考方向以下方示例为切入点。 示例: SessionManager.default.request(urlString) //默认的请求 .response(completionHandler: { (dataResponse) in dataResponse.response }) //自定义序列化的请求 .response(responseSerializer: DataResponseSerializer<String>.init(serializeResponse: { (request, response, data, error) -> Result<String> in print("原始数据:\(response)") return .success("成功啦") })) { (dataResponse) in print(dataResponse) } //系统封装过的序列化请求 .responseJSON { (jsonResponse) in print(jsonResponse) } ###1.Alamofiren中Response这个角色的作用是什么? 首先在查看源码之前,通过上面的代码我们思考一下

Pagination with UICollectionView working with API

让人想犯罪 __ 提交于 2019-11-28 14:50:40
I have a working application where I am using Alamofire and SwiftyJSON to fetch images from flicr API. Now, I want to include pagination in the returned images. I am returning 5 images when the application launches and this number can increase. I want to achieve this with the UICollectionView . My codes are written below. Any help would be appreciated. 100 items are being returned on every page. Services func setData(json: JSON) -> Void { if let photos = json["photos"]["photo"].array { for item in photos { let photoID = item["id"].stringValue let farm = item["farm"].stringValue let server =

How to call post service with parameters swift 3.0 Alamofire 4.0?

我只是一个虾纸丫 提交于 2019-11-28 14:45:45
I have parameters in var Param : [String:String] = [:] I am using Alamofire.upload( multipartFormData: { multipartFormData in but in response it says invalid service is their in parameter encoding it goes wrong ? multipartFormData is used to upload images on server. So if you want to upload image data then try to use below code. let payload: [String: AnyObject] = [ "key1": "val1" as AnyObject, "key2": true as AnyObject, "key3" : [1,2,3,4] as AnyObject ] let requestString = "http://www.url.com/api/action" Alamofire.upload(multipartFormData: { (multipartFormData) in multipartFormData.append

Swift accessing response from function

送分小仙女□ 提交于 2019-11-28 14:14:10
问题 I am producing a GET request, which gives me JSON object of array within dictionaries in this format: Array<Dictionary<String,String>> I have a class: class foodMenu: UITableViewController{ var jsonData:Array<Dictionary<String,String>>! // Here is set an empty global variable(Not sure if I am doing this right either) func getFoodRequest(){ Alamofire.request("http://127.0.0.1:5000/get_food").responseJSON { response in print("This response", response.result) let result = response.result.value