Sharing Video to facebook via FB SDK for iOS - ios

I have a video created in my app.
I want to shared this video to user's Facebook wall via facebook native SDK.
I am able to get the video sharing working, but there is no preview/share dialog shown to the user before sharing. The video is directly posted to user's wall.
Here is my code to get open an active session
[FBSession openActiveSessionWithPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceOnlyMe allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error){}
Once I get the session active, I call the below method to share the video
- (void)sendVideoFeedRequestWithParams:(NSMutableDictionary *)params
{
// create the connection object
FBRequestConnection *newConnection = [[FBRequestConnection alloc] init];
// create a handler block to handle the results of the request
FBRequestHandler handler =
^(FBRequestConnection *connection, id result, NSError *error) {
// output the results of the request
[self requestCompleted:connection result:result error:error];
};
// create the request object, using the /me as the graph path
FBRequest *request = [[FBRequest alloc] initWithSession:FBSession.activeSession graphPath:#"me/videos" parameters:params HTTPMethod:#"POST"];
// add the request to the connection object, if more than one request is added
// the connection object will compose the requests as a batch request; whether or
// not the request is a batch or a singleton, the handler behavior is the same,
// allowing the application to be dynamic in regards to whether a single or multiple
// requests are occuring
[newConnection addRequest:request completionHandler:handler];
// if there's an outstanding connection, just cancel
[self.requestConnection cancel];
// keep track of our connection, and start it
self.requestConnection = newConnection;
[newConnection start];
}
The params I am sending to this method are:
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
videoData, #"video.mov",
#"video/quicktime", #"contentType",
#"Video Test Title", #"title",
#"Video Test Description", #"description",
nil];
This works well as far as posting the video is concerned. But, I want a share/preview dialog to be shown.
Anybody got any hints on how to get it done?

You need to create something yourself or search for an existing solution on github (or similar). Something like a custom alert view where you can allow the user to enter text, have a thumbnail of the video which plays when tapped and a couple of buttons.

Related

Facebook Graph Api call not working for iOS

