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
justprint(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:
- Download and unarchive Alamofire
- Copy the root folder of Alamofire to any subfolder of your project. Libs, for example.
- Drag and drop Alamofire.xcodeproj to your Xcode project
- Open project settings of your project, Build Phases pane, expand Target Dependencies section, and add Alamofire as new dependency
- Open General pane, expand Embedded Binaries section, and add Alamofire.framework
import Alamofire
// in your source fileAlamofire.request(.GET, "http://httpbin.org/get")
// use Alamofire
Related Topics
Difference When Declaring Swift Protocol Using Inheritance from Another Protocol or Using Where Self
Does Cocoa Connection Binding to Nstoolbaritem Prevent Deinitializing
Changing Value of Character Using Ascii Value in Swift
Borders Not Covering Background
How to Completely Remove All Xcode Program and Cache Files
Why Is My Libraries Not Able to Expand on The Cocoapods and Shows as Objective-C Not Swift
What's The Difference Between [String] VS. [(String)]
Get Output Frames Failed, State 8196
Swift Unable to Locate and Read Property List (.Plist) File
Error Loading Media Sources in Mlmedialibrary
Save/Copy a File from Bundle to Desktop Using Nssavepanel
Navigationlink Inside .Searchable Does Not Work
Swift 3 Google Map Add Markers on Touch
Access Id Does Not Work When Testing a Textfield with 'Issecuretextentry = True'