I am working on iphone facebook application. I am going through facebook connect tutorial on developer.facebbok.com. I did perfectly what mentioned in tutorial. But as per the tutorial there are log in and cancel button showing and in my application only "Okay" button showing on the right side of top bar. What is wrong happening here?
Flow is like this.
1. Tap on tableview cell
2. Open facebookcontroller.
3. In view did load method of facebookcontroller I added tutorial code.
MyConnectViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
facebook = [[Facebook alloc]initWithAppId:#"353076241375240" 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];
}
// Do any additional setup after loading the view, typically from a nib.
}
- (void) fbDidLogout {
// Remove saved authorization information if it exists
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]) {
[defaults removeObjectForKey:#"FBAccessTokenKey"];
[defaults removeObjectForKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
}
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
MyConnectAppDelegate.m
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [[_viewController facebook] handleOpenURL:url];
}
I want to start from login screen of facebook.
The Okay button appears when you are logged in but not authorised.
Try resetting the simulator or logging out Facebook then try again.
Use sharekit and make your life a lot easier :-)
happy coding!!
Related
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?
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!
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];
}
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];
}
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