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.
Related
I'm coding an iOS app in Objective C. I want the terms and conditions to appear when a user opens the app. I'm going to have a "don't show these again" button, which is all figured out using NSUserDefaults. What I can't figure out is how to make the terms and conditions disappear if the user hits "agree" (termsPressed) but NOT "don't show these again" (neverAgainPressed). I can hide them using button.hidden, but then as soon as the user returns to the main screen of the app the terms and conditions appear again, overlaying the main screen just as they did when the app first launched.
I've tried setting an NSUserDefault when "agree" is pressed, then resetting it when -applicationWillTerminate is called, but it appears that applicationWillTerminate is not called reliably when the app closes if it's closed from the background, and hence the user would never see the terms again even if they hadn't hit "don't show these again". Here's my code:
- (void)viewDidLoad {
// Hide all the terms and conditions elements if either "neverAgain" or "termsPressed" is true
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL neverAgain = [defaults boolForKey:#"neverAgain"];
if (neverAgain == TRUE){
_terms.hidden=YES;
_hideTerms.hidden=YES;
_background.hidden=YES;
_dontShow.hidden=YES;
}
BOOL termsPressed = [defaults boolForKey:#"termsPressed"];
if (termsPressed == TRUE){
_terms.hidden=YES;
_hideTerms.hidden=YES;
_background.hidden=YES;
_dontShow.hidden=YES;
}
NSLog(#"View loaded. termsPressed = %i", termsPressed);
NSLog(#"View loaded. neverAgain = %i", neverAgain);
[super viewDidLoad];
}
- (IBAction)neverAgainPressed:(id)sender {
BOOL neverAgain = TRUE;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:neverAgain forKey:#"neverAgain"];
[defaults synchronize];
NSLog(#"Don't show terms again pressed, neverAgain set to TRUE");
}
- (IBAction)termsPressed:(id)sender {
_terms.hidden = YES;
_hideTerms.hidden = YES;
_background.hidden = YES;
_dontShow.hidden = YES;
BOOL termsPressed = TRUE;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:termsPressed forKey:#"termsPressed"];
[defaults synchronize];
NSLog(#"Terms accepted, termsPressed set to TRUE");
}
- (void)applicationWillTerminate:(UIApplication *)application {
BOOL termsPressed = FALSE;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:termsPressed forKey:#"termsPressed"];
[defaults synchronize];
NSLog(#"Application entering background, termsPressed reset to FALSE");
}
#end
If there was a method that was called every time an app quit, or a version of .hidden that lasted the whole time an app was running, I'd be all set. Let me know if you guys have any ideas.
If you want "don't show these again", why reset? You can show at first launch and after some time (30 days for example):
BOOL ranBefore = [[NSUserDefaults standardUserDefaults] boolForKey:#"kRanBefore"];
NSDate *lastDate = [[NSUserDefaults standardUserDefaults] objectForKey:#"kLastCloseDate"];
NSTimeInterval timeDiff = [[NSDate date] timeIntervalSinceDate:lastDate];
int days = timeDiff / 86400;
if ((!ranBefore) || (days > 30)) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"kRanBefore"];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:#"kLastCloseDate"];
... show something
}
or reser at
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
I'd like to prominently show a 'help' button when my meteor app is opened for the first time (on iOS). What's the best way to achieve this? That is, how can I easily confirm my meteor app is opened for the first time, after installing the app on iOS.
set a NSUserDefaults parameter called helpShown, check setting to know which display controller to display. set to true when help is clicked / dismissed
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// set here a bool value
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:#"showHelp"];
//...............
return YES;
}
//somewhere you add your help button
//......
//remove/hide help button
- (void)helpFinishAction:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:#"showHelp"]) {
[defaults setBool:NO forKey:#"showHelp"];
NSLog(#"remove/hide you help button here");
//remove/hide you help button here
}
}
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];
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)
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