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,系統會自動設置根視圖控制器。
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- // Override point for customization after application launch.
- _window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
- _window.backgroundColor =[UIColor colorWithRed:249/255.0 green:249/255.0 blue:249/255.0 alpha:1];
- HelpViewController *mainController=[[HelpViewController alloc]init];
- _window.rootViewController=mainController;
- [_window makeKeyAndVisible];
- return YES;
- }
以上示例代碼中講 HelpViewController 的一個對象設置為了 此應用的rootViewcontroller;
2.初始化ShareSDK
導入頭文件,加上初始化方法。
- #import <ShareSDK/ShareSDK.h>
- //1.初始化ShareSDK應用,字符串"iosv1101"是應該換成你申請的ShareSDK應用中的Appkey
- [ShareSDK registerApp:@"iosv1101"];
- //2. 初始化社交平台
- //2.1 代碼初始化社交平台的方法
- [self initializePlat];
[self initializePlat ] 方法見下一步(此處以微信,qq,騰訊微博 ,新浪微博為例);
3.初始化社交平台
可以用代碼初始化或在ShareSDK開發者后台的社會化平台設置中填入社交平台的信息。
- #import "WXApi.h"
- #import "WeiboSDK.h"
- #import "WeiboApi.h"
- #import <TencentOpenAPI/QQApi.h>
- #import <TencentOpenAPI/QQApiInterface.h>
- #import <TencentOpenAPI/TencentOAuth.h>
社交平台的AppKey、AppSecret、回調地址等需要您自已去相應平台新建應用獲取。
- - (void)initializePlat
- {
- /**
- 連接微信應用以使用相關功能,此應用需要引用WeChatConnection.framework和微信官方SDK
- http://open.weixin.qq.com上注冊應用,並將相關信息填寫以下字段
- **/
- [ShareSDK connectWeChatWithAppId:@""
- appSecret:@""
- wechatCls:[WXApi class]];
- /**
- 連接QQ應用以使用相關功能,此應用需要引用QQConnection.framework和QQApi.framework庫
- http://mobile.qq.com/api/上注冊應用,並將相關信息填寫到以下字段
- **/
- //舊版中申請的AppId(如:QQxxxxxx類型),可以通過下面方法進行初始化
- // [ShareSDK connectQQWithAppId:@"QQ075BCD15" qqApiCls:[QQApi class]];
- [ShareSDK connectQQWithQZoneAppKey:@""
- qqApiInterfaceCls:[QQApiInterface class]
- tencentOAuthCls:[TencentOAuth class]];
- //
- /**
- 連接騰訊微博開放平台應用以使用相關功能,此應用需要引用TencentWeiboConnection.framework
- http://dev.t.qq.com上注冊騰訊微博開放平台應用,並將相關信息填寫到以下字段
- 如果需要實現SSO,需要導入libWeiboSDK.a,並引入WBApi.h,將WBApi類型傳入接口
- **/
- [ShareSDK connectTencentWeiboWithAppKey:@""
- appSecret:@""
- redirectUri:@"http://www.sharesdk.cn"
- wbApiCls:[WeiboApi class]];
- //
- /**
- 連接QQ空間應用以使用相關功能,此應用需要引用QZoneConnection.framework
- http://connect.qq.com/intro/login/上申請加入QQ登錄,並將相關信息填寫到以下字段
- 如果需要實現SSO,需要導入TencentOpenAPI.framework,並引入QQApiInterface.h和TencentOAuth.h,將QQApiInterface和TencentOAuth的類型傳入接口
- **/
- // [ShareSDK connectQZoneWithAppKey:@""
- // appSecret:@""
- // qqApiInterfaceCls:[QQApiInterface class]
- // tencentOAuthCls:[TencentOAuth class]];
- /**
- 連接新浪微博開放平台應用以使用相關功能,此應用需要引用SinaWeiboConnection.framework
- http://open.weibo.com上注冊新浪微博開放平台應用,並將相關信息填寫到以下字段
- **/
- [ShareSDK connectSinaWeiboWithAppKey:@""
- appSecret:@""
- redirectUri:@"http://www.sharesdk.cn"];
- }
四,設置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中添加處理打開鏈接的方法
- - (BOOL)application:(UIApplication *)application
- handleOpenURL:(NSURL *)url
- {
- return [ShareSDK handleOpenURL:url
- wxDelegate:self];
- }
- - (BOOL)application:(UIApplication *)application
- openURL:(NSURL *)url
- sourceApplication:(NSString *)sourceApplication
- annotation:(id)annotation
- {
- return [ShareSDK handleOpenURL:url
- sourceApplication:sourceApplication
- annotation:annotation
- wxDelegate:self];
- }
五,構造分享內容並分享
1.構造一個id<ISSContent>類型的對象,把需要分享的標題、內容、圖片、URL等 包裝在這個對象。
在需要分享的viewController 中導入頭文件
- #import <ShareSDK/ShareSDK.h>
在需要分享的地方 實現如下方法
比如在點擊一個button的時候調用方法 shareAct
- -(void)shareAct:(id)sender
- {
- //
- NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"ShareSDK" ofType:@"jpg"];
- //1、構造分享內容
- id<ISSContent> publishContent = [ShareSDK content:@"要分享的內容"
- defaultContent:@"默認內容"
- image:[ShareSDK imageWithPath:imagePath]
- title:@"ShareSDK"
- url:@"http://www.mob.com"
- description:@"這是一條演示信息"
- mediaType:SSPublishContentMediaTypeNews];
- //1+創建彈出菜單容器(iPad必要)
- id<ISSContainer> container = [ShareSDK container];
- [container setIPadContainerWithView:sender arrowDirect:UIPopoverArrowDirectionUp];
- //2、彈出分享菜單
- [ShareSDK showShareActionSheet:container
- shareList:nil
- content:publishContent
- statusBarTips:YES
- authOptions:nil
- shareOptions:nil
- result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
- //可以根據回調提示用戶。
- if (state == SSResponseStateSuccess)
- {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享成功"
- message:nil
- delegate:self
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil, nil nil];
- [alert show];
- }
- else if (state == SSResponseStateFail)
- {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失敗"
- message:[NSString stringWithFormat:@"失敗描述:%@",[error errorDescription]]
- delegate:self
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil, nil nil];
- [alert show];
- }
- }];
- }
到此基本完成
測試一下 點擊微博分享