Facebook connect restarts my iPhone app - iphone

I'm building an iPhone App where i want to add Facebook login.
In viewDidLoad i've added:
facebook = [[Facebook alloc] initWithAppId:#"MYAPPID" andDelegate:self];
facebook.sessionDelegate=self;
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
When the user taps a button i invoke a function that does:
NSArray *permissions = [[NSArray arrayWithObjects:#"read_stream", #"publish_stream", #"offline_access",nil] retain];
[facebook authorize:permissions ];
It launches safari and makes the facebook login; the problem is that when it returns to the app, it relaunches the app, while i need to return directly to my ViewController, not to the FirstViewController.
What am i doing wrong?

Found the answer in an old SO thread
In facebook.m :
- (void)authorize:(NSArray *)permissions {
self.permissions = permissions;
[self authorizeWithFBAppAuth:NO safariAuth:NO];
}

Related

Repeating authorization Window

I'm using Xcode 4.3.1
In my app I try to use Facebook SSO.My requirement is at the first time the app should ask for the authorization after that it should check whether the app is authorized by the user and should move to the next process(even after the task in the device is cancelled).I don't that authorization window to repeat again and again.
in Appdelegate:
facebook=[[Facebook alloc]initWithAppId:appid andDelegate:first];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog(#"%#Defaults:",[defaults objectForKey:#"FBAccessTokenKey"]);
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"])
{
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
accessToken=facebook.accessToken;
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
date=facebook.expirationDate;
}
In my view Controller:
-(IBAction)LoginClick:(id)sender
{
NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
[defaults1 setObject:xapp.facebook.accessToken forKey:#"FBAccessTokenKey"];
[defaults1 setObject:xapp.facebook.expirationDate forKey:#"FBExpirationDateKey"];
[defaults1 synchronize];
if (![xapp.facebook isSessionValid])
{
permissions = [[NSArray alloc] initWithObjects:#"read_stream",#"publish_stream", nil];
[xapp.facebook authorize:permissions];
}
else if([xapp.facebook isSessionValid])
{
NSLog(#"Hiii");
permissions=nil;
[xapp.facebook authorize: nil];
}
}
How can I make the authorization window to not appear after the app is authorized...
Alright, you are asking for permissions each time you run your app in your AppDelegate. To prevent this, you have to make a separate method for permission that will be granted only if the login button is pressed and checked if the app is permitted or not. Try this in your AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self.window makeKeyAndVisible];
facebook = [[Facebook alloc] initWithAppId:#"YOURAPPID" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"] && [defaults objectForKey:#"FBExpirationDateKey"])
{
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
return YES;
}
Above your fbDidLogIn method in your AppDelegate add this method, you have to add the FBUser protocol to your AppDelegate:
-(void)fbLogin:(id<FBUser>)_listener
{
listener = _listener;
if(![facebook isSessionValid])
{
NSArray* permissions = [[NSArray arrayWithObjects:
#"publish_stream",
#"read_stream",
nil] retain];
[facebook authorize:permissions];
}
else
{
[listener facebookDidLogin];
}
}
And in your ViewController modify your login method like this:
-(IBAction)LoginClick:(id)sender
{
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate fbLogin:self];
}
The FBUser.h is a custom header file that defines the protocol method -(void)fbDidLogIn. This is just to make sure that, the app does not ask login permission each it time launches.
#import <Foundation/Foundation.h>
#protocol FBUser <NSObject>
-(void)facebookDidLogin;
#end
By this, the login method is only called when the app needs to login to Facebook and do something.
Hope this helps you. Cheers!

iPhone facebook sdk shows Valid session even when I logged out through Facebook app

After many hours of trying codes, searching online, I have to ask for help. I have a share button in a UITableViewCell. On its touch action I open facebook login dialog and then if logged in I show feed dialog. It working fine but even if I logout through Facebook app the sessionIsValid shows YES. Here's the Code...
if(indexPath.row==0)
{
AppId=#"xxx";
AppDelegate *appDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
appDelegate.scheduleViewController=self;
self.facebook = [[Facebook alloc] initWithAppId:AppId andDelegate:self];
// [self.facebook requestWithGraphPath:#"me" andDelegate:self];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
self.facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
self.facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![self.facebook isSessionValid]) {
[self.facebook authorize:permissions];
}
else
[self doStuffAfterLogin:[self.facebook accessToken]];
}
- (void)fbDidLogin {
[defaults setObject:[self.facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[self.facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
[self doStuffAfterLogin:[self.facebook accessToken]];
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
NSLog(#"%#", [error localizedDescription]);
NSLog(#"Err details: %#", [error description]);
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:#"Error!" message:#"Error!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertView show];
[defaults removeObjectForKey:#"FBAccessTokenKey"];
[defaults removeObjectForKey:#"FBExpirationDateKey"];
[self.facebook authorize:permissions];
}
-(void)doStuffAfterLogin:(NSString*)accessToken;
{
NSMutableString *imgUrl=[[NSMutableString alloc]init];
if([[defaults objectForKey:#"imageUrl"] isEqualToString:#""])
imgUrl= #"http://artist.eventseekr.com/images/no-photo.png";
else
imgUrl=[defaults objectForKey:#"imageUrl"];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
AppId, #"app_id",
imgUrl, #"picture",
[NSString stringWithFormat:#"Watch %# in action at the %# event",[defaults objectForKey:#"name"],[defaults objectForKey:#"eventName"]], #"caption",
#"Sample Festival App", #"description",
nil];
[self.facebook dialog:#"feed" andParams:params andDelegate:self];
}
That's correct. Each app has it's own sandboxed area. So you have authorised your app and your app has stored a token that it can use for validating itself. It knows nothing about any other app. So if you logout in the facebook app, it doesn't know.

Facebook API dialog page showing error in second time and after that

I want to share my app details.I just done as same given in facebook tutorial.When user loggen in and dialog page appears and i posted my msg.and it worked fine for first time only.when user tapped a button to share again,the dialog appears and showing error page
"Error occurred with 'Your_APP_NAME'.please try again later."
I followed the tutorial in the facebook link below here. https://developers.facebook.com/docs/mobile/ios/build/
Here it is my code.
-(void)buttonPressed:(id)sender{
facebook = [[Facebook alloc] initWithAppId:#"YOUR_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];
}else{
[self postWall];
}
// Pre 4.2 support
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
[self postWall];
}
-(void)postWall{
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
kAppId,#"app_id",
#"https://developers.facebook.com/docs/reference/dialogs/",#"link",
#"http://fbrell.com/f8.jpg",#"picture",
#"Facebook Dialogs",#"name",
#"Reference Documentation",#"caption",
#"Using Dialogs to interact with users.",#"description",
#"Facebook Dialogs are so easy!",#"message",
nil];
[[self facebook] dialog:#"feed" andParams:params andDelegate:self];
}
I do all this code in my viewcontroller file(Not in app delegate).I want to open facebook only when i press the button..So i put these codings in my buttonpressed method.
Try with removing
kAppId,#"app_id"
from your NSMutableDictionary params

How to make my method run when the view re-load

I want to create a login button. When I click it the facebook launches, I accept my application, the Facebook application closes and my application reloads. My problem is that the second time my application starts, after accepting on Facebook, I cannot find where to disable the login button.
I wrote this code:
-(IBAction) login:(id)sender;
{
AutoAppDelegate *delegate = (AutoAppDelegate *) [[UIApplication sharedApplication] delegate];
// Check and retrieve authorization information
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
[delegate facebook].accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
[delegate facebook].expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![[delegate facebook] isSessionValid]) {
[delegate facebook].sessionDelegate = self;
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"user_likes",
#"read_stream",
nil];
[[delegate facebook] authorize:permissions];
} else {
logIn.hidden=YES;
lbl.text=#"";
lbl.text=#"Connected";
[self showLoggedIn];
}
}
- (void) showLoggedIn {
NSLog(#"11 Logged In ");
lbl.text=#"";
lbl.text=#"Connected";
}
- (void) showLoggedOut {
NSLog(#"11 Not Logged In ");
lbl.text=#"";
lbl.text=#"Not Connected";
}
- (void) viewWillAppear:(BOOL)animated
{
//[self.navigationController setNavigationBarHidden:YES animated:animated];
[super viewWillAppear:animated];
AutoAppDelegate *delegate = (AutoAppDelegate *) [[UIApplication sharedApplication] delegate];
// Check and retrieve authorization information
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
[delegate facebook].accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
[delegate facebook].expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![[delegate facebook] isSessionValid]) {
// [self showLoggedOut];
NSLog(#"Not Connected");
} else {
// [self showLoggedIn];
NSLog(#"Connected");
}
}
The viewWillAppear works the first time, but when I give permissions in the Facebook application, my application restarts but not viewWillAppear method!
You need to put the code in your application delegate (applicationWillEnterForeground), as it's the only thing to get notified automatically when the app is re-opened (your view is not necessarily re-drawn).
Have you tried to calling setNeedsDisplay on the view after the Facebook thing is done?

why fbdidlogin didn't call?

- (id)init {
if (self == [super init]) {
facebook = [[Facebook alloc] initWithAppId:kAppId];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
permissions = [[NSArray arrayWithObjects:
#"read_stream", #"user_birthday",
#"publish_stream", nil] retain];
[facebook authorize:permissions delegate:self];
}
[self login];
}
return self;
}
- (void)login {
if (![_session isConnected]) {
[self postToWall];
}
if (![facebook isSessionValid]) {
[facebook authorize:permissions delegate:self];
}
}
- (void)fbdidLogin {
[[NSUserDefaults standardUserDefaults] setObject:self.facebook.accessToken forKey:#"FBAccessToken"];
[[NSUserDefaults standardUserDefaults] setObject:self.facebook.expirationDate forKey:#"FBExpirationDate"];
// User has logged in to Facebook, now get their userId from Facebook
[facebook requestWithGraphPath:#"me" andDelegate:self];
}
-(void)postToWall
{
SBJSON *jsonWriter = [[SBJSON new] autorelease];
NSDictionary *actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
#"Always Running",#"text",#"http://itsti.me/",#"href", nil], nil];
NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];
NSDictionary *attachment = [NSDictionary dictionaryWithObjectsAndKeys:
#"Your Happiness!", #"name",
#"asda", #"caption",
#"asdf", #"description",
nil];
NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Share on Facebook", #"user_message_prompt",
actionLinksStr, #"action_links",
attachmentStr, #"attachment",
nil];
[facebook dialog:#"stream.publish" andParams:params andDelegate:self];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
This is my methods for calling facebook dialog boxes.But every time when i click to score for publishing;The permission box came first.I need to give permission after that the publish dialog box comes.I want to give permission ones and i want it to save it;after saving it i don't wanna see the permission box ever again.How can i do it?What is wrong with my code?
edit:
My access token and expiration date is null i guess thats because of this.
A few things to check, you call the method login handling method "fbdidLogin" instead of fbDidLogin. This is case sensitive.
Make sure in your view controller header file that you added FBSessionDelegate as one of the protocols you support. That could be a reason fbDidLogin is not called. You could always add an NSLog to verify that the fbDidLogin method is reached.
You need to implement Facebook's Single-Sign-On feature.
http://developers.facebook.com/docs/guides/mobile/#ios
This link has the step-by-step guide for implementing SSO, if you follow it carefully your App will request for permission once, then it will never request permission again (until the user remove your App)
Note that, you will need to go to www.facebook.com and use the "Developer Tools" to create a new Facebook App, you will be the admin of the app, and you will have your AppID, you will need this too.