因為項目裡要用到這方面的功能,最近一直在研究怎麼實現。還好運氣不錯,找到一個工具sharekit,
可以很方便的把資訊分享到facebook和twitter上面。
ShareKit 是iPhone中一鍵分享文字,圖片,連結,檔案到Facebook,
twitter, delicious, tumblr, google reader等第三方網站的objc庫
(1) Download : http://getsharekit.com/d/ShareKit.0.2.1.zip
(2) Github : http://github.com/ideashower/sharekit/
詳細配置如下:
一.首先需要在SHKConfig.h中針對應用的名稱和回調連結作一個全域設定:
#define SHKMyAppName @"iWhat" //app的名稱#define SHKMyAppURL @"http://itunes.apple.com/cn/app/id388833522?mt=8" //itunes上的連結
二.第三方平台的OAUTH配置(即獲得相應的API Key和API Secret)
1.Facebook
<1>.建立一個application(http://www.facebook.com/developers/)
<2>.將上面產生的key對應到SHKConfig.h:
#define SHKFacebookUseSessionProxy NO#define SHKFacebookKey @"e5aeb908d24f4c7ace1a623374280869"#define SHKFacebookSecret @"b09478d7873bd4779f387c3e58f9df93"#define SHKFacebookSessionProxyURL @"" // left it blank here
2.Twitter
<1>.建立一個基於browser的application
(http://dev.twitter.com/apps/new)
<2>.將上面產生的key對應到SHKConfig.h:
#define SHKTwitterConsumerKey @"oUYTCJTaB1BmQnIBDKPMTg"#define SHKTwitterSecret @"U4CDOvOTvUnEfHuEUfrvphQ96UFZYv87R6q7ZG1jsM"#define SHKTwitterCallbackUrl @"http://itunes.apple.com/cn/app/id388833522?mt=8"#define SHKTwitterUseXAuth 0 // To use xAuth, set to 1#define SHKTwitterUsername @"" // left it blank here , if use xAuth
注意:如果使用oauth,SHKTwitterCallbackUrl這一項目一定要填,url是不是真實存在都可以,但是要和twitter上建立的應用裡的callbackurl相對應。
三.相關調用代碼
- (void)loadToolbar{ UIBarButtonItem *spaceItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil] autorelease]; UIBarButtonItem *shareItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareLink)] autorelease]; CGRect rect = CGRectMake(0,392, 320, 44); UIToolbar *toobar = [[[UIToolbar alloc] initWithFrame:rect] autorelease]; toobar.barStyle = UIBarStyleBlackOpaque; toobar.items = [[NSArray alloc] initWithObjects:spaceItem,shareItem,spaceItem,nil]; [self.view addSubview:toobar]; }- (void)shareLink{ NSURL *sharedURL = [NSURL URLWithString:@"http://www.google.com"]; SHKItem *item = [SHKItem URL:sharedURL title:@"Google Test"]; SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item]; [actionSheet showFromToolbar:self.navigationController.toolbar];}
四.注意:
1.只有Facebook, Twitter採用的是OAuth Connect
2.如果採取的是TabBarViewController結構的話,需要為每一個Share View對應的RooViewController做如下設定:(官方文檔中沒有說明這一點)
(1)先引入:#import "SHK.h"
(2)在RootViewController初始化的地方加上:[SHK setRootViewController:self];
3.不同APP不能共用一個OAuth Connect Secret Key , 否則在twitter和facebook分享的時候出現用戶端分享成功,但是卻沒有真正提交到第三方網站的假分享.(一個mobile app必須嚴格對應一個facebook, twitter app)