登录检查
如果你的app需要获取除了public_profile和email更多的信息,Facebook必须在你获取之前审核。
了解更多审核过程以及审核需要的条件请查看登录审核指南。
查看FaceBook的开发文档或者APP测试FaceBook登录,都需要翻墙。
自定义登录按钮
除了使用Facebook品牌的登录按钮(详情请见Facebook登录快速启动)你或许想自定义一个按钮的样式以及它的响应事件。在下面的代码示例中通过使用loginManager class(LoginManager)和一个自定义的button(UIButton)进行了登录。你可以使用任何其它的自定义图形化界面或者事件处理来进行登录。
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Add a custom login button to your app
let loginButton = UIButton(type: .custom)
loginButton.backgroundColor = .darkGray
loginButton.frame = CGRect(x: 0, y: 0, width: 180, height: 40)
loginButton.center = view.center
loginButton.setTitle("My Login Button", for: .normal)
// Handle clicks on the button
loginButton.addTarget(self, action: #selector(loginButtonClicked), for: .touchUpInside)
view.addSubview(loginButton)
}
// Once the button is clicked, show the login dialog
func loginButtonClicked() {
let loginManager = LoginManager()
loginManager.logIn(permissions: ["public_profile"], from: self) { result, error in
if let error = error {
print("Encountered Erorr: (error)")
} else if let result = result, result.isCancelled {
print("Cancelled")
} else {
print("Logged In")
}
}
}
}
Access Tokens
Notifications
你可以通过.AccessTokenDidChange notification in NotificationCenter追踪AccessToken.current的改变。这允许你响应用户登录状态的改变:
NotificationCenter.default.addObserver(
forName: .AccessTokenDidChange,
object: nil,
queue: .main
) { notification in
if notification.userInfo?[AccessTokenDidChangeUserIDKey] != nil {
// Handle user change
}
}
在iOS中 SDK可以更新过期的AccessToken,比如更新一个长时间的token。所以,你应该检查notification中的userInfo dictionary中AccessTokenDidChangeUserIDKey的值来查看是否用户信息改变了。
Profiles
Profile包含了公开的描述信息,比如用户的name和profile picture。你可以获取用户的profile通过使用loadCurrentProfile(completion:)。
Profile.loadCurrentProfile { profile, error in
if let firstName = profile?.firstName {
print("Hello, (firstName)")
}
}
你可以把enableUpdatesOnAccessTokenChange属性设置为true来使得profile自动的加载Profile.current。这也让你监听.ProfileDidChange通知来响应profile的改变:
Profile.enableUpdatesOnAccessTokenChange(true)
NotificationCenter.default.addObserver(
forName: .ProfileDidChange,
object: nil,
queue: .main
) { notification in
if let currentProfile = Profile.current {
// Update for new user profile
}
}
展示 Profile Pictures
FBProfilePictureView class提供了一个简单的方式显示用户在Facebook上的头像。设置FBProfilePictureView()引用的profileID属性,你可以将其设置为AccessToken.current.userID来展示当前登录用户的照片。
let profilePictureView = FBProfilePictureView()
profilePictureView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
profilePictureView.profileID = AccessToken.current!.userID
view.addSubview(profilePictureView)
设置 Authorization Type
Facebook登录按钮默认的 auth_type是rerequest。如果把auth_type设置为nil,将不再次请求之前被拒绝给予的权限。如果设置为.reauthorize,则在用户数据过期时重新请求。
下面的例子展示了在swift中如何设置auth_type的。
let loginButton = FBLoginButton()
// Set to nil for no auth_type
button.authType = nil
// Or set to reauthorize
button.authType = .reauthorize
下面的例子展示了如何用LoginConfiguration设置auth_type并将LoginConfiguration实例配置给LoginManager()实例
let config = LoginConfiguration(
permissions: [],
tracking: .enabled,
messengerPageId: nil,
authType: nil
)
let loginManager = LoginManager()
loginManager.logIn(viewController: self, configuration: config!) { loginResult in
// Do something with loginResult
}
若有收获,就点个赞吧