I am using Old facebook sdk for implemnt facebook app on my application.
This is my code
facebook = [[Facebook alloc] initWithAppId:#"!!!!!!!"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
// [defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
NSString *accessTokenString = [defaults objectForKey:#"FBAccessTokenKey"];
NSDate *expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
if([accessTokenString length]>0 && expirationDate != nil)
{
accessToken =accessTokenString;
_expirationDate =expirationDate;
hasFBLoggedInBefore=YES;
}
else
{
hasFBLoggedInBefore = NO;
}
NSArray *permissions = [NSArray arrayWithObjects: #"offline_access",#"publish_actions",#"email",#"publish_stream",nil];
[facebook authorize:permissions delegate:self];
[facebook authorize:permissions delegate:self];
Sharing and all those things are fine..Now am facing problem..Suppose Ios6 device have no facebook application is installed then safari is using for loading sdk..if Ios6 have facebook application then sdk is open through native app.Then i am trying to login sdk through native app ,it goes to my home page(Logined user time line) not goes to permission and allthose things..what i am missing?
Related
I use the facebook-ios-sdk for my iPhone app and launch the authorization screen when the user push a button. But i always get a different authorization screen. I enabled the "enhanced auth dialog" in the advanced settings...
Other apps use this one:
facebook = [[Facebook alloc] initWithAppId:#"167080433419201" 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]) {
NSArray *permissions = [[NSArray alloc] initWithObjects:#"user_about_me",nil];
[facebook authorize:permissions];
}
Check the permissions that you set while authorizing the facebook.
this is just an example
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"user_likes",
#"read_stream",
nil];
[facebook authorize:permissions];
you can find more permission types here:https://developers.facebook.com/docs/authentication/permissions/
OAuth requires user to login to get granted permissions if there are no permissions in their app privacy list.
After login and allowing the app permissions request, we can get access_token from the redirected url.
So your situation is you must login first and your ios test app may already exist on the facebbook app privacy list.
The second screen had already been loged in, but the first screen was not.
You should adopt the FBSessionDelegate to store the access_token & expiration date.
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
I found the solution, i tried different permissions. But the authorization screen takes a while before it changed to the right one...
I have recently added Facebook integration to my app, and everything works fine except when the user relaunches the app. Each time the app is restarted, Facebook has to go back through its authorization process. This involves switching out of the app to Safari/Facebook, then back to my app. How can I make Facebook save the info, or be able to get blanket permissions for my app so that I doesn't constantly reauthorize?
Here is my code from the applicationDidFinishLaunchingWithOptions: method:
facebook = [[Facebook alloc] initWithAppId:#"203604286395694" 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];
}
Relevant Facebook delegate methods:
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
- (void) requestDialogButtonClicked {
NSMutableDictionary* params =
[NSMutableDictionary dictionaryWithObjectsAndKeys:
#"invites you to check out cool stuff", #"message",
#"Check this out", #"notification_text",
nil];
[facebook dialog:#"apprequests"
andParams:params
andDelegate:self];
}
- (void)dialogDidComplete:(FBDialog *)dialog {
NSLog(#"dialog completed successfully");
}
Are you calling Facebook authorize: somewhere? That's how the fbDidLogin will be triggered. Assuming you are, the behavior indicates that either the token or the date (or both) is nil, or that the date is past expiration (unlikely).
Log the values upon writing and reading the user defaults. My guess is that the authorize either wasn't called, or failed (due to app key or some other problem not shown in the code), leaving the fbDidLogin method uncalled or saving nil values.
That's because you are not asking for permissions when you authorize your app .. "offline_access" provide a long term access token so you will not need to get a new access token every time the user wants to use the Facebook .. so what you need to authorize your app like this
if (![facebook isSessionValid]) {
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"user_likes",
#"read_stream",
#"offline_access"
nil];
[facebook authorize:permissions];
[permissions release];
}
Edit :
Please check the following in your implementation :
1.you need to add the following functions in your application delegate(the Facebook object here is your instance value of FaceBook Class).
// 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];
}
2.In your info.plist add to URL types > URL Schemes > your Facebook app ID with fb prefix (finally your value will be like this for ex. fb313714***).
I am executing the following code to logout of Facebook from my iPhone application:
if ([mFacebook isSessionValid]) {
[mFacebook logout];
}
This code runs successfully and the delegate is called after this in which I am clearing the access token:
- (void)fbDidLogout
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:#"FBAccessTokenKey"];
[defaults removeObjectForKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
But when I again log in to Facebook, it does not ask for username and password.
What am I doing wrong?
Are you using FB mobile Login Dialog? In Facebook.m, add the following code to remove cookies at m.facebook.com domain.
- (void)invalidateSession {
...
NSArray* facebookMCookies = [cookies cookiesForURL:
[NSURL URLWithString:#"https://m.facebook.com"]];
for (NSHTTPCookie* cookie in facebookMCookies) {
[cookies deleteCookie:cookie];
}
...
}
Try adding these at the end of your fbDidLogout block:
mFacebook.accessToken = nil;
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
NSString* domainName = [cookie domain];
NSRange domainRange = [domainName rangeOfString:#"facebook"];
if(domainRange.length > 0)
{
[storage deleteCookie:cookie];
}
}
Just add this line to fbDidLogout method
[mFacebook invalidateSession];
This will surely make you logout from Facebook.
Try this code:
- (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];
}
}
Don't forget to create an instance for facebook.h in your AppDelegate.
Try to log off from the Facebook application. It might just take the token from the Facebook application.
I am using Facebook Connect with my app. Everything is set up, all the code WAS working. Around yesterday, I could login with my app, then post stuff and other functions. I don't recall making any real changes. Now, as usual, whenever I press login and it goes to a Safari page where I have to confirm that I want to use this app and it says who I am logging in as. If I press ok it goes back to the app. During this whole process, in the app, Facebook never logs in. REMEMBER: JUST YESTERDAY THIS WORKS, I DONT RECALL MAKING ANY CHANGES! Here is how the connection starts:
-(IBAction)connectToFacebook {
[loginButton setImage:[UIImage imageNamed:#"LoginWithFacebookPressed#2x.png"] forState:UIControlStateNormal];
facebook = [[Facebook alloc] initWithAppId:#"387500177929927" 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]) {
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"user_likes",
#"read_stream",
nil];
[facebook authorize:permissions];
[permissions release];
}
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(makeVisibleButtons) userInfo:nil repeats:NO];
}
So I press a button to call this IBAction. The NSTimer is not related, its just for the appearance of some buttons. Now, these two methods never get called (when I log it):
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [self.facebook handleOpenURL:url];
NSLog(#"handleOpenURL");
}
- (void)fbDidLogin {
NSLog(#"log in");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
I don't understand why. Especially if it was working before. Do you have any ideas?
Facebook API having SSO(Single Sign On). So It will ask the user to login for only one time. This login is valid till that login tokens are expired. Check your code itself that you are storing your token and expiration time in NSUserDefaults. This will help the API to login again. If you want to make sure your app login functionality is working or not, then delete your app from simulator and again run it. It will ask your login credentials for the first time only. Hope you undertand.
I apologize if this is stupid question but I just started trying to incorporate the FB SDK into my app today.
I followed the instructions online and got to the point where they suggested testing the app. The FB app opened as expected and asked permission as expected. However when I accepted the FB app stayed open instead of going back to my app.
I think it may have to do with the access token keys. I found online how to get this key, but should I enter it in the code shown below? What about the expiration date key? Or are these generated dynamically?
facebook = [[Facebook alloc] initWithAppId:#"##########" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
Thanks!
if (![facebook isSessionValid]) {
[facebook authorize:nil];
}
did you add the custom URL scheme to your app plist?
you need to add fb[FB_APP_ID] as a custom URL scheme
it's in their instructions