ライブラリ追加
- Twitter.framework
実装
Hoge.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import <Twitter/Twitter.h>
@interface Hoge : CCLayer {
UIViewController* viewController;
}
@property(nonatomic,retain) UIViewController* viewController;
@end
Hoge.m
@synthesize viewController;
-(id) init
{
self = [super init];
if (self) {
CGSize screenSize = [[CCDirector sharedDirector]
// uiview for twitter
viewController = [[UIViewController alloc] init];
// メニュー
CCMenuItemFont *menuTwitter = [CCMenuItemFont itemFromString:@"つぶやく" target:self selector: @selector(onTwitter:)];
CCMenu *menu = [CCMenu menuWithItems:menuTwitter, nil];
menu.position = ccp(160, screenSize.height - 200);
[menu alignItemsVerticallyWithPadding: 20.0f];
[self addChild:menu z:1];
self.isTouchEnabled = YES;
}
return self;
}
- (void) onTwitter:(id)sender
{
if ([TWTweetComposeViewController canSendTweet]) // Check if twitter is setup and reachable
{
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
// set initial text
[tweetViewController setInitialText:@"Greetings Twitterverse!"];
// setup completion handler
tweetViewController.completionHandler = ^(TWTweetComposeViewControllerResult result) {
if(result == TWTweetComposeViewControllerResultDone) {
// the user finished composing a tweet
} else if(result == TWTweetComposeViewControllerResultCancelled) {
// the user cancelled composing a tweet
}
[viewController dismissViewControllerAnimated:YES completion:nil];
};
// present view controller
[[[CCDirector sharedDirector] openGLView] addSubview:viewController.view];
[viewController presentViewController:tweetViewController animated:YES completion:nil];
}
else
{
// Twitter account not configured, inform the user
NSLog(@"NO TWITTER ACCOUNT CONFIGURED. DO SOMETHING");
UIAlertView* alertView =
[[[UIAlertView alloc] initWithTitle: @"未登録"
message: @"ついったーにあかうんと登録してください?"
delegate: self
cancelButtonTitle: nil
otherButtonTitles: @"OK", nil] autorelease];
[alertView show];
}
}