iphone 裡使用sharekit實現分享資訊到facebook、twitter

來源:互聯網
上載者:User

    因為項目裡要用到這方面的功能,最近一直在研究怎麼實現。還好運氣不錯,找到一個工具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) 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.