Last active
December 30, 2015 05:19
-
-
Save synboo/7781847 to your computer and use it in GitHub Desktop.
Sign in with Twitter using iOS SDK
https://dev.twitter.com/docs/ios/making-api-requests-slrequest
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
- (void)testTwitter | |
{ | |
// Step 0: Check that the user has local Twitter accounts | |
BOOL isTwitterAvailable = [SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]; | |
NSLog(@"isTwitterAvailable:%d", isTwitterAvailable); | |
if (isTwitterAvailable) { | |
// Step 1: Obtain access to the user's Twitter accounts | |
ACAccountStore *accountStore = [[ACAccountStore alloc] init]; | |
ACAccountType *twitterAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; | |
[accountStore requestAccessToAccountsWithType:twitterAccountType options:nil completion:^(BOOL granted, NSError *error) { | |
// Allow access to iOS Twitter account | |
if (granted) { | |
NSLog(@"granted!!"); | |
// Step 2: Create a request | |
NSArray *twitterAccounts = [accountStore accountsWithAccountType:twitterAccountType]; | |
NSLog(@"twitterAccounts:%@", twitterAccounts); | |
NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/mentions_timeline.json"]; | |
NSDictionary *params = @{@"screen_name" : @"synboo", | |
@"include_rts" : @"0", | |
@"trim_user" : @"1", | |
@"count" : @"50"}; | |
params = nil; | |
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; | |
NSDictionary *timelineData = [NSJSONSerialization JSONObjectWithData:responseData | |
options:NSJSONReadingAllowFragments | |
error:&jsonError]; | |
if (timelineData) { | |
NSLog(@"Timeline Response: %@\n", timelineData); | |
for (NSDictionary *tweetStatus in timelineData) { | |
NSLog(@"tweetStatus.text:%@", tweetStatus[@"text"]); | |
} | |
} | |
else { | |
// Our JSON deserialization went awry | |
[self alertMessage:[NSString stringWithFormat:@"JSON Error: %@", [jsonError localizedDescription]]]; | |
} | |
} else { | |
// The server did not respond ... were we rate-limited? | |
[self alertMessage:[NSString stringWithFormat:@"The response status code is %d", urlResponse.statusCode]]; | |
} | |
} else { | |
[self alertMessage:@"Network error"]; | |
} | |
}]; | |
// Not allow | |
} else { | |
NSLog(@"not granted!!"); | |
// iOS Twitterアカウントがこのアプリを使うのを許可していない | |
[self alertMessage:@"iOSのTwitterアカウント設定でこのアプリケーションを許可してください"]; | |
} | |
}]; | |
} else { | |
// iOS Twitterアカウント未設定 | |
[self alertMessage:@"iOSのTwitterアカウント連携をしてください"]; | |
} | |
} | |
- (void)alertMessage:(NSString *)message | |
{ | |
[[[UIAlertView alloc] initWithTitle:nil | |
message:message | |
delegate:self cancelButtonTitle:@"OK" | |
otherButtonTitles:nil, nil] show]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment