2

我试图使用社交框架从 Cocos2d 中呈现“发布到 Facebook”视图控制器。这是我通常在故事板应用程序中使用的代码

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];
    }];
}

我将如何在 Cocos2d 中启动并运行它?目前它会对该行发出警告

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

提前致谢

4

2 回答 2

10

在 cocos2d 2.0 中,您可以[CCDirector sharedDirector]使用self.

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

这是有效的,因为CCDirector继承自UIViewController.

于 2012-11-30T04:43:51.083 回答
0

这对我有用....

-(无效)facebookWithInitialText:(NSString *)文本{

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.");
}

}

于 2013-03-25T15:11:30.513 回答