I have used the Facebook API to publish videos on Facebook and I'm now working on iOS to do the same. The APIs look very similar however they are missing the contentTitle and description. How do I set that? My API usage below works perfect just missing those two fields. Anyone know how to include that?
NSURL *videoURL = [NSURL fileURLWithPath:videoPath];
FBSDKShareVideo *video = [[FBSDKShareVideo alloc] init];
video.videoURL = videoURL;
FBSDKShareVideoContent *content = [[FBSDKShareVideoContent alloc] init];
content.video = video;
// Upload video
[FBSDKShareAPI shareWithContent:content delegate:self];
try this code because in this method you can share only video not text and description
if ([[FBSDKAccessToken currentAccessToken] hasGranted:#"contact_email"]) {
NSData *videoData = [NSData dataWithContentsOfURL:appDelegateObj.finalVideoUrl];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:3L];
[params setObject:videoData forKey:#"videofilename.MOV"];
[params setObject:#"Your post title" forKey:#"title"];
[params setObject:#"Your post description" forKey:#"description"];
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"/me/videos" parameters:params HTTPMethod:#"POST"]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
//video posted
NSLog(#"successfull %#:",result);
strFbSocialPostId = [result valueForKey:#"id"];//post ID
}
}];
}
When I post photo in faceboook one by one through ios. it creates a photo album,I want to post photo separately, don't create the album or thumbnails. upload single post at a time. I am doing R & D more than two days.
please provide a better solution.
Thanks,
UIImage *postImag = [UIImage ImageWithName:#"Hello.png"];
NSString *message = #"Hello Test Message";
NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithCapacity:0];
NSData *imageData = UIImagePNGRepresentation(postImag);
[params setObject:message forKey:#"message"];
if (imageData ) {
[params setObject:imageData forKey:#"source"];
}
/* make the API call */
[FBRequestConnection startWithGraphPath:#"me/photos" parameters:params
HTTPMethod:#"POST"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
if (error) {
NSLog(#"Error: %#",[error localizedDescription]);
} else {
NSLog(#"Successfully shared.");
}
completionAction();
}];
}
i am new in ios , i want to write something on my friend post . i am try this code using graph
api this is not working please help me
NSMutableDictionary* params = [NSMutableDictionary dictionary];
[params setObject:#"Some text" forKey:#"user_message_prompt"];
[params setObject:#"another text" forKey:#"action_links"];
[params setObject:#"Yet another text" forKey:#"attachment"];
[params setObject:#"SOME FACEBOOK ID" forKey:#"target_id"];
FBRequest *request = [[FBRequest alloc] initWithSession:FBSession.activeSession
graphPath:#"https://graph.facebook.com/100000329153640/feed"
parameters:params
HTTPMethod:#"POST"];
NSLog(#"%#",request);
in this NSlog-- request line return this
graphPath: https://graph.facebook.com/100000329153640/feed, HTTPMethod: POST, parameters: {
"action_links" = "another text";
attachment = "Yet another text";
"migration_bundle" = "fbsdk:20131212";
"target_id" = "SOME FACEBOOK ID";
"user_message_prompt" = "Some text";
}>
please give me solution
please share your valuable knowledge ... i am waiting
Thankyou
Try this one,
NSMutableDictionary *params = [NSMutableDictionary new];
[params setObject:#"Some text" forKey:#"user_message_prompt"];
[params setObject:#"another text" forKey:#"action_links"];
[params setObject:#"Yet another text" forKey:#"attachment"];
[params setObject:#"SOME FACEBOOK ID" forKey:#"target_id"];
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
NSLog(#" RESULT : %u",result);
}
];
Refer Developer Guide for facebook
Facebook Developer guide
The first thing you need to do is request publish permissions. I wrote this helper method to do it:
void runBlockWithPublishPermissions(void (^block)(FBSession *))
{
FBSession *session = [FBSession activeSession];
NSString *publishPermission = #"publish_actions";
// If we already have publish permissions, then submit.
if (([session isOpen] && FB_ISSESSIONOPENWITHSTATE(session.state)) &&
[session.permissions containsObject:publishPermission])
{
block(session);
}
// Else request for the permissions, then submit.
else
{
[session requestNewPublishPermissions:#[publishPermission]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error)
{
block(session);
}
else
{
NSLog(#"Failed to request publish permissions: %#", error);
}
}];
}
}
You would then use your code in the following way:
void (^submit)(FBSession *) = ^(FBSession *session){
NSMutableDictionary* params = [NSMutableDictionary dictionary];
[params setObject:#"Some text" forKey:#"user_message_prompt"];
[params setObject:#"another text" forKey:#"action_links"];
[params setObject:#"Yet another text" forKey:#"attachment"];
[params setObject:#"SOME FACEBOOK ID" forKey:#"target_id"];
FBRequest *request = [[FBRequest alloc] initWithSession:session
graphPath:#"https://graph.facebook.com/100000329153640/feed"
parameters:params
HTTPMethod:#"POST"];
[request startWithCompletionHandler:nil];
};
runBlockWithPublishPermissions(submit);
In Facebook, you can post something and share it with just some friends.
How can this be done programmatically on iOS using Facebook SDK?
My App has an "Emergency Button" that sends: a) User Location on a Map (picture); b) An emergency text (message); and c) The post is only shared with the friends the user has chosen in the config. section (privacy).
- (void)sendMessage {
//Just an Example: Send an Emergency Post with: message, picture, just to SOME friends.
//This action may not need the User to actually press a button.
//Privacy parameter obtained from:
//https://developers.facebook.com/docs/graph-api/reference/v2.0/post/
NSMutableDictionary *privacy = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
#"100000278095294,100000278095528", #"allow", //Friends Id separeted with commas.
#"", #"deny",
#"CUSTOM", #"value", //Privacy Custom Value
nil];
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:self.emergencyText forKey:#"message"];
[params setObject:self.locationMap forKey:#"picture"];
[params setObject:privacy forKey:#"privacy"];
[FBRequestConnection startWithGraphPath:#"me/feed"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
if (error) {
NSLog(#"Error");
NSLog(#"Error descriptcion %#",error.description);
}else{
NSLog(#"Success");
}
}];
}
This code in not running. I get an error from Facebook.
If I comment this line:
// [params setObject:privacy forKey:#"privacy"];
Then it runs fine and I see the post in FB, but it is a public post.
I need to post: message, picture, just to some friends.
Any solution using startWithGraphPath or using any other command is welcome!
You must format the privacy as a JSON string and then assign that json string to the params NSMutableDictionary.
For Example:
NSMutableDictionary *privacy = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
#"100000278095294,100000278095528", #"allow", //Friends Id separeted with commas.
#"", #"deny",
#"CUSTOM", #"value", //Privacy Custom Value
nil];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:privacy
options:NSJSONWritingPrettyPrinted
error:&error];
NSString *jsonString;
if (! jsonData) {
NSLog(#"Got an error: %#", error2);
} else {
jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:#"to my friend" forKey:#"message"];
[params setObject:self.locationMap forKey:#"picture"];
[params setObject:jsonString forKey:#"privacy"];
I am making a test app through that I want to post video on facebook. I am using latest sdk of facebook. But I am not able to post it on facebook.
My code is as below.
NSDictionary *parameters = [NSDictionary dictionaryWithObject:videoData forKey:#"CareAppDemo.mov"];
FBRequest *request = [FBRequest requestWithGraphPath:#"me/videos" parameters:parameters HTTPMethod:#"POST"];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(#"result: %#, error: %#", result, error);
}];
Please help me to post video on facebook via my app.
You need to download FacebookSDK first
and then add following framework into your project
FacebookSDK.framework, FBSDKLoginKit.framework, FBSDKShareKit.framework,
Bolts.framework,FBSDKCoreKit.framework
import them,
and write followin code
if(![FBSDKAccessToken currentAccessToken])
{
FBSDKLoginManager *login1 = [[FBSDKLoginManager alloc]init];
[login1 logInWithPublishPermissions:#[#"publish_actions"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
FBSDKShareVideo *video = [[FBSDKShareVideo alloc] init];
video.videoURL = videoAssetURL;
FBSDKShareVideoContent *content = [[FBSDKShareVideoContent alloc] init];
content.video = video;
[FBSDKShareDialog showFromViewController:self withContent:content delegate:nil];
}];
}
else {
FBSDKShareVideo *video = [[FBSDKShareVideo alloc] init];
video.videoURL = videoAssetURL;
FBSDKShareVideoContent *content = [[FBSDKShareVideoContent alloc] init];
content.video = video;
[FBSDKShareDialog showFromViewController:self withContent:content delegate:nil];
}
The video URL videoURL must be an asset URL. You can get a video asset URL e.g. from UIImagePickerController.
or for recording video you can take as follow
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:[NSURL URLWithString:[[NSHomeDirectory() stringByAppendingPathComponent:#"Library/Caches"] stringByAppendingFormat:#"/current.mov"]] completionBlock:^(NSURL *assetURL, NSError *error)
{
videoAssetURL =assetURL;
}];
for more detail you can use https://developers.facebook.com/docs/sharing/ios
Successfully tested On FaceBook SDK 3.14.1
Recommendation: 3 properties in .plist file
set FacebookAppID,FacebookDisplayName,
URL types->Item 0->URL Schemes set to facebookappId prefix with fb See
-(void)shareOnFaceBook
{
//sample_video.mov is the name of file
NSString *filePathOfVideo = [[NSBundle mainBundle] pathForResource:#"sample_video" ofType:#"mov"];
NSLog(#"Path Of Video is %#", filePathOfVideo);
NSData *videoData = [NSData dataWithContentsOfFile:filePathOfVideo];
//you can use dataWithContentsOfURL if you have a Url of video file
//NSData *videoData = [NSData dataWithContentsOfURL:shareURL];
//NSLog(#"data is :%#",videoData);
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
videoData, #"video.mov",
#"video/quicktime", #"contentType",
#"Video name ", #"name",
#"description of Video", #"description",
nil];
if (FBSession.activeSession.isOpen)
{
[FBRequestConnection startWithGraphPath:#"me/videos"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error)
{
NSLog(#"RESULT: %#", result);
[self throwAlertWithTitle:#"Success" message:#"Video uploaded"];
}
else
{
NSLog(#"ERROR: %#", error.localizedDescription);
[self throwAlertWithTitle:#"Denied" message:#"Try Again"];
}
}];
}
else
{
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"publish_actions",
nil];
// OPEN Session!
[FBSession openActiveSessionWithPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceEveryone allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
if (error)
{
NSLog(#"Login fail :%#",error);
}
else if (FB_ISSESSIONOPENWITHSTATE(status))
{
[FBRequestConnection startWithGraphPath:#"me/videos"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error)
{
[self throwAlertWithTitle:#"Success" message:#"Video uploaded"];
NSLog(#"RESULT: %#", result);
}
else
{
[self throwAlertWithTitle:#"Denied" message:#"Try Again"];
NSLog(#"ERROR: %#", error.localizedDescription);
}
}];
}
}];
}
}
I GOT Error In first time of App runs:
The operation couldn’t be completed. (com.facebook.sdk error 5.)
It happens when facebook is being inited. Next time i open my app, it works fine, its always the first time. Tried everything in app, but it seems to be on the Facebook SDK side.
Few causes for seeing com.facebook.sdk error 5:
Session is is not open. Validate.
Facebook has detected that you're spamming the system. Change video name.
Facebook has a defined limit using the SDK. Try a different app.
Wrong publish permission. Give publish_actions a spin.
many more....
Get the publish permission
NSArray* permissions = [[NSArray alloc] initWithObjects:
#"publish_stream", nil];
[facebook authorize:permissions delegate:self];
[permissions release];
Try this
- (void)fbDidLogin {
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"mov"];
NSData *videoData = [NSData dataWithContentsOfFile:filePath];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
videoData, #"video.mov",
#"video/quicktime", #"contentType",
#"Video Test Title", #"title",
#"Video Test Description", #"description",
nil];
[facebook requestWithGraphPath:#"me/videos"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
}
Best Example to upload video on Facebook Check it
Download from below link that's only iPhone
This is the old thread but for all the future readers coming, here is how to do it with the currently latest facebook SDK (v3.24.0 - September 10th 2015).
- (IBAction)bntShareOnFacebookAction:(id)sender {
if ([[FBSDKAccessToken currentAccessToken] hasGranted:#"publish_actions"]) {
[self shareVideoOnFacebook];
} else {
FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
[loginManager logOut]; //very important line for login to work
[loginManager logInWithPublishPermissions:#[#"publish_actions"]
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if(!error) {
[self shareVideoOnFacebook];
}
}];
}
}
- (void) shareVideoOnFacebook {
NSString *videoPath = #"/Documents/.../movie.mov";
NSData *videoData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:videoPath]];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:3L];
[params setObject:videoData forKey:#"video_filename.MOV"];
[params setObject:#"Title for this post." forKey:#"title"];
[params setObject:#"Description for this post." forKey:#"description"];
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"/me/videos" parameters:params HTTPMethod:#"POST"]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
//video posted
}
}];
}
IF you want to Upload/Video sharing you must need to pass the Assets Library URL for the original version of the picked item.
URL e.g assets-library://asset/asset.MOV?id=18BC70A0-208A-4F03-A207-7D57C8863425&ext=MOV
If you are using
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
Then you must need to pass
NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL];
If you using Document Directory Path then first you need to save video into library. You must need to create url link Assets Library URL.
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:videoURL];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (success) {
// Fetch Last saved video.
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:#"creationDate" ascending:false]];
// Get video url.
PHAsset *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:fetchOptions].firstObject;
NSString *assetID = [fetchResult.localIdentifier substringToIndex:(fetchResult.localIdentifier.length - 7)];
NSURL *assetURL = [NSURL URLWithString:[NSString stringWithFormat:#"assets-library://asset/asset.MOV?id=%#&ext=MOV", assetID]];
// Share Video.
FBSDKShareVideo *video = [[FBSDKShareVideo alloc] init];
video.videoURL = assetURL;
FBSDKShareVideoContent *content = [[FBSDKShareVideoContent alloc] init];
content.video = video;
FBSDKShareDialog *shareDialog = [[FBSDKShareDialog alloc] init];
shareDialog.shareContent = content;
shareDialog.delegate = (id)self;
shareDialog.fromViewController = self;
NSError * error = nil;
BOOL validation = [shareDialog validateWithError:&error];
if (validation) {
dispatch_async(dispatch_get_main_queue(), ^{
[shareDialog show];
});
} else {
NSLog(#"%#", error.localizedDescription);
}
}
}];