我试图使用社交框架在Cocos2d中呈现“Post To 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];
提前致谢
答案 0 :(得分:10)
在cocos2d 2.0中,您可以使用[CCDirector sharedDirector]
代替self
。
[[CCDirector sharedDirector] presentViewController:facebook animated:YES completion:nil];
这是有效的,因为CCDirector
继承自UIViewController
。
答案 1 :(得分:0)
这对我有用....
- (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.");
}
}