步骤:
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
@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
#import
#import
#import
#import
#import
#import
//微信:
#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"];
}