使用分享類的SDK其實有很多,例如友盟、ShareSDK等等,參照他們的文檔集成起來並不是很難,可能出的一些問題也就是配置文件的問題,這里我個人使用了ShareSDK分享,具體操作可出現的問題如下:
1.首先去ShareSDK官方網站注冊開發者賬號,獲取AppKey用來注冊SDK:http://www.mob.com/#/
2.下載SDK:http://www.mob.com/#/downloadDetail/ShareSDK/ios
(1) ShareSDK.framework:核心靜態庫。(必要)
(2) Support 文件夾。其中包含三個文件夾 :
(a) Required (需要的類庫) :
----- MOBFoundation.framework:基礎功能框架。(必要)
----- ShareSDK.bundle:ShareSDK資源文件。(必要)
----- ShareSDKConnector.framework:用於ShareSDK框架與外部框架連接的代理框架插件。(使用第三方SDK時必要。)
(b) Optional (可選類庫):
----- ShareSDKExtension.framework:對ShareSDK功能的擴展框架插件。
(主要提供第三方平台登錄、 一鍵分享、截屏分享、搖一搖分享等相關功能。需要使用以上功能時必要。)
----- ShareSDKInterfaceAdapter.framework:ShareSDK v2.x對簡潔版的接口兼容包。
----- ShareSDKUI.bundle:分享菜單欄和分享編輯頁面資源包。(如果自定義這些UI可直接移除)
----- ShareSDKUI.framework:分享菜單欄和分享編輯頁面。(如果自定義這些UI可直接移除)
(c) PlatformSDK 第三方平台SDK。(不需要的平台的SDK可直接移除)
3.去新浪微博開放平台、騰訊開放平台、微信開放平台等去注冊賬號,獲取AppKey、AppID、APPScret等
平台 |
開放平台地址
|
APPkey 申請流程
|
新浪微博 | ||
騰訊微博 | ||
QQ空間[2] | ||
微信[3] | ||
google+ | https://console.developers.google.com/ | |
豆瓣 | ||
有道雲筆記 | ||
印象筆記 | ||
Flickr | ||
Tumblr | ||
Dropbox | ||
VKontakte | ||
易信好友[4] | ||
明道 | ||
人人網 | ||
開心網 | ||
搜狐隨身看 | https://open.sohu.com/ |
4.在plist文件配置屬性
(1)Htpp協議,如果適配iSO9,需要暫時回退到http協議 info plist加
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
(2)設置白名單 ,在plist文件的URL Types中設置URL Schemes白名單:具體設置可以參照官方文檔,很詳細

(3)涉及到客戶端跳轉,一定要設置scheme到LSApplicationQueriesSchemes,只有設置了這個,在真機才能實現客戶端的跳轉,進入QQ、微信、新浪等
進入QQ客戶端:
進入微信客戶端:
新浪微博客戶端:
好了,一定准備就緒,直接上代碼:
//1.創建一個AppDelegate分類
#import "AppDelegate.h" //shareSDK #import <ShareSDK/ShareSDK.h> #import <ShareSDKConnector/ShareSDKConnector.h> //騰訊開放平台(對應QQ和QQ空間)SDK頭文件 #import <TencentOpenAPI/TencentOAuth.h> #import <TencentOpenAPI/QQApiInterface.h> //微信SDK頭文件 #import "WXApi.h" //新浪微博SDK頭文件 //新浪微博SDK需要在項目Build Settings中的Other Linker Flags添加"-ObjC" #import "WeiboSDK.h" @interface AppDelegate (KJShareSDk) -(void)shareApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions; @end
// AppDelegate+KJShareSDk.m // KenJiao // // Created by mac on 16/4/26. // Copyright © 2016年 mac. All rights reserved. // #import "AppDelegate+KJShareSDk.h" @implementation AppDelegate (KJShareSDk) -(void)shareApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ /** * 設置ShareSDK的appKey,如果尚未在ShareSDK官網注冊過App,請移步到http://mob.com/login 登錄后台進行應用注冊, * 在將生成的AppKey傳入到此方法中。 * 方法中的第二個第三個參數為需要連接社交平台SDK時觸發, * 在此事件中寫入連接代碼。第四個參數則為配置本地社交平台時觸發,根據返回的平台類型來配置平台信息。 * 如果您使用的時服務端托管平台信息時,第二、四項參數可以傳入nil,第三項參數則根據服務端托管平台來決定要連接的社交SDK。 */ [ShareSDK registerApp:@"121f16c6xxxxx" activePlatforms:@[ @(SSDKPlatformTypeSinaWeibo), @(SSDKPlatformTypeWechat), @(SSDKPlatformTypeQQ)] onImport:^(SSDKPlatformType platformType) { switch (platformType) { case SSDKPlatformTypeWechat: [ShareSDKConnector connectWeChat:[WXApi class]]; break; case SSDKPlatformTypeQQ: [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]]; break; case SSDKPlatformTypeSinaWeibo: [ShareSDKConnector connectWeibo:[WeiboSDK class]]; break; default: break; } } onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) { switch (platformType) { case SSDKPlatformTypeSinaWeibo: //設置新浪微博應用信息,其中authType設置為使用SSO+Web形式授權 [appInfo SSDKSetupSinaWeiboByAppKey:@"117501xxx" appSecret:@"ac85119d8a70e9c954bbf62364xxxx" redirectUri:@"http://www.baidu.com" authType:SSDKAuthTypeBoth]; break; case SSDKPlatformTypeWechat: [appInfo SSDKSetupWeChatByAppId:@"wx365f7b8b89dxxxx" appSecret:@"652feede37bd1ff5484abb1ed51xxxxf"]; break; case SSDKPlatformTypeQQ: [appInfo SSDKSetupQQByAppId:@"1105359xxx" appKey:@"ewgzpB2GOf3SIxxx" authType:SSDKAuthTypeBoth]; break; default: break; } }]; } @end
//2.在AppDelegate類中直接調用即可
#import "AppDelegate.h" #import "AppDelegate+KJShareSDk.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //注冊shareSDK [self shareApplication:application didFinishLaunchingWithOptions:launchOptions]; return YES; } @end
//3、最后一步就是分享內容了
//點擊微信 -(void)weiixnBtnClicked:(UIButton *)sender{ //1.隱藏分享視圖 [self cancelBtnClicked:nil]; //2、創建分享參數 NSMutableDictionary *shareParams = [NSMutableDictionary dictionary]; [shareParams SSDKSetupShareParamsByText:[NSString stringWithFormat:@"班級號:%@",self.currentClass.class_id] images:nil url:nil title:@"將該班級號分享到微信" type:SSDKContentTypeAuto]; //3、分享(微信平台) [self startSharePlatform:SSDKPlatformTypeWechat parameters:shareParams]; } //點擊QQ -(void)QQBtnClicked:(UIButton *)sender{ //1.隱藏分享視圖 [self cancelBtnClicked:nil]; //2、創建分享參數 NSMutableDictionary *shareParams = [NSMutableDictionary dictionary]; [shareParams SSDKSetupShareParamsByText:[NSString stringWithFormat:@"班級號:%@",self.currentClass.class_id] images:nil url:nil title:@"將該班級號分享到QQ" type:SSDKContentTypeAuto]; //3、分享(QQ平台) [self startSharePlatform:SSDKPlatformTypeQQ parameters:shareParams]; } //點擊微博 -(void)weiBoBtnClicked:(UIButton *)sender{ //1.隱藏分享視圖 [self cancelBtnClicked:nil]; //2、創建分享參數 NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
[shareParams SSDKEnableUseClientShare];//登錄客戶端
[shareParams SSDKSetupShareParamsByText:[NSString stringWithFormat:@"班級號:%@",self.currentClass.class_id]
images:nil url:nil title:@"將該班級號分享到微博" type:SSDKContentTypeAuto]; //3、分享(新浪微博平台) [self startSharePlatform:SSDKPlatformTypeSinaWeibo parameters:shareParams]; } //開始分享 -(void)startSharePlatform:(SSDKPlatformType)platform parameters:(NSMutableDictionary *)parameters{ [ShareSDK share:platform parameters:parameters onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) { switch (state) { case SSDKResponseStateSuccess: { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil]; [alertView show]; break; } case SSDKResponseStateFail: { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失敗" message:[NSString stringWithFormat:@"%@",error] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; break; } default: break; } }]; } //點擊取消 -(void)cancelBtnClicked:(UIButton *)sender{ [self.inviteStuView removeFromSuperview]; [self.inviteStuView.backgroundView removeFromSuperview]; }
手機測試截圖:例如立即邀請后,使用新浪微博分享班級號,此時打開新浪微博客戶端。QQ和微信也是一樣的
總結:
總結:像適配iOS9此類問題 使用第三方的直接看別人官網就行了,要不訪問提供方的如微信,QQ,支付寶API官方看開發文檔
如果是用sharesdk 友盟此類的 去看他們的開發文檔都會第一時間給開發者提供解決方案的
sharesdk分享 iOS9適配文檔地址
http://wiki.mob.com/ios9-%E5%AF%B9sharesdk%E7%9A%84%E5%BD%B1%E5%93%8D%EF%BC%88%E9%80%82%E9%85%8Dios-9%E5%BF%85%E8%AF%BB%EF%BC%89/
友盟分享 iOS9適配地址
http://dev.umeng.com/social/ios/ios9
本人原創,轉載須注明出處,謝謝!