Last active
June 30, 2018 00:07
-
-
Save carlosalexandresmo/0540d3f06fa860d4818aa4e452bf7ae6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
enum UserDefaultsKeys : String { | |
case isLoggedIn | |
case userID | |
} | |
//Save in UserDefaults where you want | |
UserDefaults.standard.setLoggedIn(value: true) // Bool | |
UserDefaults.standard.setUserID(value: result.User.id!) // integer | |
//Retrieve data anywhere in app | |
print("ID : \(UserDefaults.standard.getUserID())") | |
UserDefaults.standard.getUserID() | |
//Remove Values | |
UserDefaults.standard.removeObject(forKey: UserDefaultsKeys.userID) |
This file contains hidden or 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
let url = URL(string: "http://www.thisismylink.com/postName.php")! | |
var request = URLRequest(url: url) | |
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") | |
request.httpMethod = "POST" | |
let postString = "id=13&name=Jack" | |
request.httpBody = postString.data(using: .utf8) | |
let task = URLSession.shared.dataTask(with: request) { data, response, error in | |
guard let data = data, error == nil else { // check for fundamental networking error | |
print("error=\(error)") | |
return | |
} | |
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors | |
print("statusCode should be 200, but is \(httpStatus.statusCode)") | |
print("response = \(response)") | |
} | |
let responseString = String(data: data, encoding: .utf8) | |
print("responseString = \(responseString)") | |
} | |
task.resume() |
This file contains hidden or 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
extension UserDefaults{ | |
//MARK: Check Login | |
func setLoggedIn(value: Bool) { | |
set(value, forKey: UserDefaultsKeys.isLoggedIn.rawValue) | |
//synchronize() | |
} | |
func isLoggedIn()-> Bool { | |
return bool(forKey: UserDefaultsKeys.isLoggedIn.rawValue) | |
} | |
//MARK: Save User Data | |
func setUserID(value: Int){ | |
set(value, forKey: UserDefaultsKeys.userID.rawValue) | |
//synchronize() | |
} | |
//MARK: Retrieve User Data | |
func getUserID() -> Int{ | |
return integer(forKey: UserDefaultsKeys.userID.rawValue) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment