FBSDKLoginManager 未在本机 ios 应用程序中打开登录对话框
Posted
技术标签:
【中文标题】FBSDKLoginManager 未在本机 ios 应用程序中打开登录对话框【英文标题】:FBSDKLoginManager not open login Dialog in native ios apps 【发布时间】:2015-06-11 04:19:58 【问题描述】:我是 iPhone 开发的新手,我想使用最新的 facebook sdk 4 从我的原生 ios 简单应用程序进行 facebook 登录。我只想从自定义 UIButton
打开一个 facebook 登录对话框,在我的应用程序中为此,我在按钮单击方法中执行以下代码..
- (IBAction)didTapLoginFaceBook:(id)sender
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
if (error)
// Process error
NSLog(@"Unexpected login error: %@", error);
NSString *alertMessage = error.userInfo[FBSDKErrorLocalizedDescriptionKey] ?: @"There was a problem logging in. Please try again later.";
NSString *alertTitle = error.userInfo[FBSDKErrorLocalizedTitleKey] ?: @"Oops";
[[[UIAlertView alloc] initWithTitle:alertTitle
message:alertMessage
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
else if (result.isCancelled)
// Handle cancellations
else
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
if ([result.grantedPermissions containsObject:@"email"])
// Do work
];
此代码的工作方式有所不同,但我想打开一个登录对话框表单自定义UIButton
在本机 iOS 应用程序中单击。
所以,请用一些代码提出正确的方法,提前谢谢
【问题讨论】:
【参考方案1】:在 Objective-C 中使用自定义按钮的 Facebook 登录 4.x
- (IBAction)btnFacebookPressed:(id)sender
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
login.loginBehavior = FBSDKLoginBehaviorBrowser;
[login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
if (error)
// Process error
else if (result.isCancelled)
// Handle cancellations
else
if ([result.grantedPermissions containsObject:@"email"])
NSLog(@"result is:%@",result);
[self fetchUserInfo];
[login logOut];
];
- (void)fetchUserInfo
if ([FBSDKAccessToken currentAccessToken])
NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);
[[[FBSDKGraphRequest alloc] initWithGraphpath:@"me" parameters:@@"fields": @"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location , friends ,hometown , friendlists"]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
if (!error)
NSLog(@"resultis:%@",result);
else
NSLog(@"Error %@",error);
];
在 Swift 中使用自定义按钮的 Facebook 登录 4.x
@IBAction func btnFacebookPressed(_ sender: Any)
let loginManager = FBSDKLoginManager()
loginManager.loginBehavior = FBSDKLoginBehavior.browser;
loginManager.logIn(withReadPermissions: ["email"], from: self, handler: (result : FBSDKLoginManagerLoginResult?, error : Error?) -> Void in
if error != nil
// Process error
else if (result?.isCancelled)!
// Handle cancellations
else if (result?.grantedPermissions.contains("email"))!
print("result is: \(result)");
self.fetchUserInfo()
loginManager.logOut()
)
func fetchUserInfo()
if let accessToken = FBSDKAccessToken.current()?.tokenString
print("Token is available : \(accessToken)")
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location , friends ,hometown , friendlists"]).start(completionHandler: (connection : FBSDKGraphRequestConnection?, result : Any?, error : Error?) in
if let error = error
print("Error \(error)")
else if let result = result
print("result:\(result)")
)
【讨论】:
它如何注销当前用户?当我再次按下登录按钮时,它直接进入“您已经授权演示应用程序”.. 我用另一个按钮注销, @Hirenkanetiya 是的,将此代码添加到您的注销按钮的操作中 @Hirenkanetiya[[FBSDKLoginManager new] logOut];
从登录按钮的代码中删除注销代码,并通过单击按钮注销您自己。你也可以检查这个答案***.com/questions/15610152/…以上是关于FBSDKLoginManager 未在本机 ios 应用程序中打开登录对话框的主要内容,如果未能解决你的问题,请参考以下文章
带有电子邮件地址的 FBSDKLoginManager 令牌
Javascript onloadedmetadata 事件未在 iOS 设备上触发