Fb request dialog returns nil incorrectly - IOS Facebook - iphone

Thanks for reading!
I'm trying to create a Facebook request to enable the user to invite his friends to the app.
NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:nil];
[FBWebDialogs
presentRequestsDialogModallyWithSession:nil
message:#"Learn how to make your iOS apps social."
title:#"Test"
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
// Error launching the dialog or sending the request.
NSLog(#"Error sending request.");
} else {
if (result == FBWebDialogResultDialogNotCompleted) {
// User clicked the "x" icon
NSLog(#"User canceled request.");
} else {
// Handle the send request callback
NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
if (![urlParams valueForKey:#"request"]) {
// User clicked the Cancel button
NSLog(#"User canceled request.");
} else {
// User clicked the Send button
NSString *requestID = [urlParams valueForKey:#"request"];
NSLog(#"Request ID: %#", requestID);
}
}
}
}];
}
This is pretty much a clean copy of the Facebook docs code. And it works up to a point
The user can choose the friends and the request goes away and gets received by the friends and it shows up as a notification - everything fine so far.
The problem is that when I try to get the response from the "handler" the resultURL is nil and does not contain anything. And after that I get the log message "User canceled request".
Why don't I get anything back? The main reason that I need this is that I need to know which friends the request was sent to.
Thank you for any help!

Seems like you're passing an nil empty session to presentRequestsDialogModallyWithSession
Use FBSession.activeSession instead and check you have enough permissions to run the request (although if not, you would be getting another different error)

I think you are not having a session opened..please try to use this to open the session.
if (!FBSession.activeSession.isOpen) {
// if the session is closed, then we open it here, and establish a handler for state changes
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState state,
NSError *error) {
}];
}

Related

Invite Facebook friends through iPhone App using FBWebDialogs

I am using Facebook sdk 3.10 to send a request to multiple friend at a time using FBWebDialogs. Following code I'm using and all thing is fine like selecting multiple friends , sending them request. But there is one problem, is this FBWebDialogs uses some limit of friends as I have more that 300 friends but this is showing only 12-15 friends always.
CODE
[FBWebDialogs
presentRequestsDialogModallyWithSession:nil
message:#"Learn how to make your iOS apps social."
title:nil
parameters:nil
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
// Error launching the dialog or sending the request.
NSLog(#"Error sending request.");
} else {
if (result == FBWebDialogResultDialogNotCompleted) {
// User clicked the "x" icon
NSLog(#"User canceled request.");
} else {
// Handle the send request callback
NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
if (![urlParams valueForKey:#"request"]) {
// User clicked the Cancel button
NSLog(#"User canceled request.");
} else {
// User clicked the Send button
NSString *requestID = [urlParams valueForKey:#"request"];
NSLog(#"Request ID: %#", requestID);
}
}
}
}];
Using above I can see only max 12 friends in the dialog? Am I missing something?
Any help would be appreciated.
You should pass the NSDictionary type object to parameters argument.
You can create like this:
NSArray *suggestedFriends = [[NSArray alloc] initWithObjects:#"fb_id1", #"fb_id2", nil];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:[suggestedFriends componentsJoinedByString:#","], #"suggestions", nil];
Now
[FBWebDialogs
presentRequestsDialogModallyWithSession:nil
message:#"Learn how to make your iOS apps social."
title:nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
// Error launching the dialog or sending the request.
NSLog(#"Error sending request.");
} else {
if (result == FBWebDialogResultDialogNotCompleted) {
// User clicked the "x" icon
NSLog(#"User canceled request.");
} else {
// Handle the send request callback
NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
if (![urlParams valueForKey:#"request"]) {
// User clicked the Cancel button
NSLog(#"User canceled request.");
} else {
// User clicked the Send button
NSString *requestID = [urlParams valueForKey:#"request"];
NSLog(#"Request ID: %#", requestID);
}
}
}
}];
Here it will show all of the friends with the suggested Facebook ids.

Facebook share dialog

I got one problem in Facebook web-dialog which i am implementing in iPhone(ios7). After Facebook login whenever i click on share button it is not opening a Facebook web dialog. But after closing the application if i run again my application and click share button it is working perfectly. I don't know what is the problem. Please help me.
This is my share function
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:params
handler:
^(FBWebDialogResult result, NSURL *resultURL, NSError *error)
{
if (error) {
// Error launching the dialog or sending the request.
NSLog(#"Error sending request.");
} else {
if (result == FBWebDialogResultDialogNotCompleted) {
// User clicked the "x" icon
NSLog(#"User canceled request.");
} else {
// Handle the send request callback
NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
if (![urlParams valueForKey:#"request"]) {
// User clicked the Cancel button
NSLog(#"User canceled request.");
} else {
// User clicked the Send button
NSString *requestID = [urlParams valueForKey:#"request"];
NSLog(#"Request ID: %#", requestID);
}
}
}
}];

Always return Error FBErrorCategoryUserCancelled while login with facebook in my application

In my Application there are three ways of login. one of them is login with facebook. But when I click on facebook button it ask me for accesing permissions, when click OK then it returs error FBErrorCategoryUserCancelled. And this is not happening on every devices, it happens on some devices. Here is my code -
if ([[FBSession activeSession]isOpen]) {
/*
* if the current session has no publish permission we need to reauthorize
*/
if ([[[FBSession activeSession]permissions]indexOfObject:#"publish_actions"] == NSNotFound) {
[[FBSession activeSession] requestNewPublishPermissions:[NSArray arrayWithObject:#"publish_actions"] defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session,NSError *error){
[ProgressHUD dismiss];
self.view.userInteractionEnabled = YES;
}];
}else{
[self fetchUserDetails];
}
}else{
/*
* open a new session with publish permission
*/
[FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObject:#"publish_actions"]
defaultAudience:FBSessionDefaultAudienceOnlyMe
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
if (!error && status == FBSessionStateOpen) {
[self fetchUserDetails];
}else{
NSLog(#"error");
if ([FBErrorUtility shouldNotifyUserForError:error]) {
alertTitle = #"Facebook Error";
alertMessage = [FBErrorUtility userMessageForError:error];
// This code will handle session closures that happen outside of the app
// You can take a look at our error handling guide to know more about it
// https://developers.facebook.com/docs/ios/errors
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession) {
alertTitle = #"Session Error";
alertMessage = #"Your current session is no longer valid. Please log in again.";
// If the user has cancelled a login, we will do nothing.
// You can also choose to show the user a message if cancelling login will result in
// the user not being able to complete a task they had initiated in your app
// (like accessing FB-stored information or posting to Facebook)
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
NSLog(#"user cancelled login");
alertTitle = #"Facebook Error";
alertMessage = #"System login cancelled";
// For simplicity, this sample handles other errors with a generic message
// You can checkout our error handling guide for more detailed information
// https://developers.facebook.com/docs/ios/errors
} else {
alertTitle = #"Something went wrong";
alertMessage = #"Please try again later.";
NSLog(#"Unexpected error:%#", error);
}
[[[UIAlertView alloc] initWithTitle:alertTitle
message:alertMessage
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil] show];
[ProgressHUD dismiss];
self.view.userInteractionEnabled = YES;
}
}];
}
Any help will be appreciated.Thank you.
In my case it was because the facebook app's sandbox mode was still on, and therefore result in FBErrorCategoryUserCancelled for anyone who is not an administrator/developer/tester of the app.
To turn off sandbox mode on facebook, go to
https://developers.facebook.com/apps/YOUR_FACEBOOK_APP_ID/review-status/
See where it says "Do you want to make this app and all its live features available to the general public?", make it to be YES. (You might need to insert a contact email before you can access this option).
Or, if you don't want to make the app go public just yet, invite the tester to be an admin/developer/tester of the app. The person need to accept your invitation before he can use it.

Facebook invite friend request not received

I am try to invite friends to my app ,i am used Facebook SDK and FBWebDialogResult dialog to send request every thing is working fine it should return request send message. the code i am used is
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:nil];
[FBWebDialogs presentRequestsDialogModallyWithSession:nil message:#"Cariyuk"
title:#"Cariyuk"
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
// Case A: Error launching the dialog or sending request.
NSLog(#"Error sending request.");
} else {
if (result == FBWebDialogResultDialogNotCompleted) {
// Case B: User clicked the "x" icon
NSLog(#"User canceled request.");
} else {
NSLog(#"Request Sent.");
}
}}];
The problem with this is no request is received to receiver account.
Can anyone suggest me what is wrong with this ?

Facebook SDK 3.1 error when posting from iOS app

I'm facing the following problems with my iOS app:
While publishing to user's friend's wall, using iOS app I'm getting the error "com.facebook.sdk error 5".
The app is asking to login every time when I check for "FBSession.activeSession.isOpen".
Everything was working fine before. But since I've changed my app ID (as now I have to use different Facebook app). I'm getting the error.
I've done some research on this and found some solutions. Ive tried them all but nothing works.
I've written the following code to login to Facebook:
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
comingfromFB = YES;
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"friends_birthday",
nil];
return [FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:allowLoginUI
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
And the following code for publishing story:
if ([FBSession.activeSession.permissions
indexOfObject:#"publish_actions"] == NSNotFound) {
// No permissions found in session, ask for it
[FBSession.activeSession
reauthorizeWithPublishPermissions:
[NSArray arrayWithObject:#"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
// If permissions granted, publish the story
[self publishStory];
}
}];
} else {
// If permissions present, publish the story
[self publishStory];
}
When I run the app I get the following error:
Error: HTTP status code: 403
2013-02-05 14:36:47.109 <appname>[1705:c07] Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0xaa9af20 {com.facebook.sdk:ParsedJSONResponseKey={
body = {
error = {
code = 200;
message = "(#200) The user hasn't authorized the application to perform this action";
type = OAuthException;
};
};
code = 403;
}, com.facebook.sdk:HTTPStatusCode=403}
Please help me to understand where I'm going wrong.
I've also added some key-value pair to NSUserDefaults. Could that be a possible reason for that?
Thanks.
When you're first showing your view with post button, perhaps you want to check if the session is open and set your post button title according to session state. Assuming your button initially has "Post" title:
[FBSession openActiveSessionWithAllowLoginUI: NO];
if (![FBSession.activeSession isOpen])
{
[self setSendButtonTitle:NSLocalizedString(#"Log in",#"")];
}
Then add the following code to your Post button action:
- (void) send: (UIButton*) sender
{
if (![FBSession.activeSession isOpen])
{
[FBSession openActiveSessionWithPublishPermissions: [NSArray arrayWithObjects: #"publish_stream", nil]
defaultAudience: FBSessionDefaultAudienceEveryone
allowLoginUI: YES
completionHandler: ^(FBSession *session,
FBSessionState status,
NSError *error)
{
if (error)
{
NSLog(#"error");
}
else
{
[FBSession setActiveSession:session];
[self setSendButtonTitle: NSLocalizedString(#"Post",#"")];
}
}];
return;
}
// here comes the code you use for sending the message
}
So if the user presses send and the app is not authorized, it will perform a login operation and change your Post button title from "Log in" to "Post". Then the user will be able to post the message by pressing the button once again.
Hope it helps