raptor raptor - 5 months ago 21
iOS Question

How to share text and image on facebook wall (objective C)

I am working on an app which provides user to share the details of their app into facebook . I have UITableview where it contains the list of author name and when u click on book name it goes to another

UITableView
where it shows the different book of that author in each
TableViewCell
. I'm able to display the list in 2nd
UITableView
.
I wanted to know how to share the author name , book name and the image (2nd tableview details).
I'm showing the code which does that ..
So when the user wants to share the details, he will have to tap on the
UIButton
which is there in every cell of author
UITableView
.

didselectatindexpath

NSDictionary *selectedAuthor = nil;

NSArray *sectionArray=[mainIndexDictionary objectForKey:[allKeysArray objectAtIndex:indexPath.section]];
selectedAuthor = [sectionArray objectAtIndex:indexPath.row];
iPath=[[selectedAuthor objectForKey:@"SymptIndexID"] intValue];
NSLog(@"iPath - %d",iPath);

authortitleString=[[selectedAuthor objectForKey:@"authorIndexName"]stringByAppendingString:@" cure!"];
}


bookArray=[objDB customCellData:(NSInteger)iPath];
[self.view bringSubviewToFront:authorView];
[bookTableView scrollRectToVisible:CGRectMake(0.0, 0.0, 320.0,10.0) animated:NO];

[bookTableView reloadData]

;

Answer

In ios 6.0 you can achieve as follows (you must add the social framework to your project)

 #import <Social/Social.h>

 - (void)ShareFacebook
 {
    SLComposeViewController *fbController=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
    SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result){

        [fbController dismissViewControllerAnimated:YES completion:nil];

        switch(result){
            case SLComposeViewControllerResultCancelled:
            default:
            {
                NSLog(@"Cancelled.....");
               // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs://"]];

            }
                break;
            case SLComposeViewControllerResultDone:
            {
                NSLog(@"Posted....");
            }
                break;
        }};


    [fbController setInitialText:@"This is My Sample Text"];


    [fbController setCompletionHandler:completionHandler];
    [self presentViewController:fbController animated:YES completion:nil];
}
else{
    UIAlertView *alert = [[[UIAlertView alloc]initWithTitle:@"Sign in!" message:@"Please first Sign In!" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil]autorelease];
    [alert show];
 }
}
Comments