分享

weixin_34019929發表於2017-10-23

步驟:

1、 在ShareSDK註冊賬號,獲取Appkey和App Secret,在專案工程的Info.plist 中增加MOBAppkey 和 MOBAppSecret 兩個欄位
2、 在各個開放平臺註冊賬號,獲取Appkey和App Secret,初始化應用
3、 新增URL Types,輸入URL Schemes出新增APPID
注意:
QQ的URL Schemes的設定,需要在終端操作,命令如下:
echo 'ibase=10;obase=16;801312852'|bc,其中801312852為QQ的AppID,替換為自己的APPID即可,得到8位的16進位制數,則URL Schemes填入QQ05RDWE32
4、 新增白名單

程式碼

//
//  ShareTools.h
//  ShareDemo
//
//  Created by Mac Air on 2017/10/23.
//  Copyright © 2017年 Mac Air. All rights reserved.

#import <Foundation/Foundation.h>

@interface SHShareTools : NSObject

+ (instancetype)shareInstance;
- (void)connectConfig;
- (void)inviteFriendsWithTitle:(NSString *)title detailTitle:(NSString *)detailTitle shareUrl:(NSString *)shareUrl;

@end

//
//  ShareTools.m
//  ShareDemo
//
//  Created by Mac Air on 2017/10/23.
//  Copyright © 2017年 Mac Air. All rights reserved.
//

#import "SHShareTools.h"

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

#import <ShareSDKUI/ShareSDKUI.h>


//微信:
#define WechatAppID @"微信開放平臺申請"
#define WechatAppSecret @"微信開放平臺申請"

//QQ:
#define  QQAPPID @"QQ開放平臺申請"
#define  QQAPPAppSecret @"QQ開放平臺申請"

//微博:
#define  WeiboAppKey @"微博開放平臺申請"
#define  WeiboAppSecret @"微博開放平臺申請"



@implementation SHShareTools

+ (instancetype)shareInstance
{
    static SHShareTools *_shareInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _shareInstance = [[self alloc] init];
    });
    return _shareInstance;
}

- (void)connectConfig
{
    [ShareSDK registerActivePlatforms:@[
                                        @(SSDKPlatformSubTypeWechatSession), //微信好友
                                        @(SSDKPlatformSubTypeWechatTimeline),//微信朋友圈
                                        @(SSDKPlatformSubTypeQQFriend),//QQ好友
                                        @(SSDKPlatformTypeSinaWeibo),
                                        ]
                             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 SSDKPlatformTypeWechat:
                                  
                              {
                                  [appInfo SSDKSetupWeChatByAppId:WechatAppID
                                                        appSecret:WechatAppSecret];
                              }
                                  break;
                                  
                              case SSDKPlatformTypeQQ:
                              {
                                  [appInfo SSDKSetupQQByAppId:QQAPPID
                                                       appKey:QQAPPAppSecret
                                                     authType:SSDKAuthTypeBoth];
                                  
                              }
                                  break;
                                  
                              case SSDKPlatformTypeSinaWeibo:
                              {
                                  //設定新浪微博應用資訊,其中authType設定為使用SSO+Web形式授權
                                  [appInfo SSDKSetupSinaWeiboByAppKey:WeiboAppKey
                                                            appSecret:WeiboAppSecret
                                                          redirectUri:@"https://www.baidu.com"
                                                             authType:SSDKAuthTypeBoth];
                              }
                                  break;
                                  
                                  
                                  
                              default:
                                  break;
                          }
                          
                      }];

}



- (void)inviteFriendsWithTitle:(NSString *)title detailTitle:(NSString *)detailTitle shareUrl:(NSString *)shareUrl
{
    
    //1、建立分享引數
    NSArray* imageArray = @[[UIImage imageNamed:@"邀請好友"]];
    // (注意:圖片必須要在Xcode左邊目錄裡面,名稱必須要傳正確,如果要分享網路圖片,可以這樣傳iamge引數 images:@[@"http://mob.com/Assets/images/logo.png?v=20150320"])
    
    if (imageArray) {
        
        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
        [shareParams SSDKSetupShareParamsByText:detailTitle
                                         images:imageArray
                                            url:[NSURL URLWithString:shareUrl]
                                          title:title
                                           type:SSDKContentTypeAuto];
        //有的平臺要客戶端分享需要加此方法,例如微博
        [shareParams SSDKEnableUseClientShare];
        
        NSLog(@"分享的引數112,,%@",shareParams);
        
        //2、分享(可以彈出我們的分享選單和編輯介面)
        [ShareSDK showShareActionSheet:nil //要顯示選單的檢視, iPad版中此引數作為彈出選單的參照檢視,只有傳這個才可以彈出我們的分享選單,可以傳分享的按鈕物件或者自己建立小的view 物件,iPhone可以傳nil不會影響
                                 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;
                       }
                   }];
    }

}

@end

呼叫


//  AppDelegate.m
//  ShareDemo
//
//  Created by Mac Air on 2017/10/23.
//  Copyright © 2017年 Mac Air. All rights reserved.
//

#import "AppDelegate.h"
#import "SHShareTools.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //配置分享的引數:
    [[SHShareTools shareInstance]connectConfig];
    
    return YES;
}

- (IBAction)shareClick:(id)sender {
    
    [[SHShareTools shareInstance] inviteFriendsWithTitle:@"邀請好友" detailTitle:@"邀請好友的副標題" shareUrl:@"http://www.jianshu.com/u/ce78ee1f9a89"];
    
}

效果圖

1460329-38ab06e7be7f8999.png
螢幕快照 2017-10-23 下午5.52.44.png

相關文章