- (void)request:(FBRequest *)request didLoad:(id)result not called - facebook

Why does this method never called? who can help me to review my code, i am a green hand.
here is my *.h file
#interface LoginViewController : UIViewController <FBSessionDelegate,FBRequestDelegate,FBDialogDelegate> {
Facebook *facebook;
IBOutlet UITextField *_txt;
NSString *facebookUserID;
I want to get the faceboo user ID,So i called the API '[facebook requestWithGraphPath:#"me" andDelegate:self];'
- (void) fbDidLogin
{
NSLog(#"[LOGIN] Used did login");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
// User has logged in to Facebook, now get their userId from Facebook
[facebook requestWithGraphPath:#"me" andDelegate:self];
}
I know below method will get the user ID,but it never called.
- (void)request:(FBRequest *)request didLoad:(id)result
{
NSLog(#"Inside didLoad");
if ([result isKindOfClass:[NSArray class]])
{
result = [result objectAtIndex:0];
}
// When we ask for user infor this will happen.
if ([result isKindOfClass:[NSDictionary class]])
{
//NSDictionary *hash = result;
NSLog(#"Name: %#", [result objectForKey:#"name"]);
self.facebookUserID = [result objectForKey:#"id"];
NSLog(#"ID: %#", [result objectForKey:#"id"]);
}
}

You need to implement the openURL or handleOpenURL (the latter is prior to ios 4.2). It needs to be implemented in your app delegate, not your view controller.
See fbDidLogin not called. openURL will result in fbDidLogin getting called
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
NSLog(#"============HANDLE OPEN URL");
ViewController *v =(ViewController *) vc;
return [[v facebook] handleOpenURL:url];
}

Related

Facebook SSOw Login and Posting Image not working.

I will try and be as in-depth as possible. I followed the guide on the Facebook dev site and ended up with this code:
in my AppDelegate.h:
#interface AppDelegate : UIResponder <UIApplicationDelegate, FBSessionDelegate> {
...
Facebook *facebook;
}
#property (nonatomic, retain) Facebook *facebook;
In the AppDelegate implementation I have specified the facebook appid etc.
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [facebook handleOpenURL:url];
}
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.menuViewController = [[MenuViewController alloc] init];
UIView *menuView = self.menuViewController.view;
facebook = [[Facebook alloc] initWithAppId:#"172938549507285" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
[facebook authorize:nil];
}
...
}
and just incase my app was never closed I have implemented the same method in:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
facebook = [[Facebook alloc] initWithAppId:#"172938549507285" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
[facebook authorize:nil];
NSLog(#"Facebook session is not valid");
}
NSLog(#"Application Entering Foreground.. setting segue to login flag");
[defaults setObject:#"yes" forKey:#"shouldgotologin"];
[defaults synchronize];
}
However even after implementing all this I never get the facebook login window open... It just never comes up... it came up once before... But I now never see anything in my console log.... I dont really know why... I am very lost. can someone help me? Please dont direct me to the Facebook dev site... that is where I just came from thanks!.
Debug the code.
As Chris said, you are now logged in.
if (![facebook isSessionValid]) {
}
put an else block here to give confirmation that you are alreadly logged in or perhaps move on to the next viewcontroller.
This may sound like a stupid question, but you say that you only saw it once... are you sure you're not just logged in now?
If you uninstall and re-install the App do you see it again?

Cocoa Facebook login not working

My APP's Facebook login wont work and I cant seem to figure out why.
In my .h file i added #import "FBConnect.h" and #interface MyImageViewControllerViewController : UIViewController<UINavigationControllerDelegate, FBSessionDelegate, FBRequestDelegate> and i have my Facebook *facebook; iVar.
I implement all of the methods that are required (As said in the Tutorial) this is how I did it. ( I wont post everything coz I think it'll be pointless)
-(BOOL)facebooking{
facebook = [[Facebook alloc] initWithAppId:FB_APP_ID andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
[facebook authorize:nil];
}
return YES;
}
And
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
UIAlertView *a = [[UIAlertView alloc] initWithTitle:#"Token" message:[facebook accessToken] delegate:self cancelButtonTitle:#"ok" otherButtonTitles: nil];
[a show];
}
Now when I call the facebooking method it starts up the safari on my Facebook APP auth page, i press Okay it goes back to my iOS app but for some reason the fbDidLogin isn't called. neither is fbDidNotLogin. Is there a place that i need to point to them somehow?
Thank you.
Have you implementing this methode ?
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [facebook handleOpenURL:url];
}

ios facebook api not fire fbLogin

i need to use the facebook api:
in the appdelegate.m I have this code:
// Initialize Facebook
facebook = [[Facebook alloc] initWithAppId:#"MY_TOKEN" andDelegate:self];
// Check and retrieve authorization information
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"] && [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
after, in another view I have this:
AppDelegate* dlg = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSArray *permissions = [[NSArray alloc] initWithObjects:#"offline_access", nil];
if (![[dlg facebook] isSessionValid]) {
[[dlg facebook] authorize:permissions];
}
and works fine: call safari with the authorization to use my app, I click Ok to authorize and my app is called again... my app visualize the last view before the authorize, but the event
- (void)fbDidLogin
{
NSLog(#"fbDidLogin");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
in AppDelegate.m (my appdelegate have FBSessionDelegate) is not fired (also the other events of FBSessionDelegate like fbDidNotLogin are not fired...
where is my error?
thanks in advance
I think you forgot about this two methods and perhaps forgot to add your Facebook app id to URL types in info file. for more detail
// Pre 4.2 support
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
// For 4.2+ support
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [facebook handleOpenURL:url];
}

Facebook SDK for iOs isn't working correctly

I'm developing an iOs 5 app and I'm trying to add Facebook sharing with Facebook SDK. I've been coding a lot but it doesn't work. This is the code I use from the Facebook Developers Site:
- (IBAction) buttonpressed {
facebook = [[Facebook alloc] initWithAppId:#"245697495524266" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"] && [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
[facebook authorize:nil];
} else {
[self fbDidLogin];
}
}
- (BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [facebook handleOpenURL:url];
}
- (void)fbDidLogin {
NSLog(#"Facebook did login");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
// Here I would like to share something (photo...)
}
When I press the button I go to the Facebook app and I have to allow the app, then I return to my app. The problem is that - (void) fbDidLogin doesn't run (the NSLog doesn't appear in the console), so I can't share anything. How can I fix it?
The only problem can be if your class does not conform to FBSessionDelegate in its .h file
i.e
#interface Yourclass : NSObject <FBSessionDelegate>
Where did you put this code ? in the app delegate?
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [facebook handleOpenURL:url];
}
it must be in the app delegate not in your class.
I think you forgot about this two methods and perhaps forgot to add your Facebook app id to URL types in info file. for more detail
// Pre 4.2 support
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
// For 4.2+ support
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [facebook handleOpenURL:url];
}

How to get UserName in Facebook API ios?

i'm using Facebook native API for sharing in my iPhone App, i need to know as soon as user logged in i need his username, how can i get this?
Any help is appriciated in advance.
You can use Graph API to do this. Just make a request in fbDidLogin delegate method:
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
[facebook requestWithGraphPath: #"me" andDelegate: self];
}
And then you can get all the information about current user in didLoad method:
- (void)request:(FBRequest*)request didLoadRawResponse:(NSData*)data
{
NSString *response = [[NSString alloc] initWithData: data
encoding:NSUTF8StringEncoding];
NSLog(#"%#", response);
[response release];
}
- (void) request:(FBRequest*)request didLoad:(id)result
{
if ([result isKindOfClass:[NSDictionary class]])
{
NSString *name = [result objectForKey: #"name"];
NSString *fbID = [result objectForKey:#"id"];
}
}