ios5 - posting to facebook when facebook app is already on device - ios

I'm following the information provided in facebook url. Its working well and good if facebook app is not there on the device.
consider facebook app is not on device: i'm authorizing facebook and its opening safari. i authorize the app, comes back to ios app and it calls the delegate "fbDidLogin" in FBSessionDelegate. I'm saving the access token and expiration date as mentioned in above URL. Then later, I'm able to post it facebook. This works fine.
now, facebook app is available on the device and logged in also: when i call "authorize", it opens facebook app and authorizes the app, then comes back to the ios app. but its not calling "fbDidLogin" delegate method. how should I recognize or catch the access token and expiration date?
I'm writing the following code:
facebook = [[Facebook alloc] initWithAppId:FB_APPID andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"])
{
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
NSArray* permissions = [NSArray arrayWithObjects:#"publish_stream",nil];
if (![facebook isSessionValid])
{
[facebook authorize:permissions];
}
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults setBool:YES forKey:#"IIRY_FACEBOOK"];
[defaults synchronize];
}
Can some one point me the mistake that I'm doing?

Actually when you tap on authorize then it will open safari and if there is facebook app on device then it will open facebook app otherwise it will open facebook in safari. If you don't want to open the safari and facebook app then in facebook.m change this statement everywhere where it is calling to NO.
[self authorizeWithFBAppAuth:NO safariAuth:NO];

Found the solution. The problem is that the bundle ID mentioned in facebook app and my app's bundle Id are not same and so its not working.
Look the URL to know more

Related

Implementing sign in with google (Google oauth) IOS

I have implemented sign in with google i my project, now i don't want user should allow access every time he logs in the project. it should ask for first time only.
I have seen so many solution but don't have any idea how to implement. Please Any body help me
Keep a check at start.
So on your checkViewController viewDidLoad
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
if([defaults boolForKey:#"didLoginGoogle"]) //This returns false by default
{
//Open the page/code for google login
}
else
{
//Open your home page of the app without login
}
Also, once you are done with google login, do a -->
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:true forKey:#"didLoginGoogle"];
[defaults synchronize];
And that it! Using defaults you store the state of your app and then navigate accordingly.

When I call facebook authorize:permissions it keeps asking to allow the application

I am using the facebook-ios-sdk. I am basically getting facebook from the appDelegate and everytime It hits my viewController it calls this method:
- (void)openFacebook{
appDelegate.facebook = [[Facebook alloc] initWithAppId:#"11234567892515" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
appDelegate.facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
appDelegate.facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![appDelegate.facebook isSessionValid]) {
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"user_likes",
#"read_stream",
#"user_photos",
//#"user_about_me",
//#"email",
#"publish_stream",
#"publish_actions",
nil];
[appDelegate.facebook authorize:permissions];
NSLog(#"Facebook session is valid");
[appDelegate.facebook extendAccessTokenIfNeeded];
}
}
The problem is.... even if the app is allowed it still comes up with dialog to allow the application...
How can I stop it from popping up everytime asking me to authorize the app even though I hav e already authorized it? Please keep it simple I am no genius thanks :).
My guess would be that you aren't storing the AccessToken and ExpiryDate once you've logged in.
Add this to see if you have these stores in UserDefaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog(#"%#", [defaults dictionaryRepresentation]);
In your Facebook fbDidLogin method do you have like this?
// Store session info.
[[NSUserDefaults standardUserDefaults] setObject:_facebook.accessToken forKey:#"FBAccessTokenKey"];
[[NSUserDefaults standardUserDefaults] setObject:_facebook.expirationDate forKey:#"FBExpirationDateKey"];
[[NSUserDefaults standardUserDefaults] synchronize];

Facebook API for IOS

I am including Facebook API for IOS in my app to share the link from my app in facebook. When I click "Share" from my app, It shares the link when I already logged in facebook in my device. If not it asks me to login. Once I logged in, the window closed and its not shared that link. i need to click "Share" again to share link. What should I need to share automatically after I logged in.
Pls Help me.
Did you use the Facebook SDK for iOS (https://developers.facebook.com/docs/mobile/ios/build/)? I scrupulously followed those instructions and it worked well for me on the first try. The only trick for me was the much discussed solution to make sure the app didn't launch Safari for authentication when people didn't have the Facebook app by replacing:
[self authorizeWithFBAppAuth:YES safariAuth:YES];
with
[self authorizeWithFBAppAuth:YES safariAuth:NO];
in the (void)authorize:(NSArray *)permissions method in Facebook.h. But other than that, I just followed the instructions on that above web site and it worked seamlessly for me.
If this doesn't answer your question, perhaps you can post some code so we can help diagnose what's going on. The question (in the absence of code) is a little ambiguous.
See my question if you have any doubt with facebook API integration.
Facebook API dialog page showing error in second time and after that
Try this 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:
#"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];
}
If your app is not re-launching your app after sign-in (from browser or if the user has installed the Facebook app) then you have not added your FB App Id to your Info.plist (which is how your app gets launched)
See the documentation here go to section Modify the app property list file:
... Create a new row named URL types with a single item, URL Schemes,
containing a single value, fbYOUR_APP_ID (the literal characters fb
followed by your app ID). The following shows exactly how the row
should appear in the .plist file...
Eg:
URL Types.[0].URL Schemes.[0].fb1234 (where your Facebook app id is 1234)

Facebook Pop up issue - iOS

I am facing a strange issue.
When after entering correct username and password. If I press the login button twice(with the interval of 2 secs) then the popup disappears. Though when I tap on FB button again It is already logged in with share screen.
If I press it just once then its working fine.
Expected:
It takes me to the share screen in the popup after successful login.
Another issue is sometimes my share for the first time after entering username and password does not work.
Though out of 10 it works for 9 times but sometimes after fresh install it just does not post to FB wall.
Any kind of suggestion will really be appreciated.
Try this code, which perfectly works for me now.
-(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:
#"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];
}
Don't forget to add YOUR APP ID.

Facebook SDK iOS login with Facebook app installed

UPDATE: I've realized that the below problem only occurs if the user has already given Facebook access to my app, and then reinstals my app. The first time the user selects the Facebook button in my app, it goes to the Facebook app to get authentication, but since the authentication status is still saved in the Facebook app, it returns immediately to my app without explanation to the user, and the postToFacebookWall method fails.
UPDATE 2: I ended up implementing the solution found here: How to authorize user through a DIALOG with the NEW Facebook Connect iOS API? and avoided the Facebook app. Safari is better because at least the user gets a message saying that they will be logging in. However the Feed Dialogue still fails the first time as before.
I'm having what must be a common problem, but I haven't been able to find the solution yet. Note that my app is restricted to iOS 5 and above.
I have an app with a Facebook button for posting to a users wall. When the button is pressed, the app will check to see if the user has already given permission to the app to post to the wall. If they have, a view pops up with a Feed Dialog with the wall post information. If they haven't, Safari will be opened and the user will be allowed to login. After logging in they will be taken back to the app and the Feed Dialog view will pop up.
This works on the Simulator, and on my device, if the Facebook app is not installed. If the Facebook app is installed, the user is taken to the Facebook app, but then immediately returned to the original app, without allowing the user to do anything. The Feed Dialog pops up, but it is completely blank and dismisses automatically. Pressing the Facebook button again will bring up the Feed Dialog view as it is supposed to once the user has given permission to the app to post to their wall.
I pretty much followed the instructions found here:
http://developers.facebook.com/docs/mobile/ios/build/#implementsso
and here:
http://developers.facebook.com/docs/reference/dialogs/feed/
In the AppDelegate I have the following code:
- (void)loginToFacebook
{
// Set up facebook
self.facebook = [[Facebook alloc] initWithAppId:#"YOUR_APP_ID" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
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:nil];
}
}
// This is an FBSessionDelegate protocol method
// that gets called after a successful login
- (void)fbDidLogin
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[self.facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[self.facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:#"POST_TO_FACEBOOK_WALL" object:nil];
}
// This is a UIApplicationDelegate protocol method
// It is called when safari is dismissed
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [self.facebook handleOpenURL:url];
}
The only difference between this and the sample code is that I added an NSNotification that is picked up by my view controller, where I have the following code:
- (void)facebookButtonPressed:(UIControl*)sender
{
Facebook *facebook = [(AppDelegate*)[[UIApplication sharedApplication] delegate] facebook];
if (![facebook isSessionValid])
{
// Login to facebook if not already logged in
[(AppDelegate*)[[UIApplication sharedApplication] delegate] loginToFacebook];
}
else
{
[self postToFacebookWall];
}
}
- (void)postToFacebookWall
{
Facebook *facebook = [(AppDelegate*)[[UIApplication sharedApplication] delegate] facebook];
/*Facebook Application ID*/
// TODO: Get this either from the AppDelegate facebook property or the info plist
NSString *client_id = #"YOUR_APP_ID";
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
kAppId, #"app_id",
#"http://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",
nil];
[facebook dialog:#"feed" andParams:params andDelegate:self];
}
Does anyone know how to fix my issue? (Also, in case it isn't clear, YOUR_APP_ID is replaced with my app id in my code).
This sounds like an out-case in that it is this scenario of 'only when the app was installed, authed, then uninstalled, then reinstalled'.
You should be able to fall-back to "classic web view authentication" when this particular thing happens.
if (facebookAppDidNotReturnProperly) {
// therefore I must have been re-installed
// fallback to UIWebView
[facebook authorizeWithFBAppAuth:NO safariAuth:NO];
}
Then again, you could also use safari auth too I suppose.
I ended up solving my questions by implementing the two solutions found here:
https://stackoverflow.com/a/5683454/472344
https://stackoverflow.com/a/9137887/472344

Resources