Skip to content

Instantly share code, notes, and snippets.

@fabdarice
Created September 12, 2015 08:24
Show Gist options
  • Save fabdarice/f1fbeec18db8809d026c to your computer and use it in GitHub Desktop.
Save fabdarice/f1fbeec18db8809d026c to your computer and use it in GitHub Desktop.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var loginViewController:LoginVC = mainStoryboard.instantiateViewControllerWithIdentifier("loginVC") as! LoginVC
var timelineViewController:TimelineVC = mainStoryboard.instantiateViewControllerWithIdentifier("timeline") as! TimelineVC
let login = KeychainInfo.login
let auth_token = KeychainInfo.auth_token
var launchScreenVC = UIViewController(nibName: "LoadingLaunchScreen", bundle: nil)
self.window?.rootViewController = launchScreenVC
if (login != nil && auth_token != nil) {
let parameters:[String: AnyObject] = [
"login": login!,
"token": auth_token!,
"timezone": NSTimeZone.localTimeZone().name
]
// Check if the login and auth_token are valid for the user
request(.POST, ApiLink.sign_in, parameters: parameters, encoding: .JSON)
.responseJSON { (_, _, mydata, _) in
if (mydata != nil) {
let json = JSON(mydata!)
if (json["success"].intValue == 1) {
// Activate the Background Fetch Mode to Interval Minimum
UIApplication.sharedApplication().setMinimumBackgroundFetchInterval(
UIApplicationBackgroundFetchIntervalMinimum)
self.window?.rootViewController = timelineViewController
} else {
KeychainInfo.login = nil
KeychainInfo.auth_token = nil
self.window?.rootViewController = loginViewController
}
} else {
self.window?.rootViewController = loginViewController
}
}
} else {
// GOES HERE SOMETIMES WHY??
self.window?.rootViewController = loginViewController
}
self.window?.makeKeyAndVisible()
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
let parameters:[String: AnyObject] = [
"login": self.loginTextField.text,
"password": self.passwordTextField.text,
"timezone": NSTimeZone.localTimeZone().name
]
request(.POST, ApiLink.sign_in, parameters: parameters, encoding: .JSON)
.responseJSON{ (_, _, mydata, _) in
//convert to SwiftJSON
let json = JSON(mydata!)
if (json["success"].intValue == 0) {
// ERROR RESPONSE FROM HTTP Request
GlobalFunctions().displayAlert(title: NSLocalizedString("Authentication_failed", comment: "Authentication Failed"), message: json["message"].stringValue, controller: self)
} else {
// SUCCESS RESPONSE FROM HTTP Request
let login:String! = json["login"].string
let auth_token:String! = json["auth_token"].string
// Save login and auth_token to the iOS Keychain
KeychainInfo.login = login
KeychainInfo.auth_token = auth_token
// Activate the Background Fetch Mode to Interval Minimum
UIApplication.sharedApplication().setMinimumBackgroundFetchInterval(
UIApplicationBackgroundFetchIntervalMinimum)
// Goes to TimelineViewController
self.performSegueWithIdentifier("homeSegue", sender: self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment