Handle Login with Swift/Firebase/Microsoft - ios

Im new with Swift and Im implementing a Login with Firebase and Microsoft Azure AD. After a successful Login, the app returns to the original ViewController instead of showing the WelcomeViewController.
The flow is the following ViewController -> Microsoft Login Form -> Successful Login -> ViewController -> WelcomeViewController.
Here's my code.
class ViewController: UIViewController {
var microsoftProvider : OAuthProvider?
let kgraphURI : String! = "https://graph.microsoft.com/v1.0/me/"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func signIn(_ sender: Any) {
self.microsoftProvider = OAuthProvider(providerID: "microsoft.com")
self.microsoftProvider?.customParameters =
["tenant":"TENANT-ID"]
self.microsoftProvider?.scopes =
[
"profile"
]
self.microsoftProvider?.getCredentialWith(_: nil){
credential, error in if error != nil{
// Handle Error
}
if let credential = credential{
Auth.auth().signIn(with: credential){
(authResult, error) in if error != nil{
// Handle Error
}
guard let authResult = authResult else{
print("Couldnt get graph result")
return
}
// Get credential and token when login successfully
let microCredential = authResult.credential as! OAuthCredential
let token = microCredential.accessToken!
// Use token to call Microsoft Graph API
let welcomeViewController = self.storyboard?.instantiateViewController(withIdentifier: "WelcomeViewController") as! WelcomeViewController
self.view.window?.rootViewController = welcomeViewController
self.view.window?.makeKeyAndVisible()
}
}
}
}
}

Write this UI related code on main thread
DispatchQueue
.main.async {
let welcomeViewController = self.storyboard?.instantiateViewController(withIdentifier: "WelcomeViewController") as! WelcomeViewController
self.view.window?.rootViewController = welcomeViewController
self.view.window?.makeKeyAndVisible()
}

Related

Found nil while storyboard.instantiate

I am building a renting app with firebase. I have a screen 'WelcomeViewController'.Now the functionality of this screen is that when app starts, it checks wether the user is logged in or not. If it is, then it perform storyboard instantiate to HomeScreenViewController and if user is logged out, then it should instantiate storyboard to LoginViewController. Now, the first part is running fine and storyboard does instantiate to HomeScreenViewController but during the second part when storyboard.instantiate is run, it crashes with error "found nil while force unwrapping optional". I have crosschecked all the storyboard ID's and everything. I cant seem to figure out.
import UIKit
import Firebase
class WelcomeViewController: UIViewController {
var docRef:DocumentReference!
var Uid:String?
var homeVC:UITabBarController? = nil
override func viewDidLoad() {
super.viewDidLoad()
if Auth.auth().currentUser != nil {
//User is signed in
print("User is logged in")
docRef = Firestore.firestore().document("Users/\(Auth.auth().currentUser!.uid)")
docRef.getDocument { (docSnapshot, error) in
guard let docSnapshot = docSnapshot, docSnapshot.exists else { print("Error Founddddd");return}
let myData = docSnapshot.data()
let type = myData?["Role"] as? String ?? ""
print(type)
if type == "Owner" {
self.homeVC = self.storyboard?.instantiateViewController(identifier: "OwnerHome") as? OwnerHomeTabBarViewController
self.view.window?.rootViewController = self.homeVC
self.view.window?.makeKeyAndVisible()
}
else {
self.homeVC = self.storyboard?.instantiateViewController(identifier: "TouristHome") as? TouristHomeTabBarViewController
self.view.window?.rootViewController = self.homeVC
self.view.window?.makeKeyAndVisible()
}
}
}
else {
print("User is loggedout")
//Send User to Login/Signup Screen
let Storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let LoginVC = Storyboard.instantiateViewController(identifier: "loginScreen") as! SignInViewController
self.view.window!.rootViewController = LoginVC //Error is coming here
self.view.window?.makeKeyAndVisible()
}
}
}
I think you need to remove ! from self.view.window! and replace with ?.
Have you set storyboard ID in your Storyboard for each ViewController ?

Swift - app crashes after setting UserDefaults

