分享

iOS 開發過程中可能會遇到需要進行第三方分享的需求,比如向QQ,微信,微博等分享

如下圖

我們今天要講到的方式是使用了一個第三方工具: http://www.sharesdk.cn


一,註冊賬號

去官網註冊:http://www.sharesdk.cn

創建應用,最終獲得 App Key,App Secret



二,SDK集成

下載SDK

把下載的SDK拖到項目工程,在彈出的對話框中選中"Copy items if needed"和"Create groups",並點擊“Finish“按鈕


添加依賴庫文件
必須添加的依賴庫:
SystemConfiguration.framework
QuartzCore.framework 
CoreTelephony.framework
libicucore.dylib
libz.1.2.5.dylib
Security.framework


根據社交平臺需要添加的依賴庫

Social.framework    騰訊微博
Accounts.framework  騰訊微博
MessageUI.framework 短信和郵件
libstdc++.dylib   QQ好友和QQ空間SSO授權需要(新註冊的騰訊開放平臺帳號只支持SSO授權權限)
libsqlite3.dylib   QQ好友和QQ空間SSO授權需要(新註冊的騰訊開放平臺帳號只支持SSO授權權限)
CoreMotion.framework   Google+ 
CoreLocation.framework Google+
MediaPlayer.framework  Google+
CoreText.framework   Google+ 
AssetsLibrary.framework    Google+
AddressBook.framework  Google+

到此爲止SDK集成就算完成 下來我們開始代碼部分


三,初始ShareSDK和社交平臺


在AppDelegate.m中添加ShareSDK頭文件

1.設置根視圖控制器

必須設置rootViewController。如果使用storyBoard,系統會自動設置根視圖控制器。

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  2.     // Override point for customization after application launch.  
  3.       
  4.     _window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];  
  5.     _window.backgroundColor =[UIColor colorWithRed:249/255.0 green:249/255.0 blue:249/255.0 alpha:1];  
  6.     HelpViewController *mainController=[[HelpViewController alloc]init];  
  7.     _window.rootViewController=mainController;  
  8.     [_window makeKeyAndVisible];  
  9.   
  10.     return YES;  
  11. }  

以上示例代碼中講 HelpViewController 的一個對象設置爲了 此應用的rootViewcontroller;


2.初始化ShareSDK

導入頭文件,加上初始化方法。

  1. #import <ShareSDK/ShareSDK.h>  

  1. //1.初始化ShareSDK應用,字符串"iosv1101"是應該換成你申請的ShareSDK應用中的Appkey  
  2. [ShareSDK registerApp:@"iosv1101"];  
  3.   
  4. //2. 初始化社交平臺  
  5. //2.1 代碼初始化社交平臺的方法  
  6. [self initializePlat];  

[self initializePlat ]  方法見下一步(此處以微信,qq,騰訊微博 ,新浪微博爲例);

3.初始化社交平臺

可以用代碼初始化或在ShareSDK開發者後臺的社會化平臺設置中填入社交平臺的信息。

  1. #import "WXApi.h"  
  2. #import "WeiboSDK.h"  
  3. #import "WeiboApi.h"  
  4. #import <TencentOpenAPI/QQApi.h>  
  5. #import <TencentOpenAPI/QQApiInterface.h>  
  6. #import <TencentOpenAPI/TencentOAuth.h>  

社交平臺的AppKey、AppSecret、回調地址等需要您自已去相應平臺新建應用獲取。
  1. - (void)initializePlat  
  2. {  
  3.     /** 
  4.      連接微信應用以使用相關功能,此應用需要引用WeChatConnection.framework和微信官方SDK 
  5.      http://open.weixin.qq.com上註冊應用,並將相關信息填寫以下字段 
  6.      **/  
  7.     [ShareSDK connectWeChatWithAppId:@""  
  8.                            appSecret:@""  
  9.                            wechatCls:[WXApi class]];  
  10.     /** 
  11.      連接QQ應用以使用相關功能,此應用需要引用QQConnection.framework和QQApi.framework庫 
  12.      http://mobile.qq.com/api/上註冊應用,並將相關信息填寫到以下字段 
  13.      **/  
  14.     //舊版中申請的AppId(如:QQxxxxxx類型),可以通過下面方法進行初始化  
  15.     //    [ShareSDK connectQQWithAppId:@"QQ075BCD15" qqApiCls:[QQApi class]];  
  16.       
  17.     [ShareSDK connectQQWithQZoneAppKey:@""  
  18.                      qqApiInterfaceCls:[QQApiInterface class]  
  19.                        tencentOAuthCls:[TencentOAuth class]];  
  20.       
  21.       
  22.       
  23.       
  24.     //  
  25.     /** 
  26.      連接騰訊微博開放平臺應用以使用相關功能,此應用需要引用TencentWeiboConnection.framework 
  27.      http://dev.t.qq.com上註冊騰訊微博開放平臺應用,並將相關信息填寫到以下字段 
  28.       
  29.      如果需要實現SSO,需要導入libWeiboSDK.a,並引入WBApi.h,將WBApi類型傳入接口 
  30.      **/  
  31.     [ShareSDK connectTencentWeiboWithAppKey:@""  
  32.                                   appSecret:@""  
  33.                                 redirectUri:@"http://www.sharesdk.cn"  
  34.                                    wbApiCls:[WeiboApi class]];  
  35.     //  
  36.       
  37.       
  38.       
  39.       
  40.     /** 
  41.      連接QQ空間應用以使用相關功能,此應用需要引用QZoneConnection.framework 
  42.      http://connect.qq.com/intro/login/上申請加入QQ登錄,並將相關信息填寫到以下字段 
  43.       
  44.      如果需要實現SSO,需要導入TencentOpenAPI.framework,並引入QQApiInterface.h和TencentOAuth.h,將QQApiInterface和TencentOAuth的類型傳入接口 
  45.      **/  
  46.     //    [ShareSDK connectQZoneWithAppKey:@""  
  47.     //                           appSecret:@""  
  48.     //                   qqApiInterfaceCls:[QQApiInterface class]  
  49.     //                     tencentOAuthCls:[TencentOAuth class]];  
  50.       
  51.       
  52.     /** 
  53.      連接新浪微博開放平臺應用以使用相關功能,此應用需要引用SinaWeiboConnection.framework 
  54.      http://open.weibo.com上註冊新浪微博開放平臺應用,並將相關信息填寫到以下字段 
  55.      **/  
  56.     [ShareSDK connectSinaWeiboWithAppKey:@""  
  57.                                appSecret:@""  
  58.                              redirectUri:@"http://www.sharesdk.cn"];  
  59.       
  60.       
  61. }  



四,設置URL Scheme

1.需要依賴客戶端分享或者要支持SSO授權(可以理解成跳到客戶端授權)的平臺都需要配置平臺的URL Scheme(應用分享到社交平臺後通過識別URL Scheme返回應用)。具體配置URL Scheme請參考iOS配置SSO授權

(http://wiki.mob.com/%E9%85%8D%E7%BD%AEsso%E6%8E%88%E6%9D%83-2/)。

2.需要在AppDelegate.m中添加處理打開鏈接的方法


  1. - (BOOL)application:(UIApplication *)application  
  2.       handleOpenURL:(NSURL *)url  
  3. {  
  4.     return [ShareSDK handleOpenURL:url  
  5.                         wxDelegate:self];  
  6. }  
  7.   
  8. - (BOOL)application:(UIApplication *)application  
  9.             openURL:(NSURL *)url  
  10.   sourceApplication:(NSString *)sourceApplication  
  11.          annotation:(id)annotation  
  12. {  
  13.     return [ShareSDK handleOpenURL:url  
  14.                  sourceApplication:sourceApplication  
  15.                         annotation:annotation  
  16.                         wxDelegate:self];  
  17. }  


五,構造分享內容並分享

1.構造一個id<ISSContent>類型的對象,把需要分享的標題、內容、圖片、URL等 包裝在這個對象。

 在需要分享的viewController 中導入頭文件

  1. #import <ShareSDK/ShareSDK.h>  

在需要分享的地方 實現如下方法

比如在點擊一個button的時候調用方法 shareAct

  1. -(void)shareAct:(id)sender  
  2. {  
  3. //      
  4.     NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"ShareSDK" ofType:@"jpg"];  
  5.       
  6.     //1、構造分享內容  
  7.     id<ISSContent> publishContent = [ShareSDK content:@"要分享的內容"  
  8.                                        defaultContent:@"默認內容"  
  9.                                                 image:[ShareSDK imageWithPath:imagePath]  
  10.                                                 title:@"ShareSDK"  
  11.                                                   url:@"http://www.mob.com"  
  12.                                           description:@"這是一條演示信息"  
  13.                                             mediaType:SSPublishContentMediaTypeNews];  
  14.     //1+創建彈出菜單容器(iPad必要)  
  15.     id<ISSContainer> container = [ShareSDK container];  
  16.     [container setIPadContainerWithView:sender arrowDirect:UIPopoverArrowDirectionUp];  
  17.       
  18.     //2、彈出分享菜單  
  19.     [ShareSDK showShareActionSheet:container  
  20.                          shareList:nil  
  21.                            content:publishContent  
  22.                      statusBarTips:YES  
  23.                        authOptions:nil  
  24.                       shareOptions:nil  
  25.                             result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {  
  26.                                   
  27.                                 //可以根據回調提示用戶。  
  28.                                 if (state == SSResponseStateSuccess)  
  29.                                 {  
  30.                                     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享成功"  
  31.                                                                                     message:nil  
  32.                                                                                    delegate:self  
  33.                                                                           cancelButtonTitle:@"OK"  
  34.                                                                           otherButtonTitles:nil, nil nil];  
  35.                                     [alert show];  
  36.                                 }  
  37.                                 else if (state == SSResponseStateFail)  
  38.                                 {  
  39.                                     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失敗"  
  40.                                                                                     message:[NSString stringWithFormat:@"失敗描述:%@",[error errorDescription]]  
  41.                                                                                    delegate:self  
  42.                                                                           cancelButtonTitle:@"OK"  
  43.                                                                           otherButtonTitles:nil, nil nil];  
  44.                                     [alert show];  
  45.                                 }  
  46.                             }];  
  47.   
  48. }  


到此基本完成

測試一下 點擊微博分享 


發佈了22 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章