I have a problem when I send a post to my wall from my iPhone app.
I have all the delegate methods implemented and the method:
-(void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
// Keep this just for testing purposes.
NSLog(#"received response");
}
My app works, the post appears on my wall, but my app doesn't get any response and stucks at my activity indicator. Nothing appears in the console from NSLog.
Does any one know, where's the problem? I'm dying here :-/
EDIT:
- (IBAction)fbButtonPressed:(id)sender {
// Create the parameters dictionary that will keep the data that will be posted.
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"330334280387192", #"app_id",
nil];
// Publish.
[self.facebook dialog:#"feed" andParams:params andDelegate:self];
}
I'm using the feed dialog for user input
Related
I'm new to iOS development, I have successfully integrated facebook login and such... however my problem is with the Score api. i can read the score but i cant seem to post it, i have the publish_actions permission and also am getting back a valid access_token.Not sure what the problem it, here is my code -
NSString *accessTokenToUse = [NSString stringWithFormat:#"%#",[self.facebook accessToken] ];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
//#"233", #"score",
accessTokenToUse, #"access_token",
nil];
NSMutableDictionary *paramsB = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"233", #"score",
accessTokenToUse, #"access_token",
nil];
NSLog(#"APP ACCESS TOKEN: %#",accessTokenToUse);
[self.facebook requestWithGraphPath:#"me/scores" andParams:paramsB andHttpMethod:#"POST" andDelegate:self];
[self.facebook requestWithGraphPath:#"APP_ID/scores" andParams:params andHttpMethod:#"GET" andDelegate:self];
***EDIT - Still doesn't work
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"233", #"score",
#"[APP_ACCESS_TOKEN (omitted for stackoverflow)]", #"access_token", nil];
[self.facebook requestWithGraphPath:#"/[userID]/scores" andParams:params andHttpMethod:#"POST" andDelegate:self];
EDIT - SOLVED
So bascially i WAS passing in the correct token, however the facebook sdk was overwriting it in Facebook.m to the user's access token. So the fix was simple - whenever i need to pass in the App's token i just add an other parameter < key=#"useAppToken" Value=#"yes" > and in Facebook.m just add an if statement within isSessionValid -
- (FBRequest*)openUrl:(NSString *)url
params:(NSMutableDictionary *)params
httpMethod:(NSString *)httpMethod
delegate:(id<FBRequestDelegate>)delegate {
NSLog(#"PARAMS BFORE: %# ", params);
[params setValue:#"json" forKey:#"format"];
[params setValue:kSDK forKey:#"sdk"];
[params setValue:kSDKVersion forKey:#"sdk_version"];
if ([self isSessionValid]) {
if ([params objectForKey:#"useAppToken"] == nil || [params objectForKey:#"useAppToken"] == #"no") {
[params setValue:self.accessToken forKey:#"access_token"];
}
}
NSLog(#"PARAMS AFTER: %# ", params);
[self extendAccessTokenIfNeeded];
FBRequest* _request = [FBRequest getRequestWithParams:params
httpMethod:httpMethod
delegate:delegate
requestURL:url];
[_requests addObject:_request];
[_request addObserver:self forKeyPath:requestFinishedKeyPath options:0 context:finishedContext];
[_request connect];
return _request;
}
Be sure to comment out the NSLogs in this... :)
Scores can only be published using application access_token and not one for user (which you probably using, it's ok for reading the scores but don't allow you to publish 'em).
Citing the Scores documentaion:
Create or update a score for a user
You can post a score or a user by issuing an HTTP POST request to /USER_ID/scores with the app access_token as long as you have the publish_actions permission.
Looks like it's a bug:
http://developers.facebook.com/bugs/461900043821056
But if you change your app settings to web app instead of native it would work.
I'm trying to use the Facebook iOS "feed" dialog call to allow my app's user to share something on their Facebook wall. When the facebook app is not installed, it attempts to let them authenticate within the app (presumably using a web view). The issue is that this dialog just disappears once they authenticate. I was expecting the web view to return to the "feed" sharing view.
How do I detect that they authenticated so that I can re-open the feed dialog?
I've added fbDidLogin to my app delegate, but it's not being called. (I wasn't sure if this would normally be called or not, but I read several people recommending this.)
SBJSON *jsonWriter = [SBJSON new];
// The action links to be shown with the post in the feed
NSArray* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
#"More Videos",#"name",#"http://www.example.com/",#"link", nil], nil];
NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Test Caption", #"caption",
#"Test Description", #"description",
#"https://s3.amazonaws.com/example/images/test.png",
#"source",
self.video.blogLink, #"link",
#"01234567890123", #"app_id",
actionLinksStr, #"actions",
nil];
[delegate facebook].sessionDelegate = delegate;
[[delegate facebook] dialog:#"feed" andParams:params andDelegate:self];
It turns out the issue is the same as those reporting that the dialog disappears immediately for them. There is a bug in the Facebook SDK in regards to handling error -999, with the solution described in this comment on SO.
I finally realized I was having the same issue (albeit manifesting slightly differently), when I started listening to the FBDialogDelegate's method:
- (void)dialog:(FBDialog*)dialog didFailWithError:(NSError *)error
The error description referenced the -999 error mentioned above, leading me to the answer. Luckily the code checked into github worked flawlessly. I just had to pull it into the SDK codebase and recompile the .a lib file, then include it into my project.
Did you implemented facebook protocol FBSession in your view controller ?
To detect that user is authenticated you have to call isSessionValid:
if (![[self.fbAppDelegate facebook] isSessionValid]) {
NSArray *permissions = [[NSArray alloc] initWithObjects:#"publish_stream",#"create_event",#"offline_access", nil];
[[self.fbAppDelegate facebook] authorize:permissions];
}
and authorize inApp:
[self authorizeWithFBAppAuth:NO safariAuth:NO];
Thanks for reading.
I went through Facebook iOS SDK Feed Dialog issue after authentication but in vain.
What works:
I created a sample "Hello, World" app using the Facebook iOS Tutorial with the Feed Dialog. This tutorial works fine and I am able to see the post in my feed.
Problem:
When I integrated this code in my app, I don't get to see the Feed Dialog after the authentication when the control reaches back to my app.
Flow of control:
I have a UIImagePickerController showing a camera that takes a picture, then shows UIAlertView to indicate that the image is being uploaded, shows a UIAlertView to display the result returned from server, and then finally shows a UIActionSheet to display the different sharing options (share to Facebook, Twitter, etc.).
When the user hits "Share to Facebook", the following selector gets invoked:
- (void) initFacebook
{
//Init Facebook
facebook = [[Facebook alloc] initWithAppId:#"308136969223987" andDelegate:self];
//Check for access_token
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];
}
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];
[_facebook dialog:#"feed" andParams:params andDelegate:self];
}
This launches the Facebook app to authenticate, and then opens my app again with the UIImagePickerController but doesn't show the Feed Dialog.
Can someone please help me out?
You have to manually call the method you want again, in your case -(void)initFacebook, in the -(void)fbDidLogin of the FBSessionDelegate.
In addition, to make this more generic, a little trick I did after [facebook authorize:nil]; I set a pending target and selector to store the pending command that needs to be executed after login. Something like this...
id fbPendingTarget;
SEL fbPendingAction;
if (![delegate.facebook isSessionValid]) {
[delegate.facebook authorize:nil]; // Facebook app or Safari login, mustitask
// Save pending actions
delegate.fbPendingTarget = self;
delegate.fbPendingAction = #selector(facebookButtonPressed);
[self retain]; // *** hold on to self if you will release self and is using fbPendingTarget as self
}
and when the delegate fires, it runs your pending action ...
- (void)fbDidLogin {
[self storeAuthData:[facebook accessToken] expiresAt:[facebook expirationDate]];
[self apiFQLIMe]; // request user info
// perform any pending actions after login
if ([fbPendingTarget respondsToSelector:fbPendingAction]) {
[fbPendingTarget performSelector:fbPendingAction];
}
self.fbPendingTarget = nil;
self.fbPendingAction = nil;
[self release]; // *** let go of self after we finished everything
}
This help executing any pending actions you need after login. And don't forget to clear them after executing or login failed.
-(void)fbDidNotLogin:(BOOL)cancelled {
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:#"Cannot Connect"
message:#"There was an error while connecting to Facebook. Please try Again."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil] autorelease];
[alertView show];
self.fbPendingTarget = nil;
self.fbPendingAction = nil;
[self release]; // *** let go of self after we finished everything
}
Hope this helps :)
Edit: I find it better to set fbPendingTarget to some object that exists throughout the app lifetime (like appDelegate) rather than setting to self. This is because in many cases your self is a view that will be released right after the user sends a post to Facebook. Sending a message to a released object will crash your app.
A dirty way to get around this is to explicitly use [self retain] before sending and then [self release] in your fbPendingAction. But that would retain everything else that should be released in self by that time. I just find it easier to set to appDelegate since most action you will do is just displaying an alert anyway. I'll edit the code just for safety sake.
I am trying to extract user's basic information through FBConnect latest SDK. My code is simple:
- (void)viewDidLoad {
[super viewDidLoad];
facebook = [[Facebook alloc] initWithAppId:fbAppId];
NSArray* permissions = [[NSArray arrayWithObjects:#"user_about_me",#"read_stream",nil]retain];
[facebook authorize:permissions delegate:self];
[facebook requestWithGraphPath:#"me" andDelegate:self];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
fbAppId, #"app_id",
[NSString stringWithFormat: #"Some Text"], #"description",
#"My Test App", #"name",
#"Test Facebook Graph API",#"message",
nil];
[facebook dialog:#"feed" andParams:params andDelegate:self];
}
- (BOOL) application: (UIApplication*) application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
- (void) fbDidLogin {
NSLog(#"FB didLogin");
}
- (void) fbDidNotLogin:(BOOL)cancelled {
NSLog(#"FB didNotLogin");
}
- (void) request:(FBRequest *)request didLoad:(id)result {
NSLog(#"request-didLoad-result");
}
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
NSLog(#"received response");
}
Up till this point everything goes well apparently. Publish a feed through dialog on user's wall works fine. The problem occurs when I try to get user's information like name with:
[facebook requestWithGraphPath:#"me" andDelegate:self];
But neither fbDidLogin nor requestDidLoad is called. For requestDidLoad, I checked didLoadRawResponse as it is called before request didLoad:
- (void)request:(FBRequest *)request didLoadRawResponse:(NSData*)data {
NSString *response = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"Response is = %#",response);
[response release];
}
What I get in response is the following authentication error:
{"error":{"type":"OAuthException","message":"An active access token must be used to query information about the current user."}}
What is the reason and the solution?
I have added my whole code in the following wiki
How to retrieve Facebook response using Facebook iOS SDK
Hope this will help you, this is a working code if you have any question please let me know.
I am using Graph API to get facebook user information as follows:
[facebook requestWithGraphPath:#"me" andDelegate:self];
How can I do this? I know that server response is in the form of JSON object but don't know how to parse it.
I don't find the links that helped me to resolve this issue.
This is the code I used (request:didLoad: is a FBRequestDelegate callback):
- (void)request:(FBRequest *)request didLoad:(id)result {
if ([result isKindOfClass:[NSDictionary class]]) {
NSDictionary* hash = result;
NSString *username = (NSString*)[hash valueForKey:#"name"];
[[NSUserDefaults standardUserDefaults] setObject:username forKey:#"Username"];
}
[self publishStream]; // publishStream is a custom methods of mine to create the post to publish
};
I remember that I read the FB ios api code and maybe this and this
I hope this works for you.
Bye,
Fran
I have answered this question here How to retrieve Facebook response using Facebook iOS SDK