I am trying to implement a "always logged in" function in to my app. The problem is that if I restart my app, it crashes. This is what I did:
set Userdefault:
#objc func loginButtonTapped(_ sender: Any) {
let email = self.emailTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let password = self.passwordTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
// start button animation
loginButton.startAnimation()
let qualityOfServiceClass = DispatchQoS.QoSClass.background
let backgorundQueue = DispatchQueue.global(qos: qualityOfServiceClass)
backgorundQueue.async {
// check if account details correct
Auth.auth().signIn(withEmail: email, password: password) { (result, error) in
if error != nil {
DispatchQueue.main.async {
// error -> stop animation
self.loginButton.stopAnimation(animationStyle: .shake, revertAfterDelay: 0) {
self.errorLabel.text = error!.localizedDescription
self.errorLabel.alpha = 1
}
}
}else {
// correct acount details -> login
DispatchQueue.main.async {
UserDefaults.standard.set(true, forKey: "isLoggedIn")
UserDefaults.standard.synchronize()
// transition to home ViewController
self.transitionToHome()
}
}
}
}
}
checking UserDefault:
class MainNavigationControllerViewController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
if isLoggedIn() {
let homeController = MainViewController()
viewControllers = [homeController]
}
}
fileprivate func isLoggedIn() -> Bool {
return UserDefaults.standard.bool(forKey: "isLoggedIn")
}
}
The user logs in via Firebase and all the data is stored in Cloud Firestore.
Error
cell.customWishlistTapCallback = {
let heroID = "wishlistImageIDX\(indexPath)"
cell.theView.heroID = heroID
let addButtonHeroID = "addWishButtonID"
self.addButton.heroID = addButtonHeroID
// track selected index
self.currentWishListIDX = indexPath.item
let vc = self.storyboard?.instantiateViewController(withIdentifier: "WishlistVC") as? WishlistViewController
vc?.wishList = self.dataSourceArray[self.currentWishListIDX]
// pass drop down options
vc?.theDropDownOptions = self.dropDownButton.dropView.dropDownOptions
vc?.theDropDownImageOptions = self.dropDownButton.dropView.dropDownListImages
// pass current wishlist index
vc?.currentWishListIDX = self.currentWishListIDX
// pass the data array
vc?.dataSourceArray = self.dataSourceArray
// set Hero ID for transition
vc?.wishlistImage.heroID = heroID
vc?.addWishButton.heroID = addButtonHeroID
// allow MainVC to recieve updated datasource array
vc?.dismissWishDelegate = self
vc?.theTableView.tableView.reloadData()
self.present(vc!, animated: true, completion: nil)
}
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
at line:
let vc = self.storyboard?.instantiateViewController(withIdentifier: "WishlistVC") as! WishlistViewController
I guess it is not as easy as I thought. Does anyone know why the app crashes and how I can solve this? :)
You are creating your MainViewController instance using a simple initialiser (MainViewController()) rather than instantiating it from the storyboard. As a result, any #IBOutlet properties will be nil since it is the the storyboard process that allocates those object instances and assigns them to the properties.
You need to add an identifier to your main view controller scene (if it doesn't already have one) and use that to instantiate the view controller instance. E.g. assuming the scene identifier is "MainScene" you would have something like:
override func viewDidLoad() {
super.viewDidLoad()
if isLoggedIn() {
let homeController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("MainScene")
viewControllers = [homeController]
}
}
The crash in your updated question indicates that either the scene with the identifier WishlistVC doesn't have its class set to WishlistViewController or it isn't found so the forced downcast crashes.

Passing FBSDK login manager result between view controllers

I can't transfer the login manager result between view controllers,
The segue is associated to the button and its identifier is s1.
My setup is correct.The program is crashing with green breakpoints.
here is my code:
for the first VC:
import FBSDKLoginKit
class ViewController: UIViewController {
var user_name: String?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let fbLoginManager = FBSDKLoginManager()
fbLoginManager.logIn(withReadPermissions: ["public_profile", "email"], from: self) { (result, error) in
if let error = error {
print("Failed to login: \(error.localizedDescription)")
return
}
guard let accessToken = FBSDKAccessToken.current() else {
print("Failed to get access token")
return
}
let credential = FIRFacebookAuthProvider.credential(withAccessToken: accessToken.tokenString)
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields" : "email, id, locale"])
.start(completionHandler: { (connection, result, error) in
guard let result = result as? NSDictionary,
let user_name = result["user_name"] as? String,
else {
return
}
if(segue.identifier == "s1"){
if let v = segue.destination as? Re {
v.uname=user_name ?? ""
//v.uname = usr.text ?? ""
}
}
})
// Perform login by calling Firebase APIs
FIRAuth.auth()?.signIn(with: credential, completion: { (user, error) in
if let error = error {
print("Login error: \(error.localizedDescription)")
let alertController = UIAlertController(title: "Login Error", message: error.localizedDescription, preferredStyle: .alert)
return
}
})
}
}
}
And for Re,the next VC:
class Re: UIViewController {
var uname: String?
#IBOutlet weak var l1: UILabel!
var userfb: String?
override func viewDidLoad() {
super.viewDidLoad()
l1.text=uname
// Do any additional setup after loading the view.
}
}
You need to use instance variable which you have declared at top as below.
Now you have create new user_name and use another user_name
guard let result = result as? NSDictionary,
user_name = result["user_name"] as? String,// make change here
else {
return
}

Swift - AWS Cognito-Facebook Login

I want to use Facebook for my login process with Cognito, and I've followed a lot of AWS documentation and look at tutorials and questions in Stackoverflow, but I've not found a solution for my problem.
When the user opens the app, it will check if the user is logged in using IdentityManager. If not, it will open a new view where the user can sign in using Facebook using Facebook SDK. After that, I stored the token with a custom IdentityProvider as the documentation said (credentialsProvider.logins is deprecated). Everything seems to work fine, but every time I reopen the application, my session isn't restored.
I found out that if I use AWSIdentityManager.defaultIdentityManager().resumeSessionWithCompletionHandler(handler)I restored my session, but in case the user isn't logged in, it doesn't show my custom login screen as expected, but a Safari web view to Facebook.
Here is my code:
AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let identityManager = AWSIdentityManager.defaultIdentityManager()
identityManager.resumeSessionWithCompletionHandler({
(result, error) -> Void in
if !identityManager.loggedIn {
let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier("SignIn") as UIViewController
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
}
})
return AWSMobileClient.sharedInstance.didFinishLaunching(application, withOptions: launchOptions)
}
SignInViewController.swift
#IBAction func openFacebookLoginScreen(sender: AnyObject) {
FBSDKLoginManager().logInWithReadPermissions(FACEBOOK_PERMISSIONS, fromViewController: self, handler: { (result, error) -> Void in
if error == nil {
let fbLoginResult : FBSDKLoginManagerLoginResult = result
if fbLoginResult.isCancelled {
print("Cancelled")
}
else {
if FBSDKAccessToken.currentAccessToken() != nil {
self.signInFacebook(FBSDKAccessToken.currentAccessToken().tokenString)
self.dismissSignInView()
}
}
}
})
}
func signInFacebook(fbToken: String){
let logins = [AWSIdentityProviderFacebook : fbToken]
let customProviderManager = CustomIdentityProvider(tokens: logins)
let credentialsProvider = AWSCognitoCredentialsProvider(
regionType:.USEast1,
identityPoolId: COGNITO_IDENTITY_POOL_ID,
identityProviderManager: customProviderManager)
let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider: credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
}
class CustomIdentityProvider: NSObject, AWSIdentityProviderManager {
var tokens : [NSString : NSString]?
init(tokens: [NSString : NSString]) {
self.tokens = tokens
}
#objc func logins() -> AWSTask {
return AWSTask(result: tokens)
}
}
Apparently my problem was that I was calling IdentityManager.loggedIn inside AppDelegate.swift, so I move it to viewDidLoad() on my main view controller.
Also I changed my sign in code to:
#IBAction func openFacebookLoginScreen(_: AnyObject) {
handleLoginWithSignInProvider(AWSFacebookSignInProvider.sharedInstance())
}
func handleLoginWithSignInProvider(signProvider: AWSSignInProvider){
AWSIdentityManager.defaultIdentityManager().loginWithSignInProvider(signProvider) { (result, error) in
if(error == nil){
let logins = [AWSIdentityProviderFacebook : FBSDKAccessToken.currentAccessToken().tokenString!]
let customProviderManager = CustomIdentityProvider(tokens: logins)
let credentialsProvider = AWSCognitoCredentialsProvider(
regionType:.USEast1,
identityPoolId: self.COGNITO_IDENTITY_POOL_ID,
identityProviderManager: customProviderManager)
let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider: credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
self.dismissSignInView()
}
}
}

Forceful login in Facebook iOS SDK

I have an iPad app and its used in a hotel for guests. So multiple users use the app through Facebook login and after the usage I need to logout the user from the app. So the next user will have the login screen again. I'm doing this in logout process and when I call the login function Its not giving the login screen. Instead it give me the already authorized screen. (With ok and cancel button). Please help to resolve this issue.
To login:
FBSDKLoginManager().logInWithReadPermissions(["email", "public_profile"], fromViewController: self, handler: { (result, error) -> Void in
if error != nil {
print("error : \(error.localizedDescription)")
} else if result.isCancelled {
print("user cancelled")
} else {
print("success")
}
})
To Logout :
FBSDKLoginManager().logOut()
FBSDKAccessToken.setCurrentAccessToken(nil)
FBSDKProfile.setCurrentProfile(nil)
let storage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
for cookie in storage.cookies! {
storage.deleteCookie(cookie)
}
NSUserDefaults.standardUserDefaults().synchronize()
It will always give you an already authorized screen because Facebook is not responsible to logout from the Safari browser.
You are logged out from the login manager it is fine.
For this situation you can use the loginBehavior property of FBSDKLoginManager
You have to set the behavior to the Web it will open popup to login.
let fbManager : FBSDKLoginManager = FBSDKLoginManager()
fbManager.loginBehavior = FBSDKLoginBehavior.Web
fbManager.logInWithReadPermissions(["email"], fromViewController: self) { (result, error) -> Void in
if error != nil {
print(error)
}
else {
print(result)
}
}
You can do the logout from the manager as per your need.
Hope it will help you.
clear your facebook token from ios like this... this make user to login every time in app
ACAccountStore *store = [[ACAccountStore alloc] init];
NSArray *fbAccounts = [store accountsWithAccountType:[store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]];
for (ACAccount *fb in fbAccounts) {
[store renewCredentialsForAccount:fb completion:^(ACAccountCredentialRenewResult renewResult, NSError *error) {
}];
}
This works for me
For objective-C
1st Define
#property(nonatomic,strong)FBSDKLoginManager *login;
Then Use this method.
-(void)logoutFromFacebook{
if(self.login){
[self.login logOut];
}else{
self.login = [[FBSDKLoginManager alloc] init];
[self.login logOut];
}
}
For Swift
var login: FBSDKLoginManager?
func FBSDKLoginManager () {
if self.login != nil {
self.login.logout()
}else {
self.login = self.login()
self.login.logout()
}
}
Hope this will help.
Here is a working solution using their custom button class FBSDKLoginButton on a view. You should only enter the protected page on view will appear if the currentAccessToken is not nil. This will prevent showing the first view for a few miliseconds. As you can see, I embeded my viewControllers in a NaviguationController.
UIView with custom class in the login view :
ViewWillAppear :
override func viewWillAppear(animated: Bool) {
super.viewDidAppear(animated)
if (FBSDKAccessToken.currentAccessToken() != nil) {
loginFacebook(FBSDKAccessToken.currentAccessToken().tokenString,
userId: FBSDKAccessToken.currentAccessToken().userID)
self.enterProtectedPage();
}
}
ViewDidLoad :
override func viewDidLoad() {
super.viewDidLoad()
loginBtn.delegate = self
loginBtn.readPermissions = ["public_profile", "email", "user_birthday", "user_relationship_details"];
}
login button click action :
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
if (error != nil) {
print(error.localizedDescription)
return
}
if let _ = result.token {
loginFacebook(FBSDKAccessToken.currentAccessToken().tokenString,
userId: FBSDKAccessToken.currentAccessToken().userID)
self.enterProtectedPage();
}
}
enter protected page :
func enterProtectedPage() {
let protectedPage = self.storyboard?.instantiateViewControllerWithIdentifier("ProtectedPageViewController") as!
ProtectedPageViewController
let protectedPageNav = UINavigationController(rootViewController: protectedPage)
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController = protectedPageNav
}
logout action on the protected page :
#IBAction func btnLogoutClicked(sender: AnyObject) {
let loginManager = FBSDKLoginManager()
loginManager.logOut()
let loginPage = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController") as!ViewController
let loginPageNav = UINavigationController(rootViewController: loginPage)
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController = loginPageNav
}

Resources