Skip to content

Instantly share code, notes, and snippets.

@andrewzimmer906
Created July 19, 2012 17:44
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save andrewzimmer906/3145581 to your computer and use it in GitHub Desktop.
Save andrewzimmer906/3145581 to your computer and use it in GitHub Desktop.
Post Photo and Message wall with Facebook iOS6 SDK
// Start here https://developers.facebook.com/docs/getting-started/getting-started-with-the-ios-sdk/
// Install the Facebook SDK, get it linked up, create an app, and get that setup correctly
// in your info.plist, appDelegate, and build settings.
//Note: This example uses ARC. If you aren't using ARC, made sure to release/retain objects as needed.
//Now for the fun part.
////////////////////////////
// Step 1. Login with permissions.
////////////////////////////
-(void)loginThenPost {
NSArray *permissions = [NSArray arrayWithObjects:@"publish_actions",
@"user_photos",
nil];
[FBSession sessionOpenWithPermissions:permissions
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
if(error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Problem connecting with Facebook" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[alert show];
} else {
[self postPhoto];
}
}];
}
////////////////////////////
// Step 2. Post that photo to FB.
////////////////////////////
-(void)postPhoto {
// We're going to assume you have a UIImage named image_ stored somewhere.
FBRequestConnection *connection = [[FBRequestConnection alloc] init];
// First request uploads the photo.
FBRequest *request1 = [FBRequest
requestForUploadPhoto:image_];
[connection addRequest:request1
completionHandler:
^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
}
}
batchEntryName:@"photopost"
];
// Second request retrieves photo information for just-created
// photo so we can grab its source.
FBRequest *request2 = [FBRequest
requestForGraphPath:@"{result=photopost:$.id}"];
[connection addRequest:request2
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error && result) {
NSString *ID = [result objectForKey:@"id"];
[self postDataWithPhoto:ID];
}
}
];
[connection start];
}
////////////////////////////
// Step 3. Post message with linked photo to the user's wall.
// We're going to post to the open graph 'user/feed' stream.
// Go here https://developers.facebook.com/docs/reference/api/ and do a page find for
// '/PROFILE_ID/feed' to get extra information about the parameters accepted in this call.
////////////////////////////
-(void)postDataWithPhoto:(NSString*)photoID {
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setObject:@"I'm totally posting on my own wall!" forKey:@"message"];
if(photoID) {
[params setObject:photoID forKey:@"object_attachment"];
}
[FBRequest startForPostWithGraphPath:@"me/feed"
graphObject:[NSDictionary dictionaryWithDictionary:params]
completionHandler:
^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
[[[UIAlertView alloc] initWithTitle:@"Result"
message:@"Your update has been posted to Facebook!"
delegate:self
cancelButtonTitle:@"Sweet!"
otherButtonTitles:nil] show];
} else {
[[[UIAlertView alloc] initWithTitle:@"Error"
message:@"Yikes! Facebook had an error. Please try again!"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil] show];
}
}
];
}
@chinabala
Copy link

wondering if it's possible to post a photo-comment in iOS sdk? *e.g. reply comment with photo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment