Last active
August 29, 2015 14:04
-
-
Save anzfactory/dc196eeb5ebe6a1b60a8 to your computer and use it in GitHub Desktop.
twitter投稿さんぷる(SLComposeViewController使わない版)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #import <Social/Social.h> -> Social.framework | |
// #import <Accounts/Accounts.h> -> Accounts.framework | |
// 投稿ボタンタップとか? | |
- (IBAction)postTwitter:(id)sender { | |
// Twitterアカウントが端末に設定されているか? | |
if ( ! [SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) { | |
return; | |
} | |
// 設定されてるアカウントを取得(マルチアカウントってな) | |
ACAccountStore *accountStore = [ACAccountStore new]; | |
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; | |
[accountStore requestAccessToAccountsWithType:accountType | |
options:nil | |
completion:^(BOOL granted, NSError *error) { | |
if ( ! granted) { | |
return; | |
} | |
self.accounts = [accountStore accountsWithAccountType:accountType]; | |
// ゲーム用、プライベート用などいろいろあるとおもうんで | |
// せめて選ばせたい | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
UIActionSheet *actionSheet = [UIActionSheet new]; | |
actionSheet.title = @"アカウントえらんでね"; | |
for (ACAccount *account in self.accounts) { | |
[actionSheet addButtonWithTitle:account.username]; | |
} | |
[actionSheet addButtonWithTitle:@"cancel"]; | |
actionSheet.cancelButtonIndex = [self.accounts count]; | |
actionSheet.delegate = self; | |
[actionSheet showInView:self.view]; | |
}); | |
}]; | |
} | |
// アカウント選択後 | |
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex | |
{ | |
if (buttonIndex == actionSheet.cancelButtonIndex) { | |
return; | |
} | |
// 投稿するGIF | |
NSURL *gifUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"mikumiku" ofType:@"gif"]]; | |
NSData *gifData = [NSData dataWithContentsOfURL: gifUrl]; | |
// 投稿処理へ | |
ACAccount *account = [self.accounts objectAtIndex:buttonIndex]; | |
// twitter api | |
NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update_with_media.json"]; | |
NSDictionary *params = @{@"status" : @"テスト投稿だよ〜"}; // めっせーじ | |
// request の作成 | |
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter | |
requestMethod:SLRequestMethodPOST | |
URL:url | |
parameters:params]; | |
[request setAccount:account]; // 投稿アカウントの指定 | |
[request addMultipartData:gifData withName:@"media[]" type:@"image/gif" filename:@"image.gif"]; //マルチパートの設定 | |
// リクエスト投げる | |
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { | |
NSLog(@"response : %@", [urlResponse debugDescription]); | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment