Alamofire Https Change in 10.3

How Can I Access Local Https with invalid certificate With Alamofire?

I finally sorted out my problems. There were two problems.

1. I needed to declare sessionManager outside the function.

let manager = SessionManager(configuration: URLSessionConfiguration.default, serverTrustPolicyManager: ServerTrustPolicyManager(policies:["localhost":.disableEvaluation]))

2. I read somewhere that you need to include port number for the host, but that actually caused the problem for me.

It seems like you just need to write "localhost" Instead of "localhost:443".

Regardless, here is the entire code that will disable evaluation for all addresses.

You don't need to change anything to Info.plist. The default Info.plist that Xcode gives you seem to work.

IOS 10.3.2, Xcode 8.3.2, Alamofire Master branch from Github (2017-05-23)

import UIKit
import Alamofire

class ViewController: UIViewController {

open class MyServerTrustPolicyManager: ServerTrustPolicyManager {
open override func serverTrustPolicy(forHost host: String) -> ServerTrustPolicy? {
return ServerTrustPolicy.disableEvaluation
}
}

let sessionManager = SessionManager(delegate:SessionDelegate(), serverTrustPolicyManager:MyServerTrustPolicyManager(policies: [:]))

func connect() {
let user = "user"
let password = "password"
let parameters:Parameters = ["parameter1":"value1", "parameter2":"value2"]
sessionManager.request("https://localhost:443/index.php", parameters:parameters).authenticate(user:user, password:password).responseString {
response in
debugPrint(response.result.value)
} else {
debugPrint(response)
}
}
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
connect()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

Alamofire 4.5.1 fails after updating to iOS 11.1

I have solved it by removing the additional parameters from:

Alamofire.download("https://s3-eu-west-1.amazonaws.com/xxx/xxx/xxx.sqlite3", method: .get, parameters: parameters, encoding: JSONEncoding.default, to: destination)

To:

Alamofire.download("https://s3-eu-west-1.amazonaws.com/xxx/xxx/xxx.sqlite3",  to: destination)

I'm not sure what's wrong but that's how it works

Send get request with alamofire in swift

You use URLEncoding.default the data will be added in the URL, to create a JSON with the parameters you set you have to use the option JSONEncoding.default.

Difficulty Parsing JSON With Alamofire

Need I post my response word for word from the last time you posted this question?

You can parse your result following the printout of your JSON
response:

guard let JSON = response.result.value as? [String:Any],
let weather = JSON["weather"] as? [[String:Any]] else {
print("Could not parse weather values")
return
}

for element in weather {
if let description = element["description"] as? String {
print(description)
}
}

If you wanted to save the result, or do something based on a certain
result as you described, you could insert something else instead of
just print(description) like:

if description == "sunny" {
//do something
}

Let me know if this makes sense. Keep in mind that ( and ) in the
Xcode console means "Array".

I really ought to be flagging this as a duplicate of Save Alamofire Result as Variable?

Edit: Based on this JSON snippet - http://pastebin.com/XiGhNA26 - it should be easy to parse out the desired information with:

guard let JSON = response.result.value as? [String:Any],
let data = JSON["data"] as? [String:Any] else
{
print("Could not parse weather values")
return
}

The difference is that in this JSON response, the "data" parameter is not an array of dictionaries, it is just a dictionary itself.

Cannot install Alamofire in new Xcode Project. No Such module Alamofire

Make sure you haven't added any files from Alamofire to your project except for the Alamofire.xcodeproj

Here is step by step instruction:

  1. Download and unarchive Alamofire
  2. Copy the root folder of Alamofire to any subfolder of your project. Libs, for example.
  3. Drag and drop Alamofire.xcodeproj to your Xcode project
  4. Open project settings of your project, Build Phases pane, expand Target Dependencies section, and add Alamofire as new dependency
  5. Open General pane, expand Embedded Binaries section, and add Alamofire.framework
  6. import Alamofire // in your source file
  7. Alamofire.request(.GET, "http://httpbin.org/get") // use Alamofire


Related Topics



Leave a reply



Submit