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)
Related
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.
I have an iPhone app that allows users to record videos and I'd like them to be able to share those videos on Facebook.
However, unless the user decides to share, I don't want him/her to be redirected to Facebook. I've tried the method provided in the Facebook iOS tutorial, and it requires the user to be redirected to Facebook and authenticate as soon as the app starts up.
That's unnecessary.
What I'd like to do is have a "Share" button that allows the user to authenticate and then automatically upload a video right afterward using the POST request.
Is this possible? Has anyone achieved a similar effect?
Thanks.
I'm not sure which tutorial you're looking at, but it probably has you authenticate at launch just as a demo.
In your app, you can authorize you app with something like this after the video is saved
// Share button action
- (IBAction)sharePressed {
// If user is already authenticated
if ([facebook isSessionValid]) {
[self shareLinkToFacebook];
} else {
// Authenticate with just email permissions
NSArray* permissions = [NSArray arrayWithObjects:
#"email", nil];
[facebook setSessionDelegate:self];
[facebook authorize:permissions];
}
}
- (void) shareLinkToFacebook {
// Create a simple post
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setValue:[video url] forKey:#"link"];
[params setValue:[video title] forKey:#"name"];
[params setValue:#"description" forKey:#"description"];
[facebook dialog:#"feed" andParams:params andDelegate:self];
}
// FBSessionDelegate
- (void) fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:kFBAccessToken];
[defaults setObject:[facebook expirationDate] forKey:kFBExpirationDateKey];
[defaults synchronize];
[self shareLinkToFacebook];
}
You're going to have to authenticate with facebook to get an access key- there's no way around that.
You cannot do what you want. To publish something, you must have a valid access_token. the only way to achieve this is to follow the login procedure that you saw in the official example.
The answer to this question lies in creating a singleton that operates as both the FBSessionDelegate and the FBRequestDelegate, and allows you to pass one instance of Facebook around your program.
I called my singleton FacebookVideoUploader, and in my app delegate I called:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [[[FacebookVideoUploader sharedInstance] facebook] handleOpenURL:url];
That's it. This keeps the app delegate uncluttered and let's the singleton handle the delegate methods.
In my implementation file for my view controller that handles the video upload I put:
(IBAction)shareWithFacebook:(UIButton *)sender {
[[FacebookVideoUploader sharedInstance] postVideoToFacebook];
}
That's it. All of the delegate and session methods as well as postVideoToFacebook are handled in the FacebookVideoUploader singleton.
This allows my users to log into my app without being redirected to Facebook and lets them authenticate as soon as they decide they want to share a video.
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.
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
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