facebookuiviewcontrollercocos2d-iphone

Present Social View Controller Cocos2d


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


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.