For an iOS app, using facebook sdk i need to fetch photos from user's album but i need to fetch them in a single request so that i could get info about albums and containing photos at the same time, for that i tried
/me/albums?fields=count,name,photos.limit(10){images}
that api call is working fine in Graph api explorer but not on iOS platform. I only need images field of photos that is why using
above api call
If i run this api it runs fine
/me/albums?fields=count,name,photos.limit(10)
only {images} part is causing the problem, the error message which i get is of unsupported url, have acquired the permissions for user_photos
Code :
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:#"/me/albums?fields=count,name,photos.limit(10){images}" parameters:nil HTTPMethod:#"GET"];
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
[connection addRequest:request completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if(!error) {
NSLog(#"result %#",result);
} else {
NSLog(#"error description : %#",error.description);
}
}];
[connection start];
I was making the final url call by adding/removing fields from graph api explorer side pane, the request call it was generating was
/me/albums?fields=count,name,photos.limit(10){images}
not working on iOS platform, but if i use this syntax instead
/me/albums?fields=count,name,photos.limit(10).fields(images)
to address fields of photos then it works fine, got the inspiration from answer posted here

Facebook Graph API Call Without Using User Login

I am working on getting posts from a public Facebook page using the Facebook Graph API, without the user logging in. Using the steps from here I got the Token I would need since the end-user is not logging in, and the page is public. In my app, I have the following:
-(IBAction)testing {
NSString *token = #"MYAccessToken";
FBSession* session = [[FBSession alloc] initWithPermissions:#[#"manage_pages"]];
FBAccessTokenData* tokenData =
[FBAccessTokenData createTokenFromString:token
permissions:#[#"manage_pages"]
expirationDate:nil
loginType:FBSessionLoginTypeNone
refreshDate:nil];
[session openFromAccessTokenData:tokenData completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
if ([session isOpen]) {
[self performGraph];
}
}];
}
-(void)performGraph {
NSLog(#"Go");
[FBRequestConnection startWithGraphPath:#"/{your-page}/posts"
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSLog(#"Result%#", result);
/* handle the result */
}];
}
It runs the method performGraph as I can see it in my Console, leading me to believe that the FBSession must be open, since that is where it runs it. However, I get the following error message in FBSDKLog: Error for request to endpoint '/{your-page}/posts': An open FBSession must be specified for calls to this endpoint.
If the FBSession isn't open, it shouldn't be even attempting to perform the Graph API call, yet it tells me it needs an open FBSession. Can someone help me out here a bit?
As provided by the documentation for startWithGraphPath:parameters:HTTPMethod:completionHandler: that method uses the [FBSession activeSession], and since you're creating a new session object, it is NOT the activeSession, which is why you're effectively calling the graph API with a nil session.
You can either set the session you created as the activeSession by calling FBSession.activeSession, or you can create an FBRequest object with your session instance, and then create a FBRequestConnection with the FBRequest.

ObjC, Facebook Page - posting news feed works, but posting photo does not

so I'm trying to post a photo on my Facebook page (I'm admin) from iPhone app. I'm using FB Sessions to create the session, get the read permission, get manage_pages permission, then, I successfully get my Facebook Pages app-ids as a result of
[FBRequestConnection startWithGraphPath:#"/me/accounts" completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
NSLog(#"%#", [result description]);
}]; // have token, what now?
Which is still fine. Then, I try to post a photo to the app_id/photos feed, and it does not work = it uploads the photo correctly, but shows me (as in my profile) uploading the photo rather then the Page itself. What could be the problem?
Here's the code for the params and the call
NSDictionary *params = [NSDictionary
dictionaryWithObjects: [NSArray arrayWithObjects:
[UIImage imageNamed:#"Icon.png"],
#"This is my first photo post from xcode app", nil]
forKeys:[NSArray arrayWithObjects:
#"source",
#"message", nil]];
[FBRequestConnection startWithGraphPath:#"MyPageID/photos" parameters:params HTTPMethod:#"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
NSLog(#"%#", [result description]);
} ];
the [result description] log from the call is fine (it returns the id = xx and "post_id" = yy, which I assume are correct), as is the call itself - the only problem is that it shows me as the author, and not the Page itself.
MyPageID is correct, because calling
NSDictionary *paramsFeed = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:#"This is my first page post from xcode app", nil] forKeys:[NSArray arrayWithObjects:#"message", nil]];
[FBRequestConnection startWithGraphPath:#"390106241058188/feed" parameters:paramsFeed HTTPMethod:#"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
NSLog(#"result: %#", [result description]);
}];
Also, calling me/photos and posting the photo there with dictionaryKey #"picture" works, and it posts the photo on my own wall perfectly.
Do anyone knows where the problem could be hidden?
I found the solution, although I find it weird that I did not have to do this for the /feed page update
Huge thanks goes for Mr. Arpit Kumar Kulshrestha, who pointed out the right way in my previous incarnation of the problem ( Xcode - how to share photo from iPhone app to Facebook managed page feed )
the solution:
I've tried setting a new FBSession before, but it still had a lot of errors in it, so I've left that route and went to another one. However, it makes perfect sense to do that -
create FBAccessTokenData, which has the Page's token in it, and set other things to the same as the active session - like this
FBAccessTokenData *tokenData = [FBAccessTokenData createTokenFromString:pageTokenString permissions:[FBSession activeSession].accessTokenData.permissions expirationDate:[FBSession activeSession].accessTokenData.expirationDate loginType:FBSessionLoginTypeFacebookApplication refreshDate:nil];
then, one need to create a new FBSession, and what is important, it needs to have set its tokenCacheStrategy to something without data, i.e.[FBSessionTokenCachingStrategy nullCacheInstance] . I did a blank allocation ([[FBSession alloc] init]), and that gave me a lot of errors, however when I use this one
FBSession *sessionFb = [[FBSession alloc] initWithAppID:appID permissions:[NSArray arrayWithObjects: #"publish_stream", #"manage_pages", nil] urlSchemeSuffix:nil tokenCacheStrategy:[FBSessionTokenCachingStrategy nullCacheInstance]];
it works perfectly. And when you have this new session, you want to open the path for the data to come and go, and you can make it like this:
[sessionFb openFromAccessTokenData:tokenData completionHandler:^(FBSession *session, FBSessionState status, NSError *error)
{
}];
and the last part before it starts working correctly is setting the activeSession to this newly allocated and opened session, this way:
[FBSession setActiveSession:sessionFb];
after I've made all of these changes, the photo page sharing started to work magically.
However, I'm still not sure how is it possible that the photo share did not work before but the feed share did.. but this solves my issue.

I Only Get Facebook Active Access Token When Using iOS Facebook Integration

So when I am logged in to ios's facebook integration, my code works great. I get an active access token with permissions to read and write, and I have actually written to people's walls and whatnot from the app. However, when my app uses safari to authenticate people's login credentials, there is the common error: "An active access token must be used to query information about the current user."
What I don't understand is why I can only get my access code from the ios facebook integration. Anyways, my relevant code below is implemented by the current view controller when it loads:
if (![FBSession activeSession].isOpen) {
[self openSession];
}
my method openSession is defined as follows
- (void)openSession
{
//create the permissions array
NSArray *permissions =
[NSArray arrayWithObjects:#"email", #"basic_info", nil];
[FBSession openActiveSessionWithReadPermissions: permissions
allowLoginUI:YES
completionHandler: ^(FBSession *session, FBSessionState status, NSError *error) {
NSLog(#"\n\nopenSession called error = %#\n\n\n",error);
}];
}
I then go on to continue in the viewDidLoadMethod
[FBRequestConnection
startForMeWithCompletionHandler:^(FBRequestConnection *connection,
id<FBGraphUser> user,
NSError *error)
{
NSLog(#"error requestion connection: %#",error);
}];
I look forward to the response. Thanks in advance.
Using Facebook SDK.
NSString *fbAccessToken = [[[FBSession activeSession] accessTokenData] accessToken];
If you prefer dot syntax,
NSString *fbAccessToken = [FBSession activeSession].accessTokenData.accessToken;`
I hope , it will work for u

Facebook friend's wall posting through ID

I am trying to post on a Facebook friend's wall, I tried these two methods but none work:
1.
//post on wall
NSMutableDictionary *variables = [[NSMutableDictionary alloc]initWithCapacity:1];
[variables setObject:#"v" forKey:#"message"];
[graphref doGraphPost:[NSString stringWithFormat:#"1389799421/feed"] withPostVars:variables];
//post on wall
2.
[_facebook requestWithGraphPath:#"1389799421/feed"
andParams:[NSMutableDictionary dictionaryWithObject:#"test wall post" forKey:#"message"]
andHttpMethod:#"POST"
andDelegate:self];
...and I can't understand why!! On the facebook website I have added the bundle and the permissions.
I am trying to post on a Facebook friend's wall
Facebook recently announced in the developer blog, that posting to another user’s wall through the API will not be possible any more from Feb 2013 on:
Removing ability to post to friends walls via Graph API
We will remove the ability to post to a user's friends' walls via the Graph API. Specifically, posts against [user_id]/feed where [user_id] is different from the session user, or stream.publish calls where the target_id user is different from the session user, will fail. If you want to allow people to post to their friends' timelines, invoke the feed dialog.
So I think it’s pretty useless starting to develop a feature like that now.
First off, you have to open your session with publish permissions. Specifically, you must request the publish_stream permission.
NSArray * permissions = [[NSArray alloc] initWithObjects:#"publish_stream", nil];
return [FBSession openActiveSessionWithPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceFriends allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
[self sessionStateChanged:session
state:status
error:error];
}];
If you have publish permissions, you can create and send the request. Make sure you include the access_token as one of the parameters. If you don't, you will get authentication errors.
NSDictionary * postParameters = [NSDictionary dictionaryWithObjectsAndKeys:_textView.text, #"message", FBSession.activeSession.accessToken, #"access_token", nil];
NSString * graphPath = #"ID_NUMBER_HERE/feed";
FBRequest * request = [FBRequest requestWithGraphPath:graphPath parameters:postParameters HTTPMethod:#"POST"];
[[request initWithSession:FBSession.activeSession graphPath:graphPath parameters:postParameters HTTPMethod:#"POST"] startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(#"Successful posted to Facebook");
}];
}

Resources