Skip to content

Instantly share code, notes, and snippets.

@kmakita13714
Created May 30, 2014 02:27
Show Gist options
  • Save kmakita13714/949655ac5e2608b78474 to your computer and use it in GitHub Desktop.
Save kmakita13714/949655ac5e2608b78474 to your computer and use it in GitHub Desktop.
SLRequestでTwitterのStreaming APIを利用する方法
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
__weak typeof(self) weakSelf = self;
[accountStore requestAccessToAccountsWithType:accountType
options:nil
completion:^(BOOL granted, NSError *error) {
if (granted) {
NSArray *accountArray = [accountStore accountsWithAccountType:accountType];
if (accountArray.count > 0) {
ACAccount *account = accountArray[0];
NSLog(@"%@", account);
NSLog(@"%@", account.identifier);
NSLog(@"%@", account.username);
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];
[request setAccount:account];
dispatch_async(dispatch_get_main_queue(), ^{
weakSelf.connection = [NSURLConnection connectionWithRequest:request.preparedURLRequest
delegate:weakSelf];
[weakSelf.connection start];
});
}
}
}];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSError *jsonError = nil;
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