Post a picture on facebook - objective-c

I want to publish a picture with a title (#name) and a texte (#caption) on facebook when my quiz is over.
I try to put my picture in an UIImage then i called her with #picture int the attachment but it doesn't work at all.
Have you a piece of code to show me how i can do this?
Thank you
Flo

This works for me :
- (void) uploadPhoto:(UIImage *) img caption:(NSString *) caption delegate:(NSObject<FBRequestDelegate> *) delegate{
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
img, #"picture",
nil];
if (caption != nil) {
[params setObject:params forKey:#"message"];
}
[self requestWithGraphPath:#"me/photos"
andParams:params
andHttpMethod:#"POST"
andDelegate:delegate];
}

I think you have a typo in your method. I believe:
[params setObject:params forKey:#"message"];
should read:
[params setObject: caption forKey:#"message"];

Related

How to tag users in a photo using the Facebook iOS SDK?

I can't seem to work out how to tag users in a Facebook photo upload.
The documentation seems to suggest that you use an array, but the following code doesn't parse correctly (causes an application crash)
- (void)uploadImage:(UIImage *)img
withTags:(NSArray *)tags
{
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"msgstring", #"message",
img, #"picture",
nil];
if (tags) {
[params setObject:tags
forKey:#"tags"];
}
self.requestType = FBAssistantRequestImageUpload;
[self.facebook requestWithGraphPath:#"me/photos"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
}
It works fine without the tags. The array at the moment contains a single string with the identifier of the friend I wish to tag.
I assume I'm adding the tags incorrectly. I was hoping to avoid having to use the three-step method outlined here: Tag Friends in Facebook Photo Upload, as I believe that requires photos permission, which just posting the photo doesn't need.
here's the code I use to tag friends on photos:
NSMutableArray *tags = [[NSMutableArray alloc] init];
NSString *tag = nil;
if(self.selectedFriends != nil){
for (NSDictionary *user in self.selectedFriends) {
tag = [[NSString alloc] initWithFormat:#"{\"tag_uid\":\"%#\"}",[user objectForKey:#"id"] ];
[tags addObject:tag];
}
NSString *friendIdsSeparation=[tags componentsJoinedByString:#","];
NSString *friendIds = [[NSString alloc] initWithFormat:#"[%#]",friendIdsSeparation ];
[params setObject:friendIds forKey:#"tags"];
}

Photo Not Uploading on Check In Using Facebook iOS

So I'm creating an iPhone app that needs to be able to upload a photo when the user checks into a location using the Facebook Graph API.
Right now my code is this:
if (![delegate.facebook isSessionValid])
[delegate.facebook authorize:[NSArray arrayWithObjects:#"publish_stream", #"offline_access", #"publish_checkins", nil]];
parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:[placeDictionary objectForKey:#"id"] , #"place",
serialisedCoordinates, #"coordinates",
message, #"message",
pickedImage, #"picture",
#"Via the official REDACTED app", #"application", nil];
[delegate.facebook requestWithGraphPath:#"me/checkins" andParams:parameters andHttpMethod:#"POST" andDelegate:fbCheckInResultHandler];
Where 'pickedImage' is the UIImage returned from a UIImagePickerController.
Even when I pick an image (i.e., pickedImage != nil), no picture is uploaded when checked in. The check in appears on Facebook with the message, coordinates and app information, just no image.
Really hope someone can help.
Cheers,
Kiran
Here is the whole function that is being called when a checkin is made:
-(void)fbPostCheckInWithMessage:(NSString *)message andFriends:(NSArray *)friends {
if (![delegate.facebook isSessionValid]) {
NSLog(#"Session invalid");
[delegate.facebook authorize:[NSArray arrayWithObjects:#"publish_stream", #"offline_access", #"publish_checkins", nil]];
} else {
NSLog(#"Session valid");
}
NSMutableString *friendsIDString;
friendsIDString = [NSMutableString stringWithFormat:#"%#", [[friends objectAtIndex:0] userID]];
if ([friends count] > 1) {
for (User *f in taggedFriends) {
if (f != [taggedFriends objectAtIndex:0]) {
[friendsIDString appendString:[NSString stringWithFormat:#", %#", f.userID]];
}
}
}
NSLog(#"Tagged Friends: %#", friendsIDString);
SBJSON *jsonWriter = [SBJSON new];
NSMutableDictionary *locationDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:#"%f", userLocation.coordinate.latitude], #"latitude",
[NSString stringWithFormat:#"%f", userLocation.coordinate.longitude], #"longitude", nil];
NSString *serialisedCoordinates = [jsonWriter stringWithObject:locationDictionary];
NSData *pictureData = UIImagePNGRepresentation(pickedImage);
NSLog(#"picture: %#", pictureData);
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:[placeDictionary objectForKey:#"id"] , #"place",
serialisedCoordinates, #"coordinates",
pictureData, #"message",
pickedImage, #"picture",
#"Via the official REDACTED app", #"application", nil];
[delegate.facebook requestWithGraphPath:#"me/checkins" andParams:parameters andHttpMethod:#"POST" andDelegate:fbCheckInResultHandler];
}
I am using the friendsIDString to get the IDs of the friends the user with. I've removed this functionality from the example here because it was all commented out because I was trying to isolate what was causing the problem (which was the photo tagging). Just wanted to clear that up.
--UPDATE--
Here's how I'm setting pickedImage. I use the delegate methods from the UIImagePickerController:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
NSLog(#"Picked image");
pickedImage = [[UIImage alloc] init];
pickedImage = image;
[imagePickerController dismissModalViewControllerAnimated:YES];
}
You can upload photos to facebook using :- UIImagePNGRepresentation,UIImageJPEGRepresentation,etc based on type of image or imagewithdata from UIImage class..
NSData *yourImageData= UIImagePNGRepresentation(yourImage);
Initialize the dictionary :-
NSMutableDictionary *mutableDictWithParam= [NSMutableDictionary dictionaryWithObjectsAndKeys:#"Your text", #"message", yourImageWithData, #"theSource", nil];
finally send post :-
[facebook requestWithGraphPath:#"/me/photos" andParams:mutableDictWithParam andHttpMethod:#"POST" andDelegate:self];
In you app i think you have not initialized your NSData object(pickedimage) ..else everything seems fine.
As Per Our Discussion You can use this to compress image:-
NSData* UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);
NSData *compressedImage = UIImagePNGRepresentation(yourIMage, .5)//quality is ensured through second argument lies between o and 1

MGTwitterEngine send retweet is not working

I'm using MGTwitterEngine. I've found this post where shows a modification to the current version of MGTwitterEngine.
I've applied this modification and when I'm executing the method returns this:
2012-04-10 01:04:17.908 Otsuka On[27519:707] INFO -> retweet response: 07695198-2526-4FF4-BC46-8D39F7719836
But nothing happens in the timeline of the Twitter account.
The method added to TwitterEngine is:
- (NSString *)sendRetweet:(unsigned long)updateID
{
if (updateID == 0){
return nil;
}
NSString *path = [NSString stringWithFormat:#"statuses/retweet/%u.%#", updateID, API_FORMAT];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
[params setObject:[NSString stringWithFormat:#"%u", updateID] forKey:#"id"];
NSString *body = [self _queryStringWithBase:nil parameters:params prefixed:NO];
return [self _sendRequestWithMethod:HTTP_POST_METHOD path:path
queryParameters:params body:body
requestType:MGTwitterUpdateSendRequest
responseType:MGTwitterStatus];
}
Anyone knows what I'm doing wrong?
Thanks.
I use the following method and it works for me:
- (NSString *)sendRetweet:(MGTwitterEngineID)tweetID {
NSString *path = [NSString stringWithFormat:#"statuses/retweet/%llu.%#", tweetID, API_FORMAT];
return [self _sendRequestWithMethod:HTTP_POST_METHOD path:path
queryParameters:nil body:nil
requestType:MGTwitterRetweetSendRequest
responseType:MGTwitterStatus];
}
UPDATE: Yes, try to use this fork: github.com/mattgemmell/MGTwitterEngine
Finally I've solved the problem using NSString parameter as updateID:
- (NSString *)sendRetweet:(NSString *)updateID
{
NSString *path = [NSString stringWithFormat:#"statuses/retweet/%#.%#", updateID, API_FORMAT];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
[params setObject:[NSString stringWithFormat:#"%#", updateID] forKey:#"id"];
NSString *body = [self _queryStringWithBase:nil parameters:params prefixed:NO];
return [self _sendRequestWithMethod:HTTP_POST_METHOD path:path
queryParameters:params body:body
requestType:MGTwitterUpdateSendRequest
responseType:MGTwitterStatus];
}

Tag Friends in Facebook Photo Upload

I'd like to be able to tag existing friends using the graph api:
Here's the code I have at the moment. The photo is being uploaded, but the photo isn't tagging the user specified in the user_id:
UIImage *testImage = [UIImage imageNamed:#"sendingTo"];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:kFacebookFBConnectAppID, #"app_id",
testImage, #"source",
#"1381470076", #"message_tags",
#"TEST!", #"message", nil];
[self.socialIntegration.facebook requestWithGraphPath:[NSString stringWithFormat:#"/me/photos?access_token=%#", self.socialIntegration.facebook.accessToken]
andParams:params
andHttpMethod:#"POST" andDelegate:self];
Is the message_tags attribute not the correct attribute to use?
Thanks!
EDIT
From what I see here (https://developers.facebook.com/docs/reference/api/photo/#tags), it looks like I need to make three calls in total:
Post the Photo with the code I already have
Ask Facebook to give me the ID of this photo (which i can probably get from the FBRequestDelegate)
Tag People after posting.
ok, figured it out.
Here's how you do it.
First, you upload the image.
UIImage *testImage = [UIImage imageNamed:#"sendingTo"];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:kFacebookFBConnectAppID, #"app_id",
testImage, #"source",
#"TEST!", #"message", nil];
[self.socialIntegration.facebook requestWithGraphPath:[NSString stringWithFormat:#"/me/photos?access_token=%#", self.socialIntegration.facebook.accessToken]
andParams:params
andHttpMethod:#"POST" andDelegate:self];
Next, upon successful upload, the - (void)request:(FBRequest *)request didLoad:(id)result method will return a dictionary result with 1 key id. That ID is the photoID of the photo you just uploaded, which you save into a string:
NSString *photoID = [NSString stringWithFormat:#"%#", [(NSDictionary*)result valueForKey:#"id"]];
Then make another GraphAPI request to tag your friends. In the code below I am tagging one specific friends, but to tag multiple friends use CSV string or array:
[self.socialIntegration.facebook requestWithGraphPath:[NSString stringWithFormat:#"%#/tags/%#?access_token=%#", photoID, #"1381470076", self.socialIntegration.facebook.accessToken]
andParams:nil
andHttpMethod:#"POST" andDelegate:self];

iOS Facebook Connect news feed

I need to get the user news feed data.
Currently I'm using this:
[[FBManager defaultManager]getFBRequestWithGraphPath:#"me/home" params: andDelegate:self];
and then:
- (void)request:(FBRequest *)request didLoad:(id)result {
if ([result isKindOfClass:[NSArray class]])
result = [result objectAtIndex:0];}
result gives me a bunch of data. But how do Extract these individually? Maybe the name and message for example??
I'd change from using requestWithGraphPath to requestWithMethodName.
Here's the sample code:
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:#"SELECT post_id, actor_id, message FROM stream WHERE filter_key = 'others '", #"query",
nil];
[facebook requestWithMethodName: #"fql.query"
andParams: params
andHttpMethod: #"POST"
andDelegate: self];
Then in the request didLoad method:
- (void)request:(FBRequest *)request didLoad:(id)result
{
if ([result isKindOfClass:[NSArray class]])
result = [result objectAtIndex:0];
friendID = [[result objectForKey:#"actor_id"]copy];
friendMsg = [[result objectForKey:#"message"]copy];
}
Facebook documentations on the iOS FB Connect SDK are very blend. It helps if you know a bit of SQL(fql) :)