8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

cocos2DでTwitter処理

Last updated at Posted at 2012-07-16

ライブラリ追加

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

Reference

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?