Tested with latest Firebase iOS SDK i.e. 4.0.0 and Xcode 8.3
Firstly , remove this key FirebaseAppDelegateProxyEnabled
from info.plist. This is not needed.
Now in AppDelegate.swift add following functions
import Firebase
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate , UNUserNotificationCenterDelegate{
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
FirebaseApp.configure()
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Pass device token to auth.
let firebaseAuth = Auth.auth()
//At development time we use .sandbox
firebaseAuth.setAPNSToken(deviceToken, type: AuthAPNSTokenType.sandbox)
//At time of production it will be set to .prod
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let firebaseAuth = Auth.auth()
if (firebaseAuth.canHandleNotification(userInfo)){
print(userInfo)
return
}
}*
Send a verification code to the user's phone:
In the class where you want to integrate Phone Authentication write :
Note : I have added +91
as its country code for India. You can add country code according to your region.
PhoneAuthProvider.provider().verifyPhoneNumber("+919876543210") { (verificationID, error) in
if ((error) != nil) {
// Verification code not sent.
print(error)
} else {
// Successful. User gets verification code
// Save verificationID in UserDefaults
UserDefaults.standard.set(verificationID, forKey: "firebase_verification")
UserDefaults.standard.synchronize()
//And show the Screen to enter the Code.
}
Sign in the user with the verification code:
let verificationID = UserDefaults.standard.value(forKey: "firebase_verification")
let credential = PhoneAuthProvider.provider().credential(withVerificationID: verificationID! as! String, verificationCode: self.txtEmailID.text!)
Auth.auth().signIn(with: credential, completion: {(_ user: User, _ error: Error?) -> Void in
if error != nil {
// Error
}else {
print("Phone number: \(user.phoneNumber)")
var userInfo: Any? = user.providerData[0]
print(userInfo)
}
} as! AuthResultCallback)