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。 数据可以包括用户公共档案,电子邮件甚至朋友列表。
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)
步骤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)
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:
步骤5)最终-更新AppDelegate (Step 5) Final — Update AppDelegate)
使用SDK登录按钮登录 (Login with SDK login button)
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除
public_profile
和
使用自定义按钮登录 (Login with Custom button)
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
来处理登录/注销功能,然后开发人员可以使用自定义登录按钮。 取而代之的LoginButtonDelegate
, LoginManager
处理Facebook登录结果作为回调。
以测试人员身份登录 (Login As as a 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帐户添加为注册测试人员来解决该错误。 请参阅我的另一篇有关解决此问题的中型文章:
检索敏感用户的信息 (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_profile
和email
,则需要Facebook进行审查,然后才能被应用程序开发人员以外的其他人使用。 这个过程可能持续数周。
登出 (Logout)
Facebook SDK logout function is straight forward and loginButtonDidLogOut: loginButton
of LoginButtonDelegate
is called if default FBSDKLoginButton
is used.
Facebook SDK注销功能很简单,如果使用默认的FBSDKLoginButton
, LoginButtonDelegate
调用loginButtonDidLogOut: loginButton
的LoginButtonDelegate
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)
- Facebook SDK provides authentication feature for retrieving user sensitive personal information after logging in through SDK. Facebook SDK提供了身份验证功能,用于通过SDK登录后检索用户敏感的个人信息。
- 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默认的一个相对容易实现。
- 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服务器检索用户的个人信息。
Sensitive information other than
public_profile
andemail
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_profile
和email
之外的其他敏感信息。 必须先通过Facebook审查,然后才能在App Store和Google Play商店发布应用。 它可能持续数周!- 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 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