After searching 2 days on it m finally posting my code.
Application Description: I am choosing an image through imagePicker and after editing it I want to share it on Facebook.
Problem: I can post the text on my wall but not the image, as i dont have any URL i cant find another way to post image on my wall.
here is the code ( which i stole from another application) of my PostToWall
FBStreamDialog* dialog = [[[FBStreamDialog alloc] init] autorelease];
dialog.userMessagePrompt = #"Enter your message:";
dialog.actionLinks = #"[{\"Get DowntownLA!\",\"href\":\"http://www.facebook.com/DowntownLA.com/\"}]";
dialog.attachment = [NSString stringWithFormat:
#"{ \"name\":\"%#\","
"\"href\":\"%#\","
"\"caption\":\"%#\",\"description\":\"%#\","
"\"media\":[{\"type\":\"image\","
"\"src\":\"%#\","
"\"href\":\"%#\"}],"
"\"properties\":{\"%#\":{\"text\":\"%#\",\"href\":\"%#\"}}}", #"name: priya",#"href nothing",#"cation nothing",#"description nothin", #"imageSource nothing ", #"imageHref nothing", #"linkTitle nothing", #"linkText nothing", #"linkHref nothing"];
[dialog show];
Please tel me wher m going wrong !!!!!
The error m getting on simulator is : Application Response Error - The post action links must be valid URls. You can see this because you are one of the developers of the app.
i have upload image in this way
- (void) postOnFBWall
{
NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:2];
//create a UIImage (you could use the picture album or camera too)
UIImage *picture = imgUploadPhoto.image;
//create a FbGraphFile object insance and set the picture we wish to publish on it
FbGraphFile *graph_file = [[FbGraphFile alloc] initWithImage:picture];
//finally, set the FbGraphFileobject onto our variables dictionary....
[variables setObject:graph_file forKey:#"file"];
NSString * messageValue = [NSString stringWithFormat:#"Menu Name: %#, Restaurant: %#, Category: %#, Comment: %#",txtNameofMenu.text,txtRestaurant.text,categoryLbl.text,txtViewComment.text];
[variables setObject:[NSString stringWithFormat:#"%#",messageValue] forKey:#"message"];
//the fbGraph object is smart enough to recognize the binary image data inside the FbGraphFile
//object and treat that is such.....
FbGraphResponse *fb_graph_response = [[FBRequestWrapper defaultManager] doGraphPost:#"me/photos" withPostVars:variables];
NSLog(#"postPictureButtonPressed: %#", fb_graph_response.htmlResponse);
}
Related
I got this error-
ostPictureButtonPressed: {"error":{"message":"(#100) param tags must be non-empty.","type":"OAuthException","code":100}}
My code is
NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:6];
UIImage *picture = [UIImage imageNamed:#"Default.png"];
//create a FbGraphFile object insance and set the picture we wish to publish on it
FbGraphFile *graph_file = [[FbGraphFile alloc] initWithImage:picture];
//finally, set the FbGraphFileobject onto our variables dictionary....
[variables setObject:graph_file forKey:#"file"];
NSArray *arr=[[NSArray alloc]initWithObjects:#"100001912715296", nil ];
SBJsonWriter *writer = [[SBJsonWriter alloc] init];
NSString *jsonString = [writer stringWithObject:arr];
NSLog(#"The jason string is %#",jsonString);
[variables setObject:jsonString forKey:#"tags"];
[variables setObject:#"110506962309835" forKey:#"place"];
[variables setObject:#"Hello friends I am using MySafety App.\n This is a must have app for all peoples whether you are a working lady, a house wife or a school going girl.This app lets you save 6 emergency contacts whom you want to alert in case of emergency.Whenever you feel you are in danger, just press the SOS button and it will send sms to your contacts with your current location and a alarm will go on with flashing lights" forKey:#"message"];
//the fbGraph object is smart enough to recognize the binary image data inside the FbGraphFile
//object and treat that is such.....
FbGraphResponse *fb_graph_response = [fbGraph doGraphPost:#"117795728310/photos" withPostVars:variables];
NSLog(#"fb_graph_response %#",fb_graph_response);
NSLog(#"postPictureButtonPressed: %#", fb_graph_response.htmlResponse);
I had done following code with reference to following link to create batch request for uploading multiple photos in Facebook.
I had got some solution for uploading multiple photos on Facebook through this Facebook graph API.
CODE:
NSString *jsonRequest1 = #"{ \"method\": \"POST\", \"relative_url\": \"me/photos\" , \"body\": \"Hello 1\", \"attached_files\": \"file1\" }";
NSString *jsonRequest2 = #"{ \"method\": \"POST\", \"relative_url\": \"me/photos\" , \"body\": \"Hello 2\", \"attached_files\": \"file2\" }";
NSString *jsonRequestsArray = [NSString stringWithFormat:#"[ %#, %# ]", jsonRequest1, jsonRequest2];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:jsonRequestsArray,#"batch",nil];
[params setObject:UIImagePNGRepresentation(self.image1) forKey:#"file1"];
[params setObject:UIImagePNGRepresentation(self.image2) forKey:#"file2"];
[objFacebook requestWithGraphPath:#"me" andParams:params andHttpMethod:#"POST" andDelegate:self];
Now when I am running this code I got the following output.
Result Dictionary in - (void)request:(FBRequest *)request didLoad:(id)result
(
{
body = "{\"error\":0,\"error_description\":\"File file1 has not been attached\"}";
code = 400;
headers = (
{
name = "HTTP/1.1";
value = "400 Bad Request";
},
{
name = "Content-Type";
value = "text/javascript; charset=UTF-8";
}
);
},
{
body = "{\"error\":0,\"error_description\":\"File file2 has not been attached\"}";
code = 400;
headers = (
{
name = "HTTP/1.1";
value = "400 Bad Request";
},
{
name = "Content-Type";
value = "text/javascript; charset=UTF-8";
}
);
}
)
I don't know how this files attached.. Can anyone help me to figure out this problem.
Is there any change in my code then please let me know.
Thanks in advance...
I recommend you use the new integrated Facebook API's in iOS 6 to do this instead of Facebook's API: https://developer.apple.com/videos/wwdc/2012/?id=306 there's a golden WWDC video for all social tasks. Plus using the new API's in iOS 6 is MUCH faster then using Facebooks.
For your NSDictionary of parameters ,use the NSData representation, instead of directly using the UIImage object.
Depending on the compression format of the images you are trying to upload, you'll need to use a different method to convert the UIImage to NSData. Here are two techniques, one for PNG and one for JPEG
//...cut...
[params setObject:UIImageJPEGRepresentation(self.jpegImage, 0.8) forKey:#"file1"];
[params setObject:UIImagePNGRepresentation(self.pngImage) forKey:#"file2"];
//...cut...
This is a tutorial from the Facebook Developer blog that shows something similar (although it is for a video, and not the batch API), and one significant difference from your code is that the parameter passed is in NSData representation, not UIImage.
http://developers.facebook.com/blog/post/532/
[params setObject:UIImagePNGRepresentation(self.image1) forKey:#"file1"];
This should not be the data, it should be the path for that images. It will work fine if you provide a path instead of NSData.
I got the solution for multiple file upload using FBgraph.
here is my code.
-(void) fbGraphCallback
{
FbGraphFile *graph_file = [[FbGraphFile alloc]initWithImage:imgView1.image];
FbGraphFile *graph_file1 = [[FbGraphFile alloc]initWithImage:imgView2.image];
NSString *jsonRequest1 = #"{ \"method\": \"POST\", \"relative_url\": \"me/photos\" , \"body\": \"First Image\", \"attached_files\": \"file1\" }";
NSString *jsonRequest2 = #"{ \"method\": \"POST\", \"relative_url\": \"me/photos\" , \"body\": \"Second Image\", \"attached_files\": \"file2\" }";
NSString *jsonRequestsArray = [NSString stringWithFormat:#"[ %#, %# ]", jsonRequest1, jsonRequest2];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:jsonRequestsArray,#"batch",nil];
[params setObject:graph_file forKey:#"file1"];
[params setObject:graph_file1 forKey:#"file2"];
[fbgraph doGraphPost:#"" withPostVars:params];
}
Thanks mehul.
You are adding the binary to the Dictionary incorrectly. Take a look at the file upload method in Facebook's sample app. You should be able to easily adopt Facebook's sample code to your app.
/**
* Upload a photo.
*/
-(IBAction)uploadPhoto:(id)sender
{
NSString *path = #"http://www.facebook.com/images/devsite/iphone_connect_btn.jpg";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
img, #"picture",
nil];
[_facebook requestWithMethodName:#"photos.upload"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
[img release];
}
I want to send message with image data. So I used MFMessageComposeViewController.
But that controller provide only SMS service. So I used UIPasteBoard attached an image data.
But It doesn't work, either. There are no "Paste" button created when typing messages. Attaching image at UIPasteBoard was clearly success.
I think using MFMessageComposeViewController doesn't solve my problem.
How can I accomplish my goal?
This is not possible with the current MessageUI API: the MSMessageComposeViewController doesn't accept attachments like the MFMailComposeViewController does.
The only way to do this currently is to use an external service that allows you to send mms via a REST call for example.
GSMA defines a REST specification for exactly this purpose:
http://www.gsmworld.com/oneapi/reference_documentation-version_1.html (multiple pdf's on this page)
Try to find a local service provider that implements this specification and you're good to go.
Just to add the direct wiki link to the OneAPI MMS spec: http://gsma.securespsite.com/access/Access%20API%20Wiki/MMS%20RESTful%20API.aspx and a link to the PHP/Java sandbox https://github.com/OneAPI/GSMA-OneAPI where MMS can be tested locally . Cheers.
Here is the correct working code and it is working perfectly on my device.
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.persistent = NO;
NSMutableDictionary *text = [NSMutableDictionary dictionaryWithCapacity:1];
[text setValue:label.text forKey:(NSString *)kUTTypeUTF8PlainText];
NSMutableDictionary *image = [NSMutableDictionary dictionaryWithCapacity:1];
[image setValue:imageView.image forKey:(NSString *)kUTTypePNG];
pasteboard.items = [NSArray arrayWithObjects:image,text, nil];
NSString *phoneToCall = #"sms:";
NSString *phoneToCallEncoded = [phoneToCall stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [[NSURL alloc] initWithString:phoneToCallEncoded];
[[UIApplication sharedApplication] openURL:url];
I had the same question that I posted here. There is a bug in MFMessageComposeViewController and if you just use the code below it will launch a message that you can insert images into
NSString *phoneToCall = #"sms: 123-456-7890";
NSString *phoneToCallEncoded = [phoneToCall stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [[NSURL alloc] initWithString:phoneToCallEncoded];
[[UIApplication sharedApplication] openURL:url];
This Method is tested and verified. I Used it in my code.
if (![MFMessageComposeViewController canSendText]) {
UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Your device not support SMS \nOr you hadn't login your iMessage" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alertV show];
return;
}
MFMessageComposeViewController *mVC = [[MFMessageComposeViewController alloc] init];
mVC.body = #"jjjj";
mVC.recipients = #[#"00XXXXXXXXXX"];
mVC.messageComposeDelegate = self;
if ([MFMessageComposeViewController canSendAttachments]) {
NSLog(#"ok");
}
[mVC addAttachmentData: UIImageJPEGRepresentation([UIImage imageNamed:#"test.jpg"], 1.0) typeIdentifier:#"public.data" filename:#"image.jpeg"];
[self presentViewController:mVC animated:YES completion:nil];
You can use any jpeg jpg and png formats.
Swift way. Works in iOS11
func shareViaMessage() {
if !MFMessageComposeViewController.canSendText() {
showAlert("Text services are not available")
return
}
let textComposer = MFMessageComposeViewController()
textComposer.messageComposeDelegate = self
textComposer.body = "Try my #app"
if MFMessageComposeViewController.canSendSubject() {
textComposer.subject = "AppName"
}
if MFMessageComposeViewController.canSendAttachments() {
let imageData = UIImageJPEGRepresentation(imageView.image!, 1.0)
textComposer.addAttachmentData(imageData!, typeIdentifier: "image/jpg", filename: "photo.jpg")
}
present(textComposer, animated: true)
}
Why don't you share the Image and Text via the Share API (selecting Message, and if you want exluding Facebook, twitter etc..)
I am making an iphone game , In that I want to upload score on face book. I am new to face book codes, I've got it's API from github.com. But I don't know how to write or post directly my message on wall of face book of log in account else how to share my score on facebook. I've done log in portion.
Can any one help me????
Please refer the facebook api development at http://wiki.developers.facebook.com/index.php/Facebook_iPhone_SDK
Sample application also given by facebook for iPhone.
Thanks,
Jim.
Assuming the user has allready logged in and you have a vaild faceb0ok session, you could use something like this to get you started:
- (void) post {
postingDialog = [[[FBStreamDialog alloc] init] autorelease];
postingDialog.delegate = self;
postingDialog.userMessagePrompt = #"Prompt the User:";
NSString *name = #"Name of thing"
NSString *href = #"http://www.example.com"
NSString *caption = #"This is a caption"
NSString *description = #"This is a description";
NSString *imageSource = #"http://example.com/1.png";
NSString *imageHref = #"http://example.com/1.png";
NSString *linkTitle = #"Link title";
NSString *linkText = #"Text";
NSString *linkHref = #"http://www.example.com/iphone";
postingDialog.attachment = [NSString stringWithFormat:
#"{ \"name\":\"%#\","
"\"href\":\"%#\","
"\"caption\":\"%#\",\"description\":\"%#\","
"\"media\":[{\"type\":\"image\","
"\"src\":\"%#\","
"\"href\":\"%#\"}],"
"\"properties\":{\"%#\":{\"text\":\"%#\",\"href\":\"%#\"}}}", name, href, caption, description, imageSource, imageHref, linkTitle, linkText, linkHref];
[postingDialog show];
}
Here Iam having a problem.Actually I implemented the facebook integration in my application and I need to post the images with text but I dont have any idea how to work on this.can anyone suggest this with a sample code so that it is very helpful for me.
Anyone's help will be much appreciated.
I assume that you want to draw some text in an image, and then upload the image to Facebook.
At first, we need to draw the original image and the desired text into a new image.
UIGraphicsBeginImageContext(CGSizeMake(320.0, 320.0));
CGContextRef context = UIGraphicsGetCurrentContext();
// Draw the original image
[image drawInRect:CGRectMake(0, 0, 320.0, 320.0)];
// Draw the text
[#"text" drawInRect:CGRectMake(...) withFont:[UIFont systemFontOfSize:20.0];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
And then, convert the image into NSData and call Facebook's "photos.upload" API to upload it.
NSMutableDictionary *args = [[[NSMutableDictionary alloc] init] autorelease];
[args setObject:#"caption" forKey:#"caption"];
FBRequest *uploadPhotoRequest = [FBRequest requestWithDelegate:self];
NSData *data = UIImagePNGRepresentation(newImage);
[uploadPhotoRequest call:#"photos.upload" params:args dataParam:data];
If you want to upload the images to your server, and post a small story to Facebook's wall. Use the stream API.
FBStreamDialog *dialog = [[[FBStreamDialog alloc] init] autorelease];
dialog.delegate = self;
dialog.userMessagePrompt = #"Prompt";
NSString *name = #"Your caption";
NSString *src = #"http://example.com/path/of/your/image";
NSString *href = #"http://what/happens/if/the/user/click/on/the/image";
NSString *attachment = [NSString stringWithFormat:#"{\"name\":\"%#\",\"media\":[{\"type\":\"image\", \"src\":\"%#\", \"href\":\"%#\"}]}", name, src, href];
dialog.attachment = attachment;
[dialog show];
Maybe you would be happy using BMSocialShare. It's a simple lib I wrote.
BMFacebookPost *post = [[BMFacebookPost alloc]
initWithTitle:#"Simple sharing via Facebook, Email and Twitter for iOS!"
descriptionText:#"Posting to Facebook, Twitter and Email made dead simple on iOS. Simply include BMSocialShare as a framework and you are ready to go."
andHref:#"https://github.com/blockhaus/BMSocialShare"];
[post setImageUrl:#"http://www.blockhausmedien.at/images/logo-new.gif"
withHref:#"http://www.blockhaus-media.com"];
[[BMSocialShare sharedInstance] facebookPublish:post];