TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Apr 2017 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✗ |
Maintained by thienpham234.
Depends on: | |
ReachabilitySwift | ~> 3 |
PKHUD | ~> 4.2 |
Alamofire | ~> 4.4 |
AlamofireNetworkActivityIndicator | ~> 2.1 |
SwiftyJSON | ~> 3.1 |
CryptoSwift | ~> 0.6 |
Using simple
import T_AlamofireCustom
ServerManager._makeGETrequest(baseURL: "https://example.com",
endpoint: "/get/lits",
params: nil) { (success, status,response, dict, array, string, error) in
// Handle API
}
Handling the Response
of a Request
made in TAlamofireCustom involves chaining a response handler onto the Request
.
ServerManager._makeGETrequest(baseURL: "https://example.com",
endpoint: "/get/lits",
params: nil) { (success, status,response, dict, array, string, error) in
// Handle API
print(success) // success == true => Response Success
print(status) // Reponse status code of a request. (Notice: status == -1 ==> No Connect Internet)
print(response) // Response Data JSON
print(dict) // Parsed json to (Dictionary) (If response json is Dictionary)
print(array) // Parsed json to (Array) (If response json is Array)
print(string) // Parsed json to (String) (If response json is String)
print(error) // error: NSError
}
public enum eHTTPMethod: String {
case GET
case POST
case PUT
case DELETE
}
public enum eContentTypeHTTPHeader: Int {
case JSON
case FormURLEncoded
}
let parameters: Parameter = ["foo": "bar"]
// All three of these calls are equivalent
ServerManager._makeGETrequest(baseURL: "https://httpbin.org",
endpoint: "/get",
params: parameters) { (success, status,response, dict, array, string, error) in
// Handle API
}
OR
// showHub: default is True
// showError: default is True
ServerManager._makeGETrequest(baseURL:"https://httpbin.org" ,
endpoint: "/get",
params: "paramters",
showError: false,
showHud: true) { (success, status,response, dict, array, string, error) in
//
}
// https://httpbin.org/get?foo=bar
//
ServerManager._makeGETrequest(baseURL: "",
endpoint: "",
params: nil,
username:"",
password: "",
accessToken: "",
showError: false,
showHud: false) { (success, status,response, dict, array, string, error) in
//
}
let body: Parameter = [
"foo": [1,2,3],
"bar": [
"baz": "qux"
]
]
ServerManager._makePOSTrequest(baseURL: "https://httpbin.org",
endpoint: "/post",
params: nil,
body: body) { (success, status,response, dict, array, string, error) in
//Handel response
})
// HTTP body: {"foo": [1, 2, 3], "bar": {"baz": "qux"}}
let body: Parameter = [
"foo": "bar",
"baz": ["a", 1],
"qux": [
"x": 1,
"y": 2,
"z": 3
]
]
ServerManager._makePOSTrequest(baseURL: "https://httpbin.org",
endpoint: "/post",
params: nil,
body: body,
username:nil,
password: nil,
accessToken: nil,
contentType: .FormURLEncoded,
showError: false,
showHud: false) { (success, status,response, dict, array, string, error) in
//Handel response
})
// HTTP body: foo=bar&baz[]=a&baz[]=1&qux[x]=1&qux[y]=2&qux[z]=3
let parameters: Parameter = [
"foo": [1,2,3],
"bar": [
"baz": "qux"
]
]
ServerManager._makePOSTrequest(baseURL: "https://httpbin.org",
endpoint: "/post",
params: nil,
body: body,
username:nil,
password: nil,
accessToken: nil,
contentType: .JSON,
showError: false,
showHud: false) { (success, status,response, dict, array, string, error) in
//Handel response
})
// HTTP body: {"foo": [1, 2, 3], "bar": {"baz": "qux"}}
if (ServerManager._noConnection() == true){
// connection to internet is unavailable
}else{
//connection to internet is available
}
With the addition of App Transport Security (ATS) in iOS 9, it is possible that using a custom ServerTrustPolicyManager
with several ServerTrustPolicy
objects will have no effect. If you continuously see CFNetwork SSLHandshake failed (-9806)
errors, you have probably run into this problem. Apple's ATS system overrides the entire challenge system unless you configure the ATS settings in your app's plist to disable enough of it to allow your app to evaluate the server trust.
If you run into this problem (high probability with self-signed certificates), you can work around this issue by adding the following to your Info.plist
.
<dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>example.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
<!-- Optional: Specify minimum TLS version -->
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
</dict>
</dict>
</dict>
</dict>
Whether you need to set the NSExceptionRequiresForwardSecrecy
to NO
depends on whether your TLS connection is using an allowed cipher suite. In certain cases, it will need to be set to NO
. The NSExceptionAllowsInsecureHTTPLoads
MUST be set to YES
in order to allow the SessionDelegate
to receive challenge callbacks. Once the challenge callbacks are being called, the ServerTrustPolicyManager
will take over the server trust evaluation. You may also need to specify the NSTemporaryExceptionMinimumTLSVersion
if you're trying to connect to a host that only supports TLS versions less than 1.2
.
It is recommended to always use valid certificates in production environments.
Librarys using in TAlamofireCustom.
thienpham234, [email protected]
TAlamofireCustom is available under the MIT license. See the LICENSE file for more info.