iOS APP內(nèi)容分享至qq、微信

前言:這段時(shí)間項(xiàng)目上要增加分享功能,支持分享到qq,微信,好吧,那就開(kāi)干。通過(guò)Google后,發(fā)現(xiàn)用mob可以快速集成,挺方便的。

一、使用mob集成分享功能,我是采用cocopods方式集成的,目前我這邊需求是支持分享到qq,微信。那就集成嘍,順便我也打算采用mob的UI彈出框選擇。
cocopods相信大家都會(huì)使用了,就不介紹了,百度一大堆。直接復(fù)制以下需要引用的框架到工程里(Podfile),執(zhí)行pod install時(shí),安裝可能比較慢,我這邊安裝了20多分鐘才完成。

# UI模塊(非必須,需要用到ShareSDK提供的分享菜單欄和分享編輯頁(yè)面需要以下1行)
  pod 'mob_sharesdk/ShareSDKUI'
  # 平臺(tái)SDK模塊(對(duì)照一下平臺(tái),需要的加上。如果只需要QQ、微信、新浪微博,只需要以下3行)
  pod 'mob_sharesdk/ShareSDKPlatforms/QQ'
  pod 'mob_sharesdk/ShareSDKPlatforms/WeChat'   #(微信sdk不帶支付的命令)
# pod 'mob_sharesdk/ShareSDKPlatforms/WeChatFull' //(微信sdk帶支付的命令,和上面不帶支付的不能共存,只能選擇一個(gè))

#集成微信
  pod 'WechatOpenSDK'

假如你沒(méi)有搜索不到這個(gè)mob_sharesdk時(shí):
不慌,那就清空下pod索引,就好了

1、請(qǐng)先進(jìn)行:pod setup

2、再清空一下搜索索引,讓pod重建索引:

rm ~/Library/Caches/CocoaPods/search_index.json

OK,導(dǎo)入需要集成的框架后,我們開(kāi)始為mob配置項(xiàng)目.

第一步:設(shè)置ShareSDK的Appkey并初始化對(duì)應(yīng)的第三方社交平臺(tái)

在項(xiàng)目工程的Info.plist 中如圖增加 MOBAppkey 和 MOBAppSecret 兩個(gè)字段

在 MOBAppkey中 設(shè)置ShareSDK的appKey,如果尚未在ShareSDK官網(wǎng)注冊(cè)過(guò)App,請(qǐng)移步到mob登錄后臺(tái)進(jìn)行應(yīng)用注冊(cè) 也可以點(diǎn)擊鏈接看里面的操作步驟。

mob1.png

友情提示:如果您同時(shí)多個(gè)MOBSDK產(chǎn)品 在info.plist中的設(shè)置一次就可以了。

打開(kāi)AppDelegate.m(代表你的工程名字)導(dǎo)入頭文件
#import <ShareSDK/ShareSDK.h>
在- (BOOL)application: didFinishLaunchingWithOptions:方法中調(diào)用registerApp方法來(lái)初始化SDK并且初始化第三方平臺(tái)(各社交平臺(tái)申請(qǐng)AppKey的網(wǎng)址及申請(qǐng)流程匯總

需要在AppDelegate中導(dǎo)入的頭文件

#import <ShareSDKConnector/ShareSDKConnector.h>
#import "WXApi.h"
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>

在didFinishLaunchingWithOptions方法中注冊(cè):



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    
    [ShareSDK registerActivePlatforms:@[@(SSDKPlatformTypeQQ),
                                        @(SSDKPlatformTypeWechat)]
                             onImport:^(SSDKPlatformType platformType) {
        
                                 switch (platformType) {
                                         
                                     case SSDKPlatformTypeQQ:
                                         [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
                                         break;
                                     case SSDKPlatformTypeWechat:
                                         [ShareSDKConnector connectWeChat:[WXApi class]];
                                         break;
                                     default:
                                         break;
                                 }
                                 
    } onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) {
        
        switch (platformType)
        {
            case SSDKPlatformTypeQQ:
                [appInfo SSDKSetupQQByAppId:@"1107990966"
                                     appKey:@"6iwfpt3hIJlf70Y2"
                                   authType:SSDKAuthTypeBoth];
                break;
                
            case SSDKPlatformTypeWechat:
//                                [appInfo SSDKSetupWeChatByAppId:@”wx4868b35061f87885″
//                                                      appSecret:@”64020361b8ec4c99936c0e3999a9f249″];
                break;
            
            default:
                 break;
                 }
    }];
}

第二步:添加實(shí)現(xiàn)代碼 打開(kāi)需要集成分享功能的視圖源碼,把如下代碼復(fù)制并粘貼到你要分享的位置,例如到響應(yīng)分享按鈕的方法中。并且修改相應(yīng)的參數(shù)即可。

1.需要在分享的試圖中導(dǎo)入的頭文件


//mob分享 索引需要用到的
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>

2.調(diào)用構(gòu)造分享參數(shù)接口和分享的接口

 //1、創(chuàng)建分享參數(shù)
    NSArray* imageArray = @[[UIImage imageNamed:@"shareImg.png"]];
    (注意:圖片必須要在Xcode左邊目錄里面,名稱必須要傳正確,如果要分享網(wǎng)絡(luò)圖片,可以這樣傳iamge參數(shù) images:@[@"http://mob.com/Assets/images/logo.png?v=20150320"])
    if (imageArray) {

        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
        [shareParams SSDKSetupShareParamsByText:@"分享內(nèi)容"
                                         images:imageArray
                                            url:[NSURL URLWithString:@"http://mob.com"]
                                          title:@"分享標(biāo)題"
                                           type:SSDKContentTypeAuto];
 //2、分享(可以彈出我們的分享菜單和編輯界面)
        [ShareSDK showShareActionSheet:nil //要顯示菜單的視圖, iPad版中此參數(shù)作為彈出菜單的參照視圖,只有傳這個(gè)才可以彈出我們的分享菜單,可以傳分享的按鈕對(duì)象或者自己創(chuàng)建小的view 對(duì)象,iPhone可以傳nil不會(huì)影響
                                 items:nil
                           shareParams:shareParams
                   onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {

                       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;
                       }];

//大家請(qǐng)注意:4.1.2版本開(kāi)始因?yàn)閁I重構(gòu)了下,所以這個(gè)彈出分享菜單的接口有點(diǎn)改變,如果集成的是4.1.2以及以后版本,如下調(diào)用:
 [ShareSDK showShareActionSheet:nil customItems:nil shareParams:shareParams sheetConfiguration:nil onStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
        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;
                            }
}];

編譯并運(yùn)行后,點(diǎn)擊分享按鈕就成功發(fā)送到社交平臺(tái)的話說(shuō)明你已經(jīng)基本集成成功了。

可選:支持微信所需的相關(guān)配置及代碼 在微信開(kāi)放平臺(tái)(http://open.weixin.qq.com/ )注冊(cè)應(yīng)用并取得應(yīng)用的AppID,然后打開(kāi)下圖位置,在URL Types中添加微信AppID

mob2.png

打開(kāi)AppDelegate.m(代表你的工程名字)文件,導(dǎo)入微信SDK的頭文件并初始化:

#import <ShareSDK/ShareSDK.h>
[ShareSDK registPlatforms:^(SSDKRegister *platformsRegister) {
            //QQ
            [platformsRegister setupQQWithAppId:@"100371282" appkey:@"aed9b0303e3ed1e27bae87c33761161d"];
}];

可選:支持QQ所需的相關(guān)配置及代碼 登錄騰訊開(kāi)放平臺(tái)(http://open.qq.com/ )注冊(cè)成為開(kāi)發(fā)者并登記應(yīng)用取得AppId,然后打開(kāi)下圖位置,在URL Types中添加QQ的AppID,其格式為:”QQ” + AppId的16進(jìn)制(如果appId轉(zhuǎn)換的16進(jìn)制數(shù)不夠8位則在前面補(bǔ)0,如轉(zhuǎn)換的是:5FB8B52,則最終填入為:QQ05FB8B52 注意:轉(zhuǎn)換后的字母要大寫(xiě)

必看注意:
1.在iOS9中,如果沒(méi)有添加上述白名單,系統(tǒng)會(huì)打印類似如下提示:.-canOpenURL: failed for URL: “sinaweibohdsso://xxx” – error: “This app is not allowed to query for scheme sinaweibohdsso”(如下圖)如沒(méi)有添加相關(guān)白名單,有可能導(dǎo)致分享失敗,例如不會(huì)跳轉(zhuǎn)微信,不會(huì)跳轉(zhuǎn)QQ等。
2.添加完上述所需的名單,系統(tǒng)依然會(huì)打印類似信息:.-canOpenURL: failed for URL: “sinaweibohdsso://xxx” – error: “null”這是系統(tǒng)打印的信息,目前是無(wú)法阻止其打印,即無(wú)法消除的
3.上述白名單,是技術(shù)人員通過(guò)不斷的測(cè)試收集整理所得,如果各位開(kāi)發(fā)者朋友發(fā)現(xiàn)上表格有所遺漏,請(qǐng)根據(jù)系統(tǒng)信息添加相關(guān)白名單即可。

mob3.png

另外貼上qq和微信的開(kāi)發(fā)者平臺(tái)注冊(cè)網(wǎng)站:
點(diǎn)我進(jìn)入QQ騰訊開(kāi)放平臺(tái)
點(diǎn)我進(jìn)入微信開(kāi)放平臺(tái)
以及各社區(qū)平臺(tái)的開(kāi)發(fā)者平臺(tái)注冊(cè)網(wǎng)站:
點(diǎn)我進(jìn)入各社交平臺(tái)申請(qǐng)AppKey的網(wǎng)址及申請(qǐng)流程匯總
目前按照我以上文檔可以分享至qq及qq空間,當(dāng)然,微信也是如此,如果在分享中遇到問(wèn)題不了解的,可以留言,我會(huì)一一解答。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容