数字到firebase迁移iOS电话号码输入

时间:2017-09-20 06:48:06

标签: ios swift firebase firebase-authentication twitter-digits

我一直使用数字通过电话号码登录。我必须迁移到firebase,但有一些我很困惑的事情:

我的流程是: 1)用户点击通过电话号码登录的自定义按钮

@IBAction func loginViaDigits(_ sender: AnyObject) {
    let digits = Digits.sharedInstance()
    let configuration = DGTAuthenticationConfiguration(accountFields: .defaultOptionMask)
    configuration?.phoneNumber = "+44"
    digits.authenticate(with: self.navigationController, configuration: configuration!) { session, error in
        if error == nil {
            let digits = Digits.sharedInstance()
            let oauthSigning = DGTOAuthSigning(authConfig:digits.authConfig, authSession:digits.session())
            let authHeaders = oauthSigning?.oAuthEchoHeadersToVerifyCredentials()
            self.startActivityIndicator()
            NetworkApiCall(apiRequest: SignInApiRequest(digits_auth: authHeaders as! [NSObject : AnyObject])).run() { (result: SignInApiResponse?, error: NSError?) in
                if error != nil {
                    self.stopActivityIndicator()
                    UIUtils.showInfoAlert("Some error occurred!", controller: self)
                    return;
                }

                guard let response = result else {
                    self.stopActivityIndicator()
                    UIUtils.showInfoAlert("Some error occurred!", controller: self)
                    return;
                }

                ...
        }
    }
}

2)基本上,用户通过电话号码按钮点击登录,数字显示他们获取电话号码的弹出窗口然后他们要求验证码,当他们完成时,我会在我的回调方法中获得oauth params并且我通过了他们到我的服务器上。

我的问题是: 1)我是否需要自己构建电话号码输入和验证码输入屏幕,或者firebase是否像数字那样提供它们?

2)如果某人已经实际迁移了这种流程,那么一些指针会非常有用。

正如Lazy King所说,我正在尝试使用FirebaseAuthUI,但我的AppDelegate似乎缺少某些功能:

我的AppDelegate对Firebase进行了更改:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    // Pass device token to auth
    Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenType.prod)
}

func application(_ application: UIApplication,
                 didReceiveRemoteNotification notification: [AnyHashable : Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    if Auth.auth().canHandleNotification(notification) {
        completionHandler(.noData)
        return
    }
    // This notification is not auth related, developer should handle it.
}

但我仍然会收到此错误: 错误域= FIRAuthErrorDomain代码= 17054"如果禁用了app delegate swizzling,则需要将UIApplicationDelegate收到的远程通知转发给FIRAuth的canHandleNotificaton:方法。"

1 个答案:

答案 0 :(得分:2)

您可以使用FirebaseAuth UI或为您自己设计UI。对于我的一个项目,我使用了FirebaseAuth UI。这是一步一步:

  1. 添加FireBase Auth和Auth UI     pod'Firebase / Auth','〜> 4.0.0'和    pod'FirebaseUI / Phone','〜> 4.0'

  2. 在Appdelegate文件中注册推送通知,这是必需的。首次验证的Google用户推送通知。 在didRegisterForRemoteNotificationsWithDeviceToken上添加此行:

    Auth.auth().setAPNSToken(deviceToken, type:AuthAPNSTokenType.prod)//.sandbox for development
    
  3. 您还需要在Firebase控制台上设置Google通知

  4. 电话登录按钮功能
  5. var uiAuth = FUIAuth.defaultAuthUI() ;
    
    uiAuth?.delegate = self;
    var phoneVC = FUIPhoneAuth(authUI: uiAuth!)
    
    uiAuth.providers = [phoneVC];
    phoneVC.signIn(withPresenting: self)
    
  6. 实施委托功能

  7. 在Appdelegate中接收通知功能添加代码

     if Auth.auth().canHandleNotification(userInfo) {
        completionHandler(UIBackgroundFetchResult.noData)
        return
    }
    
  8. 如果您使用的是iOS 10或更高版本,那么

    @available(iOS 10.0, *)
    internal func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    
        let userInfo = response.notification.request.content.userInfo
        if Auth.auth().canHandleNotification(userInfo) {
            completionHandler()
            return
        }
    
        completionHandler()
    }
    

    希望它能奏效。