Last active
August 29, 2015 14:12
-
-
Save mergesort/1581b738fba5d7b8f895 to your computer and use it in GitHub Desktop.
Don't forget to force unwrap optionals when putting them into dictionaries which will be used not on iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func unauthenticatedUserSessionConfiguration() -> NSURLSessionConfiguration { | |
let base64Credentials = ("\(clientID):\(clientSecret)" as NSString).dataUsingEncoding(NSUTF8StringEncoding) | |
let encoded = base64Credentials?.base64EncodedDataWithOptions(NSDataBase64EncodingOptions(rawValue: 0)) | |
var headers = Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders ?? [:] | |
headers["X-2FA-Support"] = "default" | |
// This is the key line. Note that it's an optional string. | |
headers["Authorization"] = "Basic \(NSString(data: encoded!, encoding: NSUTF8StringEncoding))" | |
// The value is: "Basic Optional(MzA5MWY1234mU1234kYTJkOTM212I3ZWI4YzE2NACkMDY4YmEw88E3ZTo3ZD1234IxYzE481234DM5JKdkNTZLMAO0MTNkMmEyNDJhYzIxNGIz)" | |
// And your request will fail. | |
// But if we force unwrap the optional as such. | |
headers["Authorization"] = "Basic \(NSString(data: encoded!, encoding: NSUTF8StringEncoding)!)" | |
// The value is: "Basic MzA5MWY1234mU1234kYTJkOTM212I3ZWI4YzE2NACkMDY4YmEw88E3ZTo3ZD1234IxYzE481234DM5JKdkNTZLMAO0MTNkMmEyNDJhYzIxNGIz" | |
// And your request will succeed! | |
let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration() | |
configuration.HTTPAdditionalHeaders = headers | |
return configuration | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment