參考資源:
twitter開發者頁面
ios Twitter framework
The Accounts and Twitter Framework on iOS 5 by Peter Friese
The official Twitter framework example from Apple is available for download.
說明:此篇文章包含兩個按鈕,方法是使用ios 5的twitter app,你可以在手機中的ios 5設定中的Twitter按鈕中設定帳號資訊,並啓用,接下來你可以去你twitter帳戶中看到,如圖:
表示你的帳戶已經啓用該ios的應用程式,此文章是利用這個應用程式權限的使用,去做twitter的操作。
ps:twitter另有別的方法可以用你自己申請的twitter app,當作基本權限的取用設定,有興趣可以去ios Twitter framework,使用的流程類似facebook SDK。
此教學程式,一個按鈕為驗證是否使用該帳號連接你自己的app,如下,若確定使用,則按鈕無效果。
另一個為發送tweet,若無帳號,則要求帳號,若有帳號未開啟app權限,則提示使用者,如下。
Start:
step1:加入 framework :
step2: 寫入相關必要資訊
#import <Twitter/Twitter.h> #import <Accounts/Accounts.h> @property (strong, nonatomic) ACAccountStore *accountStore; @property (strong, nonatomic) NSArray *accounts; @synthesize accounts = _accounts; @synthesize accountStore = _accountStore;
此為第一個按鈕
#pragma mark - login
- (void)login_twitter
{
[self fetchData];
}
- (void)fetchData
{
if (_accounts == nil)
{
if(_accountStore == nil)
{
self.accountStore = [[ACAccountStore alloc] init];
}
ACAccountType *accountTypeTwitter = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[self.accountStore requestAccessToAccountsWithType:accountTypeTwitter
withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted)
{
dispatch_sync(dispatch_get_main_queue(), ^{
self.accounts = [self.accountStore accountsWithAccountType:accountTypeTwitter];
});
}
else {
// User denied access to his Twitter accounts
NSLog(@"用戶拒絕使用twitter帳戶連接app");
}
}
];
}
else
{
// This iOS verion doesn't support Twitter. Use 3rd party library
}
}
第二個按鈕
#pragma mark - tweet
- (void)send_tweet
{
//建立viewcontroller
TWTweetComposeViewController *tweetTOtwitter = [[TWTweetComposeViewController alloc] init];
//設定推文的內容
[tweetTOtwitter setInitialText:@"N11 Studio Twitter API 測試。"];
//推文中加入圖片資訊
[tweetTOtwitter addImage:[UIImage imageNamed:@"logo.png"]];
//推文中加入網址超連結資訊
[tweetTOtwitter addURL:[NSURL URLWithString:[NSString stringWithString:@"http://n11studio.blogspot.tw/"]]];
//顯示viewcontroller
[self presentModalViewController:tweetTOtwitter animated:YES];
//按下Send或是Cancel時的處理動作(block)
[tweetTOtwitter setCompletionHandler:^(TWTweetComposeViewControllerResult result) {
NSString *tweet_action;
switch (result) {
case TWTweetComposeViewControllerResultCancelled:
tweet_action = @"取消";
break;
case TWTweetComposeViewControllerResultDone:
tweet_action = @"完成";
break;
default:
break;
}
NSLog(@"%@", tweet_action);
//移除viewcontroller
[self dismissModalViewControllerAnimated:YES];
}];
}
As always , if you have any question , feel free to contact me.
有任何問題,請聯絡我
歡迎轉載,請註明出處,感謝。





感謝淺顯易懂的教學!
回覆刪除