Twitter

iOS 5 據說更新超過 1500個API,
其中一個大項就是 Twitter 整合,
透過這項API,
開發者就可以輕鬆整合 Twitter帳號。


Framework

要將這個功能放到我們開發的App,
首先要將 Twitter.framework 與 Accounts.framework 放到開發專案中。

Twitter.framework顧名思義就是與Twitter相關的架構,
而Account則是iOS 用來紀錄 使用者在 IOS中整合帳號的架構,
不過目前只支援 Twitter。

接著,
我們就可以在專案中放入以下程式

TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];

 

[twitter setInitialText:@"It's my tweet from iOS 5!"];

[twitter addImage:[UIImage imageNamed:@"image.png"]];

 

[self presentViewController:twitter animated:YES completion:nil];

 

twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) {

 

if(res == TWTweetComposeViewControllerResultDone)

{

 

UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Succes!" message:@"Posted succesfully" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

 

[alertView show];

 

}else if(res == TWTweetComposeViewControllerResultCancelled)

{

 

UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Tweet was not posted" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

 

[alertView show];

 

}

 

[self dismissModalViewControllerAnimated:YES];

};

這段程式就會將iOS 5中撰寫 Tweet的介面呼叫起來,
並在裡面放入預設的文字與要一起上傳的照片。

TweetCompose

如果使用者還沒設定 Twitter 帳號,
或者還沒授權這個 app 存取這個帳號,
就會彈出如上的警告畫面,
讓使用者可以進行設定。

如果使用下面的程式碼,

ACAccountStore *account = [[ACAccountStore alloc] init];

ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

 

[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)

{

if (granted == YES)

{

NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];

 

if ([arrayOfAccounts count] > 0)

{

ACAccount *acct = [arrayOfAccounts objectAtIndex:0];

 

TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:

@"http://search.twitter.com/search.json?q=iOS%205&rpp=5&with_twitter_user_id=true&result_type=recent"]

parameters:nil requestMethod:TWRequestMethodGET];

[request setAccount:acct];

 

[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)

{

if ([urlResponse statusCode] == 200)

{

NSError *error;

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];

NSLog(@"Twitter response: %@", dict);

}

else

NSLog(@"Twitter error, HTTP response: %i", [urlResponse statusCode]);

}];

}

}

}];

我們就可以透過 Accounts 的架構存取使用者設定好的 twitter資訊,
並可以利用 TWRequest 與 Twitter的API 進行溝通。

Twitter framework的文件可以參考 http://developer.apple.com/library/ios/#documentation/Twitter/Reference/TwitterFrameworkReference/_index.html
Accounts framework的文件可以參考 http://developer.apple.com/library/ios/#documentation/Accounts/Reference/AccountsFrameworkRef/_index.html
Twitter API的文件可以參考 https://dev.twitter.com/docs/api

有興趣整合 Twitter 的 iOS 開發者朋友,
可以試試這個 iOS 5的新API。

Technorati Tags: , , , , , , ,

arrow
arrow

    賽拉維‧柯南 發表在 痞客邦 留言(0) 人氣()