Question

Im trying to use the social framework to present the "Post To Facebook" view controller from within Cocos2d. This is the code I would usually use in a storyboard app

if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
    SLComposeViewController *facebook = [[SLComposeViewController alloc] init];
    facebook = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    [facebook setInitialText:[NSString stringWithFormat:@"text"]];
    [self presentViewController:facebook animated:YES completion:nil];
    [facebook setCompletionHandler:^(SLComposeViewControllerResult result) {
        NSString *output;
        switch (result) {
            case SLComposeViewControllerResultCancelled:
                output = @"Action Cancelled";
                break;
            case SLComposeViewControllerResultDone:
                output = @"Posted";
                NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
                [ud setInteger:1 forKey:@"Shared"];
                [ud synchronize];
            default:
                break;
        }
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Facebook" message:output delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
        [alert show];
    }];
}

How would I get this up and running from within Cocos2d? currently it throws up a warning for the line

[self presentViewController:facebook animated:YES completion:nil];

Thanks in advance

Was it helpful?

Solution

In cocos2d 2.0 you can use [CCDirector sharedDirector] instead of self.

[[CCDirector sharedDirector] presentViewController:facebook animated:YES completion:nil];

This works because CCDirector inherits from UIViewController.

OTHER TIPS

This works for me....

-(void) facebookWithInitialText:(NSString*) text {

if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {

    CCLOG( @"can post to Facebook");

    AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];

    SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

    [controller setInitialText:text]; // set initial text 
    [controller addImage:[UIImage imageNamed:@"Icon-72.png"]];  //add an image 
    [controller addURL:[NSURL URLWithString:@"http://www.cartoonsmart.com"]];  //add a URL to it

    [[app navController] presentViewController:controller animated:YES completion:nil ];


    [controller setCompletionHandler:^(SLComposeViewControllerResult result){

        [[app navController] dismissModalViewControllerAnimated:YES];

        NSString *outout = [[NSString alloc] init];

        switch (result) {
            case SLComposeViewControllerResultCancelled:
                outout = @"Post Cancled";
                break;
            case SLComposeViewControllerResultDone:
                outout = @"Post Done";

            default:
                break;
        }
        UIAlertView *myalertView = [[UIAlertView alloc]initWithTitle:@"Facebook" message:outout delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [myalertView show];
    }];

}  else {

    CCLOG( @"Facebook not accessible or one account not setup.");
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top