Skip to content

Instantly share code, notes, and snippets.

@taktamur
Created June 15, 2013 15:03
Show Gist options
  • Save taktamur/5788421 to your computer and use it in GitHub Desktop.
Save taktamur/5788421 to your computer and use it in GitHub Desktop.
3周遅れぐらいでiOSのSocial.frameworkを使ってみる ref: http://qiita.com/items/9a6b51fa56915d1f1d64
-(void)viewDidAppear:(BOOL)animated
{
// エミュレータでも問題なく動く
// postのクライアント表示はiOSになる。
SLComposeViewController *twitterPostVC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[twitterPostVC setInitialText:@"iOSのSocialFrameworkから投稿テスト。\nSLComposeViewController簡単。"];
[self presentViewController:twitterPostVC animated:YES completion:nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.accountStore = [[ACAccountStore alloc]init];
}
// ほぼコピペ
// https://dev.twitter.com/docs/ios/making-api-requests-slrequest
-(void)viewWillAppear:(BOOL)animated
{
// Step 0: Check that the user has local Twitter accounts
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
// Step 1: Obtain access to the user's Twitter accounts
ACAccountType *twitterAccountType = [self.accountStore
accountTypeWithAccountTypeIdentifier:
ACAccountTypeIdentifierTwitter];
[self.accountStore
requestAccessToAccountsWithType:twitterAccountType
options:NULL
completion:^(BOOL granted, NSError *error) {
if (granted) {
// Step 2: Create a request
NSArray *twitterAccounts =
[self.accountStore accountsWithAccountType:twitterAccountType];
NSURL *url = [NSURL URLWithString:@"https://api.twitter.com"
@"/1.1/statuses/user_timeline.json"];
NSDictionary *params = @{@"screen_name" : @"paming",
@"include_rts" : @"0",
@"trim_user" : @"1",
@"count" : @"10"};
SLRequest *request =
[SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodGET
URL:url
parameters:params];
// Attach an account to the request
[request setAccount:[twitterAccounts lastObject]];
// Step 3: Execute the request
[request performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse,
NSError *error) {
if (responseData) {
if (urlResponse.statusCode >= 200 && urlResponse.statusCode < 300) {
NSError *jsonError;
// サンプルではDictionaryだけど、Arrayが戻ってきてる
// JSONがArrayだし。
NSArray *timelineData =
[NSJSONSerialization
JSONObjectWithData:responseData
options:NSJSONReadingAllowFragments error:&jsonError];
NSLog(@"class=%@",[timelineData class]);
if (timelineData) {
// NSLog(@"Timeline Response: %@\n", timelineData);
for (NSDictionary *dic in timelineData) {
NSLog(@"%@",[dic objectForKey:@"text"]);
}
}
else {
// Our JSON deserialization went awry
NSLog(@"JSON Error: %@", [jsonError localizedDescription]);
}
}
else {
// The server did not respond successfully... were we rate-limited?
NSLog(@"The response status code is %d", urlResponse.statusCode);
}
}
}];
}
else {
// Access was not granted, or an error occurred
NSLog(@"%@", [error localizedDescription]);
}
}];
}
}
-(void)viewDidAppear:(BOOL)animated
{
NSURL *url = [NSURL URLWithString:@"https://userstream.twitter.com/1.1/user.json"];
NSDictionary *params = nil;
SLRequest *request =
[SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodGET
URL:url
parameters:params];
ACAccountType *twitterAccountType = [self.accountStore
accountTypeWithAccountTypeIdentifier:
ACAccountTypeIdentifierTwitter];
NSArray *twitterAccounts =
[self.accountStore accountsWithAccountType:twitterAccountType];
ACAccount *account = [twitterAccounts lastObject];
[request setAccount:account];
self.connection = [NSURLConnection connectionWithRequest:request.preparedURLRequest delegate:self];
[self.connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.connection start];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"%@",response);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSError *jsonError;
id jsonObj =
[NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&jsonError];
NSLog(@"%@",jsonObj);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment