I would like to share a custom story by Facebook SDK for iOS. I tried to do it in these ways, but none of them works:
Does not log anything
[FBGraphObject openGraphObjectForPostWithType:#"Lorem ipsum"
title:#"Lorem ipsum"
image:#"https://example.com/cooking-app/images/Lamb-Vindaloo.png"
url:#"https://example.com/cooking-app/meal/Lamb-Vindaloo.html"
description:#"Spicy curry of lamb and potatoes"];
id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
[action setObject:cell.params forKey:slugs[self.categoryID]];
[FBDialogs presentShareDialogWithOpenGraphAction:action
actionType:[NSString stringWithFormat:#"Lorem ipsum:%#", slugs[self.categoryID]]
previewPropertyName:slugs[self.categoryID]
handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
if(error) {
NSLog(#"Error: %#", error.description);
} else {
NSLog(#"Success!");
}
}];
The operation couldn't be completed. (com.facebook.sdk error 5.)
[FBSession openActiveSessionWithAllowLoginUI:NO];
FBRequest *request = [FBRequest requestForPostWithGraphPath:#"/me/Lorem ipsum" graphObject:#{#"object": cell.params}];
[request setHTTPMethod:#"POST"];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
if (!error)
{
// Sucess! Include your code to handle the results here
NSLog(#"result: %#", result);
NSString *alertTitle = #"Object successfully created";
NSString *alertText = #"An object has been created";
[[[UIAlertView alloc] initWithTitle:alertTitle
message:alertText
delegate:self
cancelButtonTitle:#"OK!"
otherButtonTitles:nil] show];
}
else
{
NSLog(#"%#", [error localizedDescription]);
}
}];
cell.params
cell.params = [FBGraphObject openGraphObjectForPostWithType:[NSString stringWithFormat:#"Lorem ipsum:%#", slugs[self.categoryID]]
title:#"Lorem ipsum"
image:[NSString stringWithFormat:#"https://example.com/cooking-app/images/%#.png", slugs[self.categoryID]]
url:#"https://example.com"
description:#"Lorem ipsum"];
cell.params[#"Lorem ipsum"] = #{#"minutes": #5, #"seconds": #37, #"hours": #1};
Related
I need to publish Open Graph story to Facebook wall from my app. I followed current tutorial on Facebook developers.
I've created object,action and story on Facebook dashboard. Then in my app do the following code:
create an object:
NSMutableDictionary<FBOpenGraphObject> *object = [FBGraphObject openGraphObjectForPost];
object.provisionedForPost = YES;
object[#"title"] = #"test title";
object[#"type"] = #"myapp:my_object_type";
object[#"description"] = #"test discription";
object[#"url"] = #"http://example.com";
object[#"image"] = #[#{#"url": [result objectForKey:#"uri"], #"user_generated" : #"false" }];
and post it:
[FBRequestConnection startForPostOpenGraphObject:object completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error) {
NSString *objectId = [result objectForKey:#"id"];
NSLog(#"object id %#",objectId);
} else {
NSLog(#"Error posting the Open Graph object to the Object API: %#", error);
}
}];
} else {
NSLog(#"Error staging an image: %#", error);
}
}];
Got object id as result.
Then create an action and link it with object:
id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
[action setObject:objectId forKey:#"my_object_type"];
FBOpenGraphActionParams *params = [[FBOpenGraphActionParams alloc] init];
params.action = action;
params.actionType = #"myapp:my_app_action";
And post it to Facebook:
[FBRequestConnection startForPostWithGraphPath:#"/me/myapp:my_app_action" graphObject:action completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error) {
NSLog(#"OG story posted, story id: %#", [result objectForKey:#"id"]);
[[[UIAlertView alloc] initWithTitle:#"OG story posted"
message:#"Check your Facebook profile or activity log to see the story."
delegate:self
cancelButtonTitle:#"OK!"
otherButtonTitles:nil] show];
} else {
NSLog(#"Encountered an error posting to Open Graph: %#", error);
}
}];
As a result i got the message that OG story was successfully posted, but when going to Facebook, there is nothing in feed or timeline, just an image added to users photo album.
Well, dunno what was the problem, but now it works with the following code:
FBRequestConnection *connection = [[FBRequestConnection alloc]init];
NSMutableDictionary <FBOpenGraphObject> *object = [FBGraphObject openGraphObjectForPost];
object[#"type"] = #"myapp:my_object_type";
object[#"title"] = #"title";
object[#"url"] = #"http://example.com";
object[#"image"] = #"http://image.png";
object[#"description"] = #"description";
FBRequest *objectRequest = [FBRequest requestForPostOpenGraphObject:object];
[connection addRequest:objectRequest completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (error) {
NSLog(#"Error: %#",error.description);
}
} batchEntryName:#"objectCreate"];
NSMutableDictionary <FBOpenGraphAction> *action = [FBGraphObject openGraphActionForPost];
action[#"buy"] = #"{result=objectCreate:$.id}";
action[#"fb:explicitly_shared"] = #"true";
FBRequest *actionRequest = [FBRequest requestForPostWithGraphPath:#"me/myapp:my_app_action" graphObject:action];
[connection addRequest:actionRequest completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (error) {
NSLog(#"error: %#",error.description);
} else {
NSLog(#"posted with id %#", result[#"id"]);
}
}];
[connection start];
I wrote code as below where file exists in resources. Its not null.
I am successful in adding images, but stuck at videos.
-(void)uploadVideo {
NSLog(#"UPload Videio ");
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"abc" ofType:#"mp4"];
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfFile:filePath options:NSDataReadingUncached error:&error];
if(data == nil && error!=nil) {
//Print error description
NSLog(#"error is %#", [error description]);
}
NSLog(#"data is %#", data);
NSDictionary *parameters = [NSDictionary dictionaryWithObject:data forKey:#"sample.mov"];
if (FBSession.activeSession.isOpen) {
[FBRequestConnection startWithGraphPath:#"me/videos"
parameters:parameters
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
// [FBRequestConnection setVideoMode:NO];
if(!error) {
NSLog(#"OK: %#", result);
} else
NSLog(#"Error: %#", error.localizedDescription);
}];
} else {
// We don't have an active session in this app, so lets open a new
// facebook session with the appropriate permissions!
// Firstly, construct a permission array.
// you can find more "permissions strings" at http://developers.facebook.com/docs/authentication/permissions/
// In this example, we will just request a publish_stream which is required to publish status or photos.
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"publish_stream",
nil];
//[self controlStatusUsable:NO];
// OPEN Session!
[FBSession openActiveSessionWithPermissions:permissions
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
// if login fails for any reason, we alert
if (error) {
// show error to user.
} else if (FB_ISSESSIONOPENWITHSTATE(status)) {
// no error, so we proceed with requesting user details of current facebook session.
[FBRequestConnection startWithGraphPath:#"me/videos"
parameters:parameters
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
// [FBRequestConnection setVideoMode:NO];
if(!error) {
NSLog(#"Result: %#", result);
} else
NSLog(#"ERROR: %#", error.localizedDescription);
}];
//[self promptUserWithAccountNameForUploadPhoto];
}
// [self controlStatusUsable:YES];
}];
}
}
In return I am getting error as
The operation couldn’t be completed. (com.facebook.sdk error 5.)
How to upload video to facebook using facebook iOS SDK?
Thanks
Here's a method to upload video to Facebook. This code is testing and 100% working.
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *facebookAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
// Specify App ID and permissions
NSDictionary *options = #{ACFacebookAppIdKey: FACEBOOK_ID,
ACFacebookPermissionsKey: #[#"publish_stream", #"video_upload"],
ACFacebookAudienceKey: ACFacebookAudienceFriends}; // basic read permissions
[accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *e) {
if (granted) {
NSArray *accountsArray = [accountStore accountsWithAccountType:facebookAccountType];
if ([accountsArray count] > 0) {
ACAccount *facebookAccount = [accountsArray objectAtIndex:0];
NSDictionary *parameters = #{#"description": aMessage};
SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:[NSURL URLWithString:#"https://graph.facebook.com/me/videos"]
parameters:parameters];
[facebookRequest addMultipartData: aVideo
withName:#"source"
type:#"video/mp4"
filename:#"video.mov"];
facebookRequest.account = facebookAccount;
[facebookRequest performRequestWithHandler:^(NSData* responseData, NSHTTPURLResponse* urlResponse, NSError* error) {
if (error == nil) {
NSLog(#"responedata:%#", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
}else{
NSLog(#"%#",error.description);
}
}];
}
} else {
NSLog(#"Access Denied");
NSLog(#"[%#]",[e localizedDescription]);
}
}];
Recommendation:
I think this might be a permissions issue but I am not sure where the error is being thrown. The delegate method that would be thrown is not shown in your code. I think reconciling your code with the steps in this sample might be helpful; if so please accept the answer.
Some key aspects of the sample:
Permissions:
- (IBAction)buttonClicked:(id)sender {
NSArray* permissions = [[NSArray alloc] initWithObjects:
#"publish_stream", nil];
[facebook authorize:permissions delegate:self];
[permissions release];
}
Build Request:
- (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];
}
Request didLoad delegate method:
- (void)request:(FBRequest *)request didLoad:(id)result {
if ([result isKindOfClass:[NSArray class]]) {
result = [result objectAtIndex:0];
}
NSLog(#"Result of API call: %#", result);
}
Request didFail delegate method:
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
NSLog(#"Failed with error: %#", [error localizedDescription]);
}
Facebook Video Permissions Link
This Code is Tested successfully On FaceBook SDK 3.14.1
Recommendation: In .plist
set FacebookAppID,FacebookDisplayName,
URL types->Item 0->URL Schemes set to facebookappId prefix with fb
-(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);
}
}];
}
}];
}
}
And I GOT Error:
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.
Did you ask for a publish_stream permission before?
Is it possible to use the Facebook SDK or Graph API to create a post like the one below?
I essentially want to combine these two together:
[FBRequestConnection startForUploadPhoto:[UIImage imageNamed:#"myImage"] completionHandler:nil];
[FBRequestConnection startForPostStatusUpdate:#"Hello, World!" completionHandler:nil];
Thanks.
Use following method :
// Present share dialog
[FBDialogs presentShareDialogWithLink:params.link
name:params.name
caption:params.caption
description:params.description
picture:params.picture
clientState:nil
handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
if(error) {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog([NSString stringWithFormat:#"Error publishing story: %#", error.description]);
} else {
// Success
NSLog(#"result %#", results);
}
}];
Here is the reference : https://developers.facebook.com/docs/ios/share
Try this,
FBRequestConnection *connection = [[FBRequestConnection alloc] init];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[dictionary setObject:UIImagePNGRepresentation(_image) forKey:#"picture"];
if (message) {
[dictionary setObject:message forKey:#"message"];
}
FBRequest *request = [FBRequest requestWithGraphPath:#"me/photos" parameters:dictionary HTTPMethod:#"POST"];
[connection addRequest:request completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
}];
[connection start];
I'm using Facebook SDK 3.6 within an iOS 6.3 app to upload a video to Facebook.
I've looked over many Stack Overflow posts about this but they are all years old and using much older Facebook SDKs.
Sometimes it works, other times it fails with the following message:
unexpected error:Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0x1e2affc0 {com.facebook.sdk:HTTPStatusCode=500, com.facebook.sdk:ParsedJSONResponseKey={
body = {
"error_code" = 1;
"error_msg" = "An unknown error occurred";
};
code = 500;
}, com.facebook.sdk:ErrorSessionKey=, expirationDate: 4001-01-01 00:00:00 +0000, refreshDate: 2013-07-30 10:54:22 +0000, attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:(
"publish_stream"
)>}
Here is my code:
FBRequestConnection *_currentConnection;
[FBSession.activeSession requestNewPublishPermissions:#[#"publish_stream"]
defaultAudience:FBSessionDefaultAudienceOnlyMe
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
NSError *attributesError;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:url.path error:&attributesError];
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];
NSLog(#"file size: %lld", fileSize);
NSString *filename = [url lastPathComponent];
NSLog(#"filename: %#", filename);
NSString *mimeType = [self MIMETypeForFilename:filename
defaultMIMEType:#"video/mp4"];
NSLog(#"mime type: %#", mimeType);
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
videoData, filename,
mimeType, #"contentType",
self.song.name, #"title",
_videoDescription, #"description",
nil];
FBRequest *request = [FBRequest requestWithGraphPath:#"me/videos"
parameters:params
HTTPMethod:#"POST"];
_currentConnection = [request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
self.stageLabel.text = #"";
NSLog(#"result: %#, error: %#", result, error);
if(error) {
// Facebook SDK * error handling *
// if the operation is not user cancelled
if (error.fberrorCategory != FBErrorCategoryUserCancelled) {
[self showAlert:#"Video Post" result:result error:error];
}
self.uploadBarButtonItem.enabled = YES;
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Video Uploaded" message:#"Video has been uploaded"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[self.delegate facebookUploaderUploadSucceeded:self];
}
// Delete the temp video
NSError *err;
[[NSFileManager defaultManager] removeItemAtURL:_sourceURL error:&err];
NSLog(#"Deleting video %#: %#", _sourceURL, [err localizedDescription]);
}];
}];
}
}];
This Code is Tested successfully On FaceBook SDK 3.14.1
-(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);
}
}];
}
}];
}
}
And I GOT Error:
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.
I'm creating iOS app using latest Facebook framework 3.1
I've seen lots of content regarding posting image and text to facebook user's wall. I'm using the following code.
NSDictionary* dict = #{
#"link" : #"https://developers.facebook.com/ios",
#"picture" : [UIImage imageNamed:#"Default"],
#"message":#"Your temp message here",
#"name" : #"MyApp",
#"caption" : #"TestPost",
#"description" : #"Integrating Facebook in ios"
};
[FBRequestConnection startWithGraphPath:#"me/feed" parameters:dict HTTPMethod:#"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSString *alertText;
if (error)
{
NSLog(#"%#",[NSString stringWithFormat: #"error: domain = %#, code = %d", error.domain, error.code]);
alertText = #"Failed to post to Facebook, try again";
} else
{
alertText = #"Posted successfully to Facebook";
}
[[[UIAlertView alloc] initWithTitle:#"Facebook Result" message:alertText delegate:nil cancelButtonTitle:#"OK!" otherButtonTitles:nil] show];
}];
If I'm providing the picture as "URL" its posting fine. Else its throwing error. I want to post an image that is within the application bundle. Can some on tell me where I'm coding wrong?
UIImage* image = [UIImage imageNamed:#"nature.jpg"];
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:#"My First message" forKey:#"message"];
[params setObject:image forKey:#"picture"];
[FBRequestConnection startWithGraphPath:#"me/photos" parameters:dict HTTPMethod:#"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSString *alertText;
if (error)
{
NSLog(#"%#",[NSString stringWithFormat: #"error: domain = %#, code = %d", error.domain, error.code]);
alertText = #"Failed to post to Facebook, try again";
} else
{
alertText = #"Posted successfully to Facebook";
}
[[[UIAlertView alloc] initWithTitle:#"Facebook Result" message:alertText delegate:nil cancelButtonTitle:#"OK!" otherButtonTitles:nil] show];
}];