iOS 分享功能開發

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

以下圖c++

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

 

一,註冊帳號api

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

建立應用,最終得到 App Key,App Secret微信

 

二,SDK集成微信開發

下載SDKapp

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

 

添加依賴庫文件
必須添加的依賴庫:
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,系統會自動設置根視圖控制器。

 

[objc]  view plain  copy
  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

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

 

[objc]  view plain  copy
  1. #import <ShareSDK/ShareSDK.h>  

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

 

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

3.初始化社交平臺

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

 

[objc]  view plain  copy
  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、回調地址等須要您自已去相應平臺新建應用獲取。

 

[objc]  view plain  copy
  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中添加處理打開連接的方法


 

[objc]  view plain  copy
  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 中導入頭文件

 

[objc]  view plain  copy
  1. #import <ShareSDK/ShareSDK.h>  

在須要分享的地方 實現以下方法

 

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

 

[objc]  view plain  copy
  1. -(void)shareAct:(id)sender  
  2. {  
  3. //      
  4.     NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"ShareSDK" ofType:@"jpg"];  
  5.       
  6.     //一、構造分享內容  
  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.     //二、彈出分享菜單  
  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. }  


 

到此基本完成

測試一下 點擊微博分享 

相關文章
相關標籤/搜索