CHShareKit是一个iOS平台的分享组件,支持微信、QQ第三方授权登录及分享功能。
通过CocoaPods安装
该命令下默认支持全平台
pod 'CHShareKit'
若只支持特定平台可使用如下命令安装子组件:
pod 'CHShareKit/QQBridge'
pod 'CHShareKit/WXBridge'
- 打开*AppDelegate.m(*代表你的工程名字)导入头文件
#import <CHShareKit/CHShareKit.h>
- 在
- (BOOL)application: didFinishLaunchingWithOptions:
方法中调用registerActivePlatforms:configurationHandler:
方法来初始化SDK并且初始化第三方平台
[CHSKShare registerActivePlatforms:@[@(CHSKPlatformTypeQQ), @(CHSKPlatformTypeWX)]
configurationHandler:^CHSKPlatformConfiguration *(CHSKPlatformType platformType) {
switch (platformType) {
case CHSKPlatformTypeQQ:
{
return [CHSKPlatformConfiguration configurationForQQWithAppId:@"appId" appSecret:@"appSecret"];
}
break;
case CHSKPlatformTypeWX:
{
return [CHSKPlatformConfiguration configurationForWXWithAppId:@"appId" appSecret:@"appSecret" universalLink:@"universalLink"];
}
break;
default:
return nil;
break;
}
}];
- 构造分享参数
// 创建文本分享类信息
CHSKShareMessage *shareMessage = [CHSKShareMessage shareMessageWithTitle:@"title"];
// 创建图片分享类信息
CHSKShareMessage *shareMessage = [CHSKShareMessage shareMessageWithTitle:@"title" image:@"picture/pictureURL"];
// 创建图片分享类信息
CHSKShareMessage *shareMessage = [CHSKShareMessage shareMessageWithTitle:@"title" image:@"picture/pictureURL" thumbali:@"picture/pictureURL"];
// 创建网页类分享信息
CHSKShareMessage *shareMessage = [CHSKShareMessage shareMessageWithTitle:@"title" description:@"description" image:@"picture/pictureURL" url:[NSURL URLWithString:@"URLString"]];
- 调用分享接口
[CHSKShare shareTo:CHSKPlatformTypeWX message:shareMessage shareHandler:^(CHSKResponseState state, CHSKShareMessage *message, NSDictionary *extraData, NSError *error) {
switch (state) {
case CHSKResponseStateBegin:
{
// 开始分享
}
break;
case CHSKResponseStateSuccess:
{
// 分享成功
}
break;
case CHSKResponseStateFailure:
{
// 分享失败
}
break;
case CHSKResponseStateFinish:
{
// 分享完成
}
break;
case CHSKResponseStateCancel:
{
// 取消分享
}
break;
}
}];
- 处理被打开时的URL
/// iOS 9.0以上
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
return [CHSKShare handleOpenURL:url];
}
/// iOS 9.0以下
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [CHSKShare handleOpenURL:url];
}
- 调用授权接口
[CHSKShare authorizeTo:CHSKPlatformTypeWX authorizeHandler:^(CHSKResponseState state, CHSKUser *user, NSDictionary *extraData, NSError *error) {
switch (state) {
case CHSKResponseStateBegin:
{
// 开始授权
}
break;
case CHSKResponseStateSuccess:
{
// 授权成功
}
break;
case CHSKResponseStateFailure:
{
// 授权失败
}
break;
case CHSKResponseStateFinish:
{
// 授权完成
}
break;
case CHSKResponseStateCancel:
{
// 取消授权
}
break;
}
}];
- 处理被打开时的URL
/// iOS 9.0以上
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
return [CHSKShare handleOpenURL:url];
}
/// iOS 9.0以下
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [CHSKShare handleOpenURL:url];
}
- info.plist -> Info -> URL Types
平台 | 配置URL Scheme 格式 | 举例 |
---|---|---|
微信 | 微信的appid | wx123456789 |
QQ/Qzone | tencent+腾讯QQ开放平台appID, QQ+APPID的十六进制 |
如appID:123456789 最后配置:tencent123456789 QQ75BCD15 |
- info.plist中添加key:LSApplicationQueriesSchemes,类型为Array,添加需要支持的项目,类型为字符串类型
平台 | OpenURL白名单说明 |
---|---|
微信 | wechat weixin |
mqqOpensdkSSoLogin mqqopensdkapiV2 mqqopensdkapiV3 wtloginmqq2 mqq mqqapi timapi mqqopensdkminiapp |
|
QZone | mqzoneopensdk mqzoneopensdk apimqzoneopensdkapi19 mqzoneopensdkapiV2 mqqOpensdkSSoLogin mqqopensdkapiV2 mqqopensdkapiV3 wtloginmqq2 mqqapi mqqwpa mqzone mqq mqqopensdkapiV4 mqqopensdkminiapp |
- 在iOS9中,如果没有添加上述白名单,系统会打印类似如下提示:.-canOpenURL: failed for URL: “sinaweibohdsso://xxx” – error: “This app is not allowed to query for scheme sinaweibohdsso”,如没有添加相关白名单,有可能导致分享失败,例如不会跳转微信,不会跳转QQ等
- 添加完上述所需的名单,系统依然会打印类似信息:.-canOpenURL: failed for URL: “sinaweibohdsso://xxx” – error: “null”这是系统打印的信息,目前是无法阻止其打印,即无法消除的
ColinHwang, [email protected]
MIT
ShareSDK