可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm using a pop up Facebook dialog for the user login and the publishing of a post on his/her stream
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys: apiKey, @"api_key", nil];
[facebook dialog:@"stream.publish" andParams:params andDelegate:self];
It works, but when the user logs in the fbDidLogin method is never called.
Why?
In this way I can't request the user name and the access token is always null.
EDIT
With the october facebook api I have in part resolved my problem.
回答1:
I just had the same problem and solved it. You NEED to implement
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
in your app delegate class, not your custom UIViewController or any other class. Then direct the call from your app delegate to whatever controller is responsible for your Facebook.
回答2:
Did you register you app to handle your facebook application id URL? If not, there are instructions how to do it on the Facebook iOS SDK website. You also need to tell your app delegate to handle calls to this URL. To do this, add the following code to your app delegate implementation:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [[controller facebook] handleOpenURL:url];
}
Where [controller facebook] is the instance of your Facebook class.
回答3:
You'll need to set the sessionDelegate member variable of the facebook handle in front of calling any dialog methods.
self.facebook.sessionDelegate = self;
Also you'll need to implement the FBSessionDelegate callbacks in your object.
I did NOT check this for stream.publish, but this is what for
[facebook dialog:@"oauth" andParams:params andDelegate:self];
is neccessary.
回答4:
The Mike Bretz method worked for me.
I have a class (a view Controller) that has a button that does the following :
NSMutableDictionary * params = [NSMutableDictionary dictionary];
[params setObject:FACEBOOK_APP_ID forKey:@"client_id"];
[[SHARED_APP facebook] setSessionDelegate:self];
[[SHARED_APP facebook] dialog:@"oauth" andParams:params andDelegate:self];
where my controller also conforms to the protocol FBSessionDelegate
and SHARED_APP is my [[UIApplication sharedApplication] delegate]
I implemented the method
-(void)fbDidLogin {
NSLog(@"fbDidLogin");
}
and it worked !
I can then make subsequent calls to methods like
[[SHARED_APP facebook] requestWithGraphPath:@"me/friends" andDelegate:self];
to retrieve JSON data (with FBRequestDelegate)
Be careful, for every request, to test the return value such as explained here
http://developers.facebook.com/blog/post/500 to keep looking that you're still authenticated
Hope it helps
回答5:
I just had this problem and believe I may have found your solution. When you create the app on facebook you must edit your app settings to allow for native ios app. The ios bundle id selected must match EXACTLY to your bundle ID in the app you are creating. Facebook states that they check this prior to authorizing your app. Funny thing is if you remove the facebook app from the device and then try to authenticate it uses the web browser and seemingly bipasses this "security" completely. Facebook doesn't send an error message back so it was a lot of fun to track down. I figured I'd throw this answer out there in hopes to save some future devs some trouble. I hope this helps.
回答6:
In my case fbDidLogin was not called because of a mistake in the info.plist.
URL Schemes should be added to the URL Types. And here, fbMyAppId string is added as an item 0. (mistakenly I added URL Identifiers instead of URL Schemes)
回答7:
You only have to implement that method within your App Delegate as this...
- (void)fbDidLogin {
NSLog(@"DID LOGIN!");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[[self facebook] accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[[self facebook] expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
The mistake your are making is that you are implementing this method in your Custom View.
EDIT:
The above code works perfect, but if you want to get the control on your custom viewcontroller you can maintain that code on your appDelegate and add this code on your CustomViewController:
delegate= (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate facebook].sessionDelegate=self;
So, you will able to override fbDidLogin within your CustomViewController, don't forget add the FBSessionDelegate on your CustomViewController.h (Header).
Kind Regards.
回答8:
When you follow steps from the sample app, into your custom app, make the controller class as singleton, and then it works!!!
Most of the developers alloc and init 2 instances, because of which the delegate fbDidlogin is never called.
Hope this helps.
回答9:
fbDidLogin is being called by Facebook object after login successful,So please remember to assign seesionDeletegate:
facebook = [[Facebook alloc] initWithAppId:kFacebookApiKey];
facebook.sessionDelegate = self;
回答10:
map sure it is in the application delegate class i.e.: MYAPPAppDelegate.m
meaning the application:(UIApplication *) app openURL: methods