我是新iPhone的发展,我想使用最新的Facebook SDK 4.一些我的家乡的iOS简单的应用程序做一个Facebook登录,我只是想打开一个网站登录对话框从自定义UIButton
点击,我的应用程序,并为我做以下我的按钮单击方法中的代码。
- (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应用程序中打开“自定义单击”的登录对话框。
因此,请通过一些代码提出正确的建议,谢谢
使用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)")
}
})
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句