2017-03-02 17 views
5

मैं एक नया आईओएस स्विफ्ट एप्लिकेशन शुरू कर रहा हूं और फायरबेसयूआई एथ का उपयोग करना चाहता हूं। यहां दस्तावेज़ों का लिंक दिया गया है जहां यह Drop-in authentication solutionFirebase Auth के तहत इसके बारे में बात करता है। एंड्रॉइड के लिए फायरबेस यूआई ऑथ बहुत आसान और आसान था। ऐसा लगता है कि आईओएस उदाहरण पुराने हैं क्योंकि एपीआई संस्करणों के बीच काफी बदल गया है। ऐसा लगता है कि वे संस्करण 3.1 पर हैं।फायरबेस यूआई ऑथ प्रदाता आईओएस स्विफ्ट उदाहरण

दिशाओं भी एक छोटे से नंगे हैं: https://github.com/firebase/FirebaseUI-iOS

कोई मेरी मदद कर सकते कृपया बाहर और फेसबुक के लिए एक उदाहरण AppDelegate और ViewController प्रदान करते हैं और गूगल के लिए लॉग इन?

मैं Xcode 8.3 उपयोग कर रहा हूँ, स्विफ्ट 3.

Podfile:

# Uncomment the next line to define a global platform for your project 
platform :ios, '9.0' 

target 'Project' do 
    # Comment the next line if you're not using Swift and don't want to use dynamic frameworks 
    use_frameworks! 

    pod 'FirebaseUI', '~> 3.1' 
    pod 'Firebase/Core' 
    pod 'Firebase/Database' 
    pod 'Firebase/Crash' 
    pod 'Firebase/Auth' 
    pod 'Firebase/Storage' 
    pod 'GoogleSignIn' 
    pod 'FBSDKLoginKit' 


    target 'ProjectTests' do 
    inherit! :search_paths 
    # Pods for testing 
    end 

    target 'ProjectUITests' do 
    inherit! :search_paths 
    # Pods for testing 
    end 

end 

यहाँ मेरी AppDelegate

import UIKit 
import CoreData 
import Firebase 
import FirebaseAuthUI 
import FirebaseAuth 
import GoogleSignIn 
import FBSDKLoginKit 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    // Override point for customization after application launch. 
    FIRApp.configure() 
    return true 
} 

func applicationWillResignActive(_ application: UIApplication) { 
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 
} 

func applicationDidEnterBackground(_ application: UIApplication) { 
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
} 

func applicationWillEnterForeground(_ application: UIApplication) { 
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 
} 

func applicationDidBecomeActive(_ application: UIApplication) { 
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
} 

} 

यहाँ है मेरी ViewController

import UIKit 
import Firebase 
import FirebaseAuth 
import FirebaseAuthUI 
import FirebaseDatabaseUI 
import FirebaseGoogleAuthUI 
import FirebaseFacebookAuthUI 
import FBSDKCoreKit 
import FBSDKLoginKit 

class ViewController: UIViewController, FUIAuthDelegate { 

    var kFacebookAppID = "111111111111111" 
override func viewDidLoad() { 
    super.viewDidLoad() 

    //FIRApp.configure() 
    checkLoggedIn() 
} 

func checkLoggedIn() { 
    FIRAuth.auth()?.addStateDidChangeListener { auth, user in 
     if user != nil { 
      // User is signed in. 
     } else { 
      // No user is signed in. 
      self.login() 
     } 
    } 
} 

func login() { 
    let authUI = FUIAuth.defaultAuthUI() 
    let facebookProvider = FUIGoogleAuth() 
    let googleProvider = FUIFacebookAuth() 
    authUI?.delegate = self 
    authUI?.providers = [googleProvider, facebookProvider] 
    let authViewController = authUI?.authViewController() 
    self.present(authViewController!, animated: true, completion: nil) 
} 

@IBAction func logoutUser(_ sender: AnyObject) { 
    try! FIRAuth.auth()!.signOut() 
} 

func authUI(_ authUI: FUIAuth, didSignInWith user: FIRUser?, error: Error?) { 
    if error != nil { 
     //Problem signing in 
     login() 
    }else { 
     //User is in! Here is where we code after signing in 

    } 
} 
} 

उत्तर

0

आपका कोड है लगता है फाई ne! Google/Facebook/Twitter Auth के साथ संवाद करने के लिए, आपको अपने ऐप में URL Scheme के लिए समर्थन जोड़ना होगा। CHECK THIS OUT!

func configureAuth() { 
    // TODO: configure firebase authentication 
    let provider: [FUIAuthProvider] = [FUIGoogleAuth(), FUIFaceBookAuth()] 
    FUIAuth.defaultAuthUI()?.providers = provider 

    // listen for changes in the authorization state 
    _authHandle = FIRAuth.auth()?.addStateDidChangeListener { (auth: FIRAuth, user: FIRUser?) in 

     // check if there is a current user 
     if let activeUser = user { 
      // check if current app user is the current FIRUser 
      if self.user != activeUser { 
       // sign in 
      } 
     } else { 
      // user must sign in 
      self.loginSession() 
     } 
    } 


} 

func loginSession() { 
    let authViewController = FUIAuth.defaultAuthUI()!.authViewController() 
    self.present(authViewController, animated: true, completion: nil) 
} 
0

आप बहुत करीब हैं! वी जय सही था कि आपको अपनी यूआरएल योजनाओं को अपने ऐप्स Info.plist फ़ाइल के अंदर परिभाषित करने की आवश्यकता है, और अपने ऐप प्रतिनिधि में कॉलबैक जोड़ें।

अनिवार्य रूप से आपको नीचे plist की जड़ में जोड़ने की आवश्यकता है।

<key>CFBundleURLTypes</key> 
<array> 
    <dict> 
     <key>CFBundleURLSchemes</key> 
     <array> 
      <string>com.googleusercontent.apps.{app-id-here}</string> 
     </array> 
    </dict> 
    <dict> 
     <key>CFBundleURLSchemes</key> 
     <array> 
      <string>fb{app-id-here}</string> 
     </array> 
    </dict> 
</array> 
<key>FacebookAppID</key> 
<string>{app-id-here}</string> 
<key>FacebookDisplayName</key> 
<string>{name-here}</string> 
<key>LSApplicationQueriesSchemes</key> 
<array> 
    <string>fbauth2</string> 
</array> 

आप अपने GoogleService-Info.plist फ़ाइल में RESERVED_CLIENT_ID प्रविष्टि से अपने गूगल ऐप्स आईडी प्राप्त कर सकते हैं।

इसके बाद, मेरे AppDelegate फ़ाइल सिर्फ openURL प्रतिनिधि विधि लागू करता है: कैसे सेटिंग्स फेसबुक

के लिए

इस दोस्त here स्थापित करने के लिए के लिए

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { 

    let googleSignIn = GIDSignIn.sharedInstance().handle(url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplicationOpenURLOptionsKey.annotation]) 

    let facebookSignIn = FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplicationOpenURLOptionsKey.annotation]) 

    return googleSignIn || facebookSignIn 
} 

Explaination here Firebase के बारे में उनकी कार्यान्वयन का एक अच्छा उदाहरण प्रदान की गई है औथ यूआई

संबंधित मुद्दे