Home > AI > IOS >

login with Facebook

Benefit: 1) If the user set email, you can skip the step to validate the email. 2) You can have facebook analytics about login with facebook.

Disadvantage: if the user doesn’t have an email (they have phone number but facebook doesn’t allow you to access that), you have no information of user identity.

Steps:

  1. create a Facebook developer account. If you have a facebook account, just come to the developer panel (https://developers.facebook.com/).
  2. create an App that you would have an App ID and App name.
  3. In the panel, come to the Products tab and choose login. Then, follow the step.
import SwiftUI
import FBSDKLoginKit
import FBSDKCoreKit


@main
struct KidsLearnHindiApp: App {

    var body: some Scene {
        WindowGroup {
            HomeView()
                .environmentObject(HomeGlobal())
                .firstLaunchViewModifier()
                .onOpenURL { (url) in
                    ApplicationDelegate.shared.application(UIApplication.shared,
                                                           open: url,
                                                           sourceApplication: nil,
                                                           annotation: UIApplication.OpenURLOptionsKey.annotation)
                
                }
        }
    }
}



struct HomeView: View {
    @AppStorage("userIsFbLogged") var userIsFbLogged: Bool = false
    @AppStorage("userFbEmail") var userFbEmail: String = ""
    
    @State var fbManager = LoginManager()
    
    
    var body: some View {
        Button {
            if userIsFbLogged {
                // log out
                
                fbManager.logOut()
                userIsFbLogged = false
                userFbEmail = ""
            } else {
                
                fbManager.logIn(permissions: ["public_profile", "email"], from: nil) { (result, error) in
                    if error != nil {
                        
                    }
                    if !result!.isCancelled {
                        ApplicationDelegate.initializeSDK(nil)
                        
                        userIsFbLogged = true
                        let request = GraphRequest(graphPath: "me", parameters: ["fields": "email"])
                        
                        request.start { (connection, res, _) in
                            guard let profileData = res as? [String: Any] else { return }
                            print("[shark]", profileData)
                            
                        }
                    }
    
                    
                }
            }
        } label: {
            VStack {
                Text(userIsFbLogged ? "Log out" : "Log in")
                Text(userFbEmail)
            }
            
        }

    }
}

Setting the info.plist

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fbapi</string>
    <string>fbapi20130214</string>
    <string>fbapi20130410</string>
    <string>fbapi20130702</string>
    <string>fbapi20131010</string>
    <string>fbapi20131219</string>
    <string>fbapi20140410</string>
    <string>fbapi20140116</string>
    <string>fbapi20150313</string>
    <string>fbapi20150629</string>
    <string>fbapi20160328</string>
    <string>fbauth</string>
    <string>fb-messenger-share-api</string>
    <string>fbauth2</string>
        <string>fbshareextension</string>
	</array>
    
    
    <key>CFBundleURLTypes</key>
    <array>
      <dict>
      <key>CFBundleURLSchemes</key>
      <array>
        <string>fb435227704194738</string>
      </array>
      </dict>
    </array>
    <key>FacebookAppID</key>
    <string>435227704194738</string>
    <key>FacebookDisplayName</key>
    <string>KidsLearnHindi</string>

Leave a Reply