XServer.app Google 唱入:更新 iOS 中的用户数据 Swift

XServer.app Google Sing in: Update user data in iOS Swift

我正在构建一个 iOS 应用程序,需要允许我的用户使用 XServer

以 Google 登录

我已经下载并安装了 SDK,在我的 Google 云平台上创建了 OAuth 凭证,这是我目前的代码:

import GoogleSignIn

class GLogin: UIViewController, GIDSignInDelegate {

    @IBOutlet weak var googleButton: UIButton!
    var googleSignIn = GIDSignIn.sharedInstance()
    

    // Google Sign in button
    @IBAction func gSignInButton(_ sender: Any) {
        googleSignIn?.presentingViewController = self
        googleSignIn?.clientID = "My_Google_OAuth_Id"
        googleSignIn?.delegate = self
        googleSignIn?.signIn()
    }

}


func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
           guard let user = user else {
               print("Google login cancelled.")
               return
           }
           
           // Get User info
           let password = user.userID ?? ""
           let firstName = user.profile.givenName ?? ""
           let lastName = user.profile.familyName ?? ""
           let email = user.profile.email ?? password + "@google.com"
           let profilePicURL = user.profile.imageURL(withDimension: 150)?.absoluteString
           let username = firstName.lowercased() + lastName.lowercased()
           let fullName = firstName + " " + lastName
           
           self.XSSignUp(username: username, password: password, email: email, signInWith: "google") { (results, e) in
           if error == nil {
               
               // I don't know what to do here...

           } else { DispatchQueue.main.async { print(e!) }
       }}// ./ XSSignUp()

    }
       
    func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
           // Call your backend server to delete user's info after they requested to delete their account
    }

因此,我可以成功检索 Google 用户的数据,并在数据库中注册用户,但我不知道如何更新其全名和个人资料图片,所以这些列是执行上述代码后为空。

我错过了什么?

我是 XServer.app 的创建者,到目前为止你做得很好,所以你缺少的是一些代码来检查你的用户是否已经注册 Google 或者他正在重新登录。您还需要调用 XSObject() 函数以将其他数据添加到您的数据库 -> Users table.

因此,在 space 您放置评论的位置:

// I don't know what to do here...

尝试使用此代码:

 let resultsArr = results!.components(separatedBy: "-")
 let uID = resultsArr[0]
 let isSignInWithAppleGoogle = resultsArr[1]

 // Already signed up
 if isSignInWithAppleGoogle == "true" {
   print("User previously signed up") 

 } else {
   DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {

     // Add data using the XSObject() function
     let params = [
       self.param(columnName: "tableName", value: "Users"),
       self.param(columnName: "ID_id", value: uID),
       self.param(columnName: "FL_profilePic", value: profilePicURL),
       self.param(columnName: "ST_fullname", value: fullName),
     ]
     self.XSObject(params) { (e, obj) in
       if e == nil {
         print("Google user just signed up!")
       // error
       } else { DispatchQueue.main.async { print(e!)
     }}}// ./ XSObject
   })

 } // ./ If

请注意,FL_profilePicST_fullname 列名称必须与您在数据库中创建列时为其指定的名称相匹配。

更多信息,您也可以查看这篇文章:https://xscoder.hashnode.dev/google-sign-in-with-xserver-ios-swift