swift 封装sdk_Swift的facebook ios登录sdk

swift 封装sdk

Facebook is a great social media and allows user to upload images, videos and links to Facebook platform in form of News Feed and Stories. Nowadays, Facebook is a necessity in most people’s life.

˚Facebook是一个伟大的社会化媒体,并允许用户上传图片,视频和链接到Facebook平台上的动态消息和故事的形式。 如今,Facebook已成为大多数人生活中的必需品。

Facebook provides an easy to use SDK to allow app developers to implement the login and logout function inside their apps. After user authentication is verified, app can query the user data on behalf of the user or even share media to Facebook. The data can include user public profile, email and even friend list.

Facebook提供了易于使用的SDK,以允许应用程序开发人员在其应用程序中实现登录和注销功能。 验证用户身份验证后,应用程序可以代表用户查询用户数据,甚至可以将媒体共享到Facebook。 数据可以包括用户公共档案,电子邮件甚至朋友列表。

Image for post

Facebook user data is an important key to let you app know more about user’s information and preferences. It can also help engaging user’s friends to install your app. Do you want to grow up your app by implementing the Facebook SDK? Let’s start here!

Facebook用户数据是使您的应用程序更多了解用户信息和首选项的重要关键。 它还可以帮助吸引用户的朋友来安装您的应用程序。 您是否想通过实现Facebook SDK来扩展您的应用程序? 让我们从这里开始!

建立 (Setup)

Image for post

步骤1)注册一个Facebook开发者帐户 (Step 1) Register a Facebook Developer account)

You can register a Facebook Developer account at the Facebook Developer Console and create a Facebook project.

您可以在Facebook Developer Console上注册一个Facebook Developer帐户并创建一个Facebook项目。

步骤2)在iOS项目中添加Facebook Login SDK依赖项 (Step 2) Add Facebook Login SDK dependencies at iOS project)

Facebook Login SDK can be imported by 3 ways, Swift Package Manager, CocoaPods and Carthage. In my opinion, CocoaPods is the most convenient one.

Facebook登录SDK可以通过3种方式导入,即Swift软件包管理器,CocoaPods和Carthage。 我认为,CocoaPods是最方便的一种。

Add the following line of code to Podfile and and run pod install to download the dependency.

将以下代码行添加到Podfile ,然后运行pod install下载依赖项。

pod 'FBSDKLoginKit'

步骤3)将Bundle ID提交到Facebook (Step 3) Submit the Bundle ID to Facebook)

Image for post
Image for post

Bundle Identifier is the unique identifier of your app.

捆绑包标识符是您应用程序的唯一标识符。

步骤4)配置info.plist (Step 4) Configure info.plist)

App the following to the info.plist file to set the keys for communication with Facebook server.

将以下内容应用于info.plist文件以设置用于与Facebook服务器通信的密钥。

Replace those {facebook_app_id} (at line 6 & 11) with your own app id:

将那些{facebook_app_id} (第6和11行)替换为您自己的应用ID:

Image for post

步骤5)最终-更新AppDelegate (Step 5) Final — Update AppDelegate)

使用SDK登录按钮登录 (Login with SDK login button)

Image for post
Default Facebook SDK login button
默认Facebook SDK登录按钮
Image for post

Facebook SDK provides an easy to use login & logout button — FBSDKLoginButton. By declaring the type of button at Storyboard and the requested permissions (line 9), it is ready to use. The LoginButtonDelegate is triggered when user logs in / out successfully or unsuccessfully.

Facebook SDK提供了一个易于使用的登录和注销按钮FBSDKLoginButton 。 通过在Storyboard上声明按钮的类型和所请求的权限(第9行),就可以使用它了。 当用户成功或失败登录/注销时,将触发LoginButtonDelegate

All apps requesting user’s information other than public_profile and email will be reviewed by Facebook. Facebook would reject any app that cannot provide good reasons for extra sensitive information.Therefore, please think twice before setting the permission at line 9.

public_profileemail之外,所有要求用户提供信息的应用都将由Facebook进行审核。 Facebook将拒绝任何无法提供充分理由来提供额外敏感信息的应用程序,因此,请在设置第9行的许可之前三思而后行。

使用自定义按钮登录 (Login with Custom button)

Image for post

Facebook SDK provides a LoginManager to handle login / logout feature and developer can then use a custom login button. Instead of LoginButtonDelegate, LoginManager handles the Facebook login result as a callback.

Facebook SDK提供了LoginManager来处理登录/注销功能,然后开发人员可以使用自定义登录按钮。 取而代之的LoginButtonDelegateLoginManager处理Facebook登录结果作为回调。

以测试人员身份登录 (Login As as a Tester)

Image for post
Error of non-registered tester
未注册测试人员的错误

Facebook limits the group of public user who can test the login function during app development stage. The above error message is frustrating and the error can be solved by added the tester’s Facebook account as a registered tester at Facebook Console. See my another Medium article about solving this issue:

Facebook限制了可以在应用程序开发阶段测试登录功能的公共用户群体。 上面的错误消息令人沮丧,并且可以通过在Facebook Console上将测试人员的Facebook帐户添加为注册测试人员来解决该错误。 请参阅我的另一篇有关解决此问题的中型文章:

Image for post

检索敏感用户的信息 (Retrieving sensitive user’s information)

After user logs in and you get the access token, app can now retrieve user’s personal information from Facebook server. GraphAPI has to be used. You can find the full list of possible fields used in the GraphRequest from the official documentation site:

用户登录并获得访问令牌后,应用程序现在可以从Facebook服务器检索用户的个人信息。 必须使用GraphAPI 。 您可以从官方文档站点找到GraphRequest使用的可能字段的完整列表:

Moreover, according to the official documentation, if your app asks for more than than public_profile and email, it will require review by Facebook before your app can be used by people other than the app's developers. This process probably lasts for several weeks.

此外,根据官方文档 ,如果您的应用程序要求的内容超过public_profileemail ,则需要Facebook进行审查,然后才能被应用程序开发人员以外的其他人使用。 这个过程可能持续数周。

登出 (Logout)

Image for post

Facebook SDK logout function is straight forward and loginButtonDidLogOut: loginButton of LoginButtonDelegate is called if default FBSDKLoginButton is used.

Facebook SDK注销功能很简单,如果使用默认的FBSDKLoginButtonLoginButtonDelegate调用loginButtonDidLogOut: loginButtonLoginButtonDelegate loginButtonDidLogOut: loginButton

检查用户是否已登录 (Check if user has logged in)

AccessToken is a class which stores the logged-in user information. The tokenString is a key to make requests to Facebook APIs on behalf of an app rather than a user.

AccessToken是存储已登录用户信息的类。 tokenString是代表应用程序而非用户向Facebook API发出请求的键。

摘要 (Summary)

  1. Facebook SDK provides authentication feature for retrieving user sensitive personal information after logging in through SDK.

    Facebook SDK提供了身份验证功能,用于通过SDK登录后检索用户敏感的个人信息。
  2. Developer can use either SDK default login button or a custom button to display a login button for user. SDK default one is relatively easier to be implemented.

    开发人员可以使用SDK默认登录按钮或自定义按钮来显示用户登录按钮。 SDK默认的一个相对容易实现。
  3. Access token is generated after user logs in through SDK. It can be used to retrieve user’s personal information from Facebook server.

    用户通过SDK登录后,将生成访问令牌。 它可用于从Facebook服务器检索用户的个人信息。
  4. Sensitive information other than public_profile and email should be requested with good consideration. Facebook review has to be passed before app can be released at App Store and Google Play Store. It can last for several weeks!

    public_profile考虑除public_profileemail之外的其他敏感信息。 必须先通过Facebook审查,然后才能在App Store和Google Play商店发布应用。 它可能持续数周!

  5. Only registered tester can test the Facebook login feature in development stage. Tester’s identifier has to be added at the Facebook Console by admin first.

    在开发阶段,只有注册的测试人员才能测试Facebook登录功能。 测试人员的标识符必须首先由管理员在Facebook控制台中添加。

您可能想要 (You probably would like)

Image for post

You are welcome to follow me at Twitter@myrick_chow for more information and articles. Thank you for reading this article. Have a nice day! 😄

欢迎您通过Twitter @ myrick_chow关注我,以获取更多信息和文章。 感谢您阅读本文。 祝你今天愉快! 😄

翻译自: https://itnext.io/swift-facebook-ios-login-sdk-dada412d8341

swift 封装sdk

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值