2015年7月13日星期一

[笔记][08723] TwitterDemo

TwitterMediaDemo(包括推图片、视频的功能):
https://github.com/1992chenlu/TwitterMediaDemo

包括Timeline的Demo的GitHub链接:
https://github.com/1992chenlu/CLTwitterDemo 
(这个要配置好Fabric才可以跑)


1. Post Tweet with Photo

- (IBAction)tweetImage:(id)sender {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        
        SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
        [tweetSheet setInitialText:@"#とある科学の超電磁砲# Twitter Demo Testing! Hahaha! "];
        
        //Add image
        [tweetSheet addImage:[UIImage imageNamed:@"mikoto.jpg"]];
        
        //Add URL
        [tweetSheet addURL:[NSURL URLWithString:@"http://codechen.blogspot.com/"]];
        
        [self presentViewController:tweetSheet animated:YES completion:nil];
    }
    else
    {
        NSLog(@"Failed");
    }
}

2. Post Tweet with Video

用到一个别人制作的SocialVideoHelper,GitHub链接:
https://github.com/liu044100/SocialVideoHelper
调用uploadTwitterVideo这个函数即可。
- (IBAction)tweetVideo:(id)sender {
    if (_video == nil) {
        NSLog(@"No Video to upload!");
        return;
    }
    
    NSData *data = [NSData  dataWithContentsOfURL:_video];
    
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    [accountStore requestAccessToAccountsWithType:accountType options:NULL completion:^(BOOL granted, NSError *error) {
        if(granted) {
            NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
            if ([accountsArray count] > 0) {
                ACAccount *account = accountsArray[0];
                
                [SocialVideoHelper uploadTwitterVideo:data account:account withCompletion:^{
                    NSLog(@"%@", @"Video Uploaded! Hahaha!");
                }];
            }
        }
    }];
}

3. Show Timeline

需要先配置一下Fabric,然后粘下面的代码。
reference: http://docs.fabric.io/ios/twitter/show-timelines.html
// Objective-C

// FABUserTimelineViewController.h
#import <UIKit/UIKit.h>
#import <TwitterKit/TwitterKit.h>

@interface FABUserTimelineViewController : TWTRTimelineViewController
@end

// FABUserTimelineViewController.m
#import "FABUserTimelineViewController.h"
#import <TwitterKit/TwitterKit.h>
@implementation FABUserTimelineViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  [[Twitter sharedInstance] logInGuestWithCompletion:^(TWTRGuestSession *guestSession, NSError *error) {
    if (guestSession) {
      TWTRAPIClient *APIClient = [[Twitter sharedInstance] APIClient];
      TWTRUserTimelineDataSource *userTimelineDataSource = [[TWTRUserTimelineDataSource alloc] initWithScreenName:@"wanquribao" APIClient:APIClient];
      self.dataSource = userTimelineDataSource;
    } else {
      NSLog(@"error: %@", [error localizedDescription]);
    }
  }];
}

@end

4.问题

尝试使用REST API上传图片,当地址在本地目录下时,没有问题,可以先get到这个图片对应的UIImage,转成NSData上传,但是,如果是相机拍摄的,作为参数传到同样的上传照片的函数中就无法,转成NSData就会得到nil,无法正常上传。
(GitHub使用的问题终于解决了,原来是每次commit之前都没有用'git add .',竟然拖了这么久才发现,这脑残程度也是给自己跪了……ref: http://blog.csdn.net/jackystudio/article/details/12249419

广告:亲,觉得demo做的好的话,请star一下哦!觉得做的不好,请在评论里喷它!

没有评论:

发表评论