Swift的Facebook登录按钮 [英] Facebook Login button for Swift
问题描述
在Xcode中,如果我创建一个UIView
,然后将自定义类添加为FBSDKLoginButton
,则当我单击它时,它会引导我通过Facebook登录,然后将我返回到与FBSDKLoginButton
相同的页面,但是不必说登录按钮显示立即注销.点击登录按钮后,如何显示新视图?
In Xcode if I create a UIView
and then add the custom class as FBSDKLoginButton
, when I click it leads me through the Facebook login and then returns me to the same page as the FBSDKLoginButton
but instead of saying login button it says log out now. How would I go about making that when the login button is clicked it lead to a new view?
我通过cocoapods下载了Facebook SDK,这是我第一次使用它,因此对此我感到困惑.感谢您的帮助!
I downloaded the Facebook SDK through cocoapods and its my first time working with it so I am confused about this. Thanks for the help!
推荐答案
一个选项是将视图控制器设置为FBSDKLoginButton
的委托并实现
One option would be to set your view controller as a delegate of the FBSDKLoginButton
and implement the loginButton:didCompleteWithResult:error:
method, which is called when the button is used to login.
快速
class ViewController: UIViewController, FBSDKLoginButtonDelegate {
@IBOutlet weak var loginButton: FBSDKLoginButton!
override func viewDidLoad() {
super.viewDidLoad()
self.loginButton.delegate = self
}
}
Obj-C
// ViewController.h
@interface ViewController : UIViewController <FBSDKLoginButtonDelegate>
@property (weak, nonatomic) IBOutlet FBSDKLoginButton *loginButton;
@end
// ViewController.m
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.loginButton.delegate = self;
}
然后,在loginButton:didCompleteWithResult:error:
方法中,您可以检查result
和error
,如果一切正常,请导航到另一个视图.
Then, in the loginButton:didCompleteWithResult:error:
method you can check the result
and error
, and if everything is fine, navigate to another view.
快速
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
if ((error) != nil) {
// Process error
}
else if result.isCancelled {
// Handle cancellations
}
else {
// Navigate to other view
}
}
Obj-C
// ViewController.m
@implementation ViewController
- (void)loginButton:(FBSDKLoginButton *)loginButton
didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result
error:(NSError *)error {
if (error) {
// Process error
}
else if (result.isCancelled) {
// Handle cancellations
}
else {
// Navigate to other view
}
}
您可以在其文档中找到有关如何使用FB登录的更多信息.
You can find more about how to login with FB in their docs.
这篇关于Swift的Facebook登录按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!