如何使用iOS的Swift设置Google登录SDK

这篇教程详细解释了如何不使用pod,而是手动在iOS项目中安装和配置Google Sign In SDK 4.0.1。教程涵盖从Google开发者页面下载SDK,添加必要的框架,配置Xcode项目,创建配置文件,实现GIDSignInDelegate协议,以及添加登录按钮的步骤。
摘要由CSDN通过智能技术生成

by Onur Tuna

通过Onur Tuna

如何使用iOS的Swift设置Google登录SDK (How to set up Google Sign In SDK with Swift for iOS)

This post is a clearer explanation of the implementation presented in the Google Developer tutorial. The Google tutorial recommends you to use pod, however, I don’t like to use pod — I want more freedom. So, this tutorial installs and sets the SDK manually.

这篇文章是对Google Developer教程中介绍的实现的更清晰的解释。 Google教程建议您使用pod,但是,我不喜欢使用pod –我想要更多的自由。 因此,本教程将手动安装和设置SDK。

The Google tutorial wrote their sample project in Objective-C. At the end of this post, you can find a sample project written in Swift.

Google教程在Objective-C中编写了示例项目。 在本文的结尾,您可以找到一个用Swift编写的示例项目。

开始吧 (Let’s start)

Now we’re going to install the latest SDK from the Google Developers page. Here in this tutorial, the SDK version is 4.0.1. You can use any version but I recommend using the latest one.

现在,我们将从Google Developers页面安装最新的SDK。 在本教程的此处,SDK版本为4.0.1。 您可以使用任何版本,但我建议使用最新版本。

When you download the SDK you’re going to see the following files and folders:

下载SDK时,您将看到以下文件和文件夹:

  • CHANGELOG.md

    CHANGELOG.md
  • GoogleAppUtilities.framework

    GoogleAppUtilities.framework
  • GoogleSignIn.bundle

    GoogleSignIn.bundle
  • GoogleSignIn.framework

    GoogleSignIn.framework
  • GoogleSignInDependencies.framework

    GoogleSignInDependencies.framework
  • GoogleSymbolUtilities.framework

    GoogleSymbolUtilities.framework
  • README.md

    自述文件
  • Sample: This is a sample project written in Objective-C.

    样本:这是一个用Objective-C编写的样本项目。

Now create a project using Xcode. We’re going to link all necessary frameworks to it. Put the frameworks located in the SDK folder anywhere you want. I prefer collecting all the libraries in a folder called Library under my main Projects folder.

现在使用Xcode创建一个项目。 我们将链接所有必要的框架。 将框架放在SDK文件夹中所需的任何位置。 我更喜欢将所有库收集在主Projects文件夹下一个名为Library的文件夹中。

Open your project and go to Build Settings. Enter the path where your frameworks are located.

打开您的项目,然后转到“ 构建设置” 。 输入框架所在的路径。

Next copy the GoogleSignIn.bundle file by drag-dropping into the project. You have to drag and drop the frameworks too but do not copy them.

接下来,通过拖放到项目中来复制GoogleSignIn.bundle文件。 您也必须拖放框架,但不要复制它们。

We need two more frameworks: Safari Services and System Configuration. Apple provides them. You can link them in the Build Phases > Link Binary with Libraries.

我们还需要两个框架: Safari ServicesSystem Configuration 。 苹果提供它们。 您可以在Build Phases> Link Binary with Libraries中将它们链接

The last thing you should do in this part is to add a linker flag. Add the flag below in Build Settings > Other Linker Flags:

在这部分中您应该做的最后一件事是添加一个链接器标志。 将以下标记添加到“ 构建设置 ”中 其他链接器标志:

$(OTHER_LDFLAGS) -ObjC

$(OTHER_LDFLAGS)-ObjC

配置文件 (Configuration File)

It’s time for getting a configuration file for your project. You should start an app in the Google Developer page. However, you’ll not copy the configuration file. Instead, keep it anywhere — some information in it might be needed later.

现在该为项目获取配置文件了。 您应该在Google Developer页面上启动一个应用。 但是,您不会复制配置文件。 相反,请将其保留在任何地方-稍后可能需要其中的一些信息。

Enter the page to create a project.

输入页面以创建项目。

Choose an app name and give your bundle identifier which you can find under General in Xcode. In the next page, you’ll enable sign in for your app by clicking the Enable Sign In button. After all that, download the GoogleService-Info.plist file. Keep it anywhere you want.

选择一个应用程序名称,并提供您的捆绑包标识符,您可以在Xcode的“ 常规”下找到该标识符。 在下一页中,您将通过单击“ 启用登录”按钮来启用应用程序的登录 。 然后,下载GoogleService-Info.plist文件。 将其保存在您想要的任何地方。

Go back to your Xcode project. Find your reversed client id in the plist file you have just downloaded. Paste it to the Info > URL Types.

回到您的Xcode项目。 在刚刚下载的plist文件中找到反向的客户机ID。 将其粘贴到“ 信息”>“ URL类型”

将Google登录添加到应用 (Add Google sign in to app)

Google Sign In SDK is an Objective-C library so you need a bridging header in order to bind it to your Swift project. You can create a bridging header manually. However, you can also leave it Xcode do this automatically.

Google登录SDK是一个Objective-C库,因此您需要一个桥接标头才能将其绑定到您的Swift项目。 您可以手动创建桥接头。 但是,您也可以让Xcode自动执行此操作。

Create a new .m file with a dummy name. It will ask you to create a bridging header — say yes. Remove the .m file, you don’t need it. Import the Google Sign In in your bridging header.

用一个虚拟名称创建一个新的.m文件。 它将要求您创建一个桥接标头-同意。 删除.m文件,您不需要它。 在您的桥接标题中导入Google登录。

#import “GoogleSignIn/GoogleSignIn.h”
#import“ GoogleSignIn / GoogleSignIn.h”

Now, go to you app delegate file named AppDelegate.swift. Your app delegate should look like something like this.

现在,转到名为AppDelegate.swift的应用程序委托文件。 您的应用程序委托应该看起来像这样。

It seems like a lot of code. However most of it is written by default when you create a new project.

似乎很多代码。 但是,大多数情况是在创建新项目时默认编写的。

Let me explain the changes. Your class — AppDelegate — now implements GIDSignInDelegate protocol. In order to conform the delegate we have implemented some methods: application:openURL:options: and signIn:signIn:didSignInForUser:withError:. Also we have configured GIDSignIn object in the application:didFinishLaunchingWithOptions: method. The rest is not important.

让我解释一下这些变化。 您的类-AppDelegate-现在实现GIDSignInDelegate协议。 为了使委托符合要求,我们实现了一些方法: application:openURL:options:signIn:signIn:didSignInForUser:withError:。 此外,我们还在application:didFinishLaunchingWithOptions:方法中配置了GIDSignIn对象。 其余的并不重要。

One significant issue is that you should paste your client id in the application:didFinishLaunchingWithOptions: method. You can find your client id in the plist file we downloaded.

一个重要的问题是您应该将客户端ID粘贴到application:didFinishLaunchingWithOptions:方法中。 您可以在我们下载的plist文件中找到您的客户端ID。

登录按钮 (Sign in button)

We can add a button and watch our app working. Go to your ViewController.swift. The final code should look something like below:

我们可以添加一个按钮并观看我们的应用程序运行。 转到您的ViewController.swift 。 最终代码应如下所示:

Only one line of code has been added. However, pay attention that our class implements the GIDSignInUIDelegate protocol. We need a button to make the user click. Go to your Story board and put a view on it. Drag and drop a UIView. Set GIDSignInButton as the base class and you’re done.

仅添加了一行代码。 但是,请注意,我们的类实现了GIDSignInUIDelegate协议。 我们需要一个按钮来使用户单击。 转到您的故事板,然后在上面查看。 拖放一个UIView 。 将GIDSignInButton设置为基类就可以了。

Now run the app and login. You’re finished with the basics. You can use Google Login in your apps from now on. In case you have any problems do not hesitate to contact me.

现在运行该应用程序并登录。 您已经掌握了基本知识。 从现在开始,您可以在应用程序中使用Google登录。 如果您有任何问题,请随时与我联系。

Example Codes

示例代码

onurtuna/Google-Signin-ExampleGoogle-Signin-Example - Google Sign in example using Swift 3github.com

onurtuna / Google-Signin-Example Google- Signin- Example- 使用Swift 3的Google登录示例 github.com

翻译自: https://www.freecodecamp.org/news/google-sign-in-sdk-with-swift-for-ios-914316e0ade8/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值