I just started using facebook sdk and slightly confused with it documentation. It's well written, but very often does not correspond to current SDK version. So i'm not found what to do with this situation:
I request publish permissions when firstly require them, with the next code:
[[FBSession activeSession] requestNewPublishPermissions:permissions
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (error) {
NSLog(#"Permissions request error: %#", error.description);
}
completion(error == nil);
}];
I need only one publish permission - 'publish_actions', when i request it sdk redirects me to facebook app and immediately returns back to the my app without asking whether i'm agree to give this permission.
There are no errors in completionHandler, but through debug i found that this permission was saved in the variable _requestedReauthPermissions:
So, finally, permission i requested not granted to me.
How properly request publish permission? What could I miss? May it occur due to setup of facebook app?
Related
My app needs to send a private message to the user's Facebook friend. The user needs to grant the XMPP permission before my app can do this.
[[FBSession activeSession]
requestNewPublishPermissions:[NSArray arrayWithObjects:
#"publish_stream",
#"xmpp_login",
nil
]
defaultAudience:FBSessionDefaultAudienceEveryone
completionHandler:^(FBSession *session, NSError *error) {
}
];
The above code will pop up this alert view as expected:
However, even before tapping any button, this warning is printed out in the debug log:
FBSDKLog: FBSession: a permission request for publish or manage
permissions contains unexpected read permissions
Surely enough, after tapping the "OK" button, the completion handler comes back with an error, containing the same warning. I understand that the Facebook API requires asking for read and write permissions separately, but the xmpp_login permission seems to need both read and write at the same time. I'm at a dead end here.
iOS 7.1
Facebook SDK 3.12.0
Xcode 5.1
See the section Extended Permissions in this link.
According to this, xmpp_login is the read permission, not the write/publish permission. So you should add that the readPermission
Facebook integration in my app works well. I ask for *read_stream* permission at login, then for *publish_actions* when I want to publish anything for the first time (as recommended).
The issue is, that when I ask for *publish_actions* permission, app switches to Facebook app and back. I wouldn't like user to see Facebook app, and I know many apps that post to the wall silently.
How can I do this?
You should ask for publish permission on login, not when publish something for the first time.
Once the user is logged in with all needed permission, he won't see the fb app screen again (unless the session expired or is closed).
[FBSession openActiveSessionWithPublishPermissions:#[#"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
allowLoginUI:YES
completionHandler:aCompletionHandler];
if You did ask about public_action in the first time user login you not need it again. U need check active permission. If public_action found you not need call to get it a gain
// Ask for publish_actions permissions in context
if ([FBSession.activeSession.permissions indexOfObject:#"publish_actions"] == NSNotFound) {
// Permission hasn't been granted, so ask for publish_actions
[FBSession openActiveSessionWithPublishPermissions:#[#"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
if (FBSession.activeSession.isOpen && !error) {
// Publish the story if permission was granted
// write your code public here
}
}];
}
else {
// If permissions present, publish the story
// write your code public here
}
In the last SDK, read and publish permissions are separated. So, in my code, whenever the users logs in I am using this code:
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
I have seen in the facebook developer that when I want to publish, I should ask for publish permissions like this:
// can include any of the "publish" or "manage" permissions
NSArray *permissions =
[NSArray arrayWithObjects:#"publish_actions", nil];
[[FBSession activeSession] reauthorizeWithPublishPermissions:permissions
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
/* handle success + failure in block */
}];
Since this is my first app that I am using facebook integration, I want to ask a couple of things:
1) Can these sessions be open at the same time? I will ask for the publish permissions only when the user wants to publish, but should I do something else with the other session? Like close it first, and reopen it later or I should not worry about?
2) If I have a postToFriends button for example, my pseudocode for making it work would be like this, right?
- (IBAction)postToFriendaction:(id)sender {
if (!FBSession.activeSession.isOpen) {
/* code from the above for enabling publish permissions*/
}
or I should change !FBSession.activeSession.isOpen to something else, because no the user is logged in with read permission only, he never enters the if clause. Can you help me?
The two sessions you refer to are actually the same session (it's the "active" session that's statically available after a call to openActiveSession... is called). So you don't need to do anything with the other session.
You should have a look at either the Scrumptious or Hello Facebook sample apps that ship with the SDK. They both give examples on how to post (while asking for publish permissions).
I'm sure there's something simple I'm missing regarding this, but when using requestForUploadPhoto to send a photo to an album, it gets sent up with permission set to 'Only me'. Because the permission setting is so restrictive, photos then need to be manually approved by the user. Is there a way to alter the default upload permission (which will hopefully stop it requiring manual approval)? Normally it would be set when requesting a publish permission, but because user_photos is technically a read permission, I'm not given the chance to specify this. Code is below.
[FBSession openActiveSessionWithReadPermissions:[NSArray arrayWithObject:#"user_photos"] allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
FBRequest* request = [FBRequest requestForUploadPhoto:uploadImage];
FBRequestConnection* connection = [[FBRequestConnection alloc] init];
[connection addRequest:request completionHandler:^(FBRequestConnection* connection, id result, NSError* error)
{ NSLog(#"Done with upload"); }];
[connection start];
}];
I know it's not even slightly close in terms of catching and managing other session changes, but I'm not fussed with that atm - I just want to get this permission issue resolved before coding the rest.
user_photos only allows you to see photos (using GET), and does not allow you to post a photo. To do that, you need either publish_stream or publish_actions. Are you sure your app didn't already have a publish permission somehow?
You are correct in that the privacy is set when an app requests publish permissions, and the user can change that at any point later on as well (via the Facebook app or website).
I'm trying to get post permissions from a user using the Facebook SDK on iOS.
I'm calling the code below in a method that is called if the app does not have the required publishing permissions to post to the users facebook wall.
// No permissions found in session, ask for it
[FBSession.activeSession requestNewPublishPermissions: [NSArray arrayWithObject:#"publish_actions"]
defaultAudience: FBSessionDefaultAudienceEveryone
completionHandler: ^(FBSession *session, NSError *error)
{
if( !error )
{
// Do something
}
}];
The first time I call this code it takes the user to the permissions page, and before it even switches to safari on the device the block gets called and this error message is returned
Error Domain=com.facebook.sdk Code=2 "The operation couldn’t be completed. (com.facebook.sdk error 2.)" UserInfo=0xc426410 {com.facebook.sdk:ErrorLoginFailedReason=com.facebook.sdk:ErrorReauthorizeFailedReasonUserCancelled,
The app then continues on to show the permissions page in safari where the user selects ok. Then it returns to the app. Permissions have not been set at this point even tho the user was presented with the permissions page and accepted.
When trying to post a second time it takes the user to the permissions page in safari and the requestNewPublishPermissions method doesn't fail instantly. The user selects ok and then everything works as expected.
So it is only on the very first time calling requestNewPublishPermissions that it fails instantly returning the error ErrorReauthorizeFailedReasonUserCancelled.
This happens in the simulator and on the device.
Any idea what might be causing this?
I found the solution to this problem on the answer to this question Facebook iOS 3.1 sdk login with publish permission callbacks
dispatch_async(dispatch_get_current_queue(), ^{
[self openSessionForPublishPermissions];
});
Where opensessionforpublishpermissions is the method that contains the requestNewPublishPermissions method.
"The reason is that the call to reauthorize.. needs to be after the event loop of which openActiveSession.. is called."
I assume this is a bug in the Facebook SDK, it doesn't make sense for this to be normal behaviour and I haven't seen any of the Facebook docs comment on this being the expected behaviour.
I had the similar issue, and the answer, provided by Tiddly worked for me. For some time.
Later I ran across the same issue. I don't know why, may be it was concerned with SDK or iOS updates, may be run loop of the app became more complicated. So I inspected FB SDK source and figured out that this issue occurs when you ask publish permissions just after read permissions, like this:
// Open with read permissions
[FBSession openActiveSessionWithReadPermissions: readPermissions
allowLoginUI: YES
completionHandler: ^
(FBSession *session, FBSessionState status, NSError *error)
{
// Ask for publish permissions (This is incorrect!)
[FBSession.activeSession requestNewPublishPermissions:publishPermissions
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:
^(FBSession *session, NSError *error)
{
// ...
}];
}];
When your app switches to Safari or FacebookApp and back,
-application: openURL: sourceApplication: annotation:
is called. CompletionHandler of
+openActiveSessionWithReadPermissions:
called immediately after this, before
applicationDidBecomeActive:. And after you start reauthorisation
applicationDidBecomeActive: is finally called. So, FB SDK think that user has returned back to the app, without giving permissions and reauthorisation fails with that "com.facebook.sdk error 2." error.
Sometimes dispatch_async() works well. But the robust solution, is to wait for active session to handle App Did Become Active event. And then request additional publish permissions. Here is an example of how to achieve this:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[FBAppCall handleDidBecomeActive];
if (self.shouldReauthorise) {
[self requestPublishPermissions];
self.shouldReauthorise = NO;
}
}