分享 UI 业务包

更新时间:2024-03-04 08:39:57

分享 UI 业务包支持三方分享能力,用户可以将信息通过微信、QQ、短信、邮箱等渠道分享给其他好友。

接入组件

在工程的 Podfile 文件中添加分享业务包组件,并执行 pod update 命令:

source 'https://github.com/tuya/tuya-pod-specs.git'
source 'https://cdn.cocoapods.org/'
platform :ios, '11.0'

target 'your_target_name' do

    # 微信分享需引入(可选)
    pod 'ThingSocialWeChat', '~> 5.0.0'

    # QQ 分享需引入(可选)
    pod 'ThingSocialQQ', '~> 5.0.0'

    # 添加分享业务包(必选)
    pod 'ThingSmartShareBizBundle', '~> 5.0.0'
end

服务协议

分享业务包实现 ThingSocialProtocol 协议以提供服务。您可以在 ThingModuleServices 组件中查看 ThingSocialProtocol.h 的协议文件内容:

@class ThingSocialShareModel;

typedef NS_ENUM(NSUInteger, ThingSocialType) {
    ThingSocialWechat          = 0,
    ThingSocialWechatMoment    = 1,   //微信朋友圈
    ThingSocialQQ              = 2,
    ThingSocialQQSpace         = 3,   //qq 空间
    ThingSocialEmail           = 7,
    ThingSocialSMS             = 8,
    ThingSocialMore            = 11,
};

typedef NS_ENUM(NSUInteger, ThingSocialShareContentType) {
    ThingSocialShareContentText,       //未使用
    ThingSocialShareContenttImage,     //单图分享
    ThingSocialShareContentH5,         //web 分享
    ThingSocialShareContentImageUrl,   //单图 URL 分享
};

typedef void (^ThingSuccessHandler)(void);
typedef void (^ThingFailureHandler)(void);

@protocol ThingSocialProtocol <NSObject>

@optional

// 注册
- (void)registerWithType:(ThingSocialType)type appKey:(NSString *)appKey appSecret:(NSString *)appSecret;
- (void)registerWithType:(ThingSocialType)type appKey:(NSString *)appKey appSecret:(NSString *)appSecret universalLink:(NSString *)universalLink;

// 分享
- (void)shareTo:(ThingSocialType)type shareModel:(ThingSocialShareModel *)shareModel success:(ThingSuccessHandler)success failure:(ThingFailureHandler)failure;

// 是否初始化&&已安装
- (BOOL)avaliableForType:(ThingSocialType)type;

// 重定向
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options;

// Universal Links
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray<id<UIUserActivityRestoring>> * __nullable restorableObjects))restorationHandler;

@end

使用指南

注意事项

调用任何接口之前,务必确认用户已登录。

注册服务

如果使用微信分享、QQ 分享,您需要到微信和 QQ 官方平台进行注册,申请 appKeyappSecret

Objective-C 示例

#import <ThingSmartBizCore/ThingSmartBizCore.h>
#import <ThingModuleServices/ThingSocialProtocol.h>

id<ThingSocialProtocol> impl = [[ThingSmartBizCore sharedInstance] serviceOfProtocol:@protocol(ThingSocialProtocol)];
[impl registerWithType:ThingSocialWechat appKey:@"<#appKey#>" appSecret:@"<#appSecret#>" universalLink:@"<#universalLink#>"];
[impl registerWithType:ThingSocialQQ appKey:@"<#appKey#>" appSecret:@"<#appSecret#>" universalLink:@"<#universalLink#>"];

Swift 示例

let impl = ThingSmartBizCore.sharedInstance().service(of: ThingSocialProtocol.self) as? ThingSocialProtocol
impl?.register?(with: ThingSocialType.wechat, appKey: "<#appKey#>", appSecret: "<#appSecret#>", universalLink:"<#universalLink#>")
impl?.register?(with: ThingSocialType.QQ, appKey: "<#appKey#>", appSecret: "<#appSecret#>", universalLink:"<#universalLink#>")

重写 AppDelegate 的 continueUserActivity 和 openURL 方法

如果使用微信分享、QQ 分享,需要重写 AppDelegatecontinueUserActivityopenURL 方法。

Objective-C 示例

#import <ThingSmartBizCore/ThingSmartBizCore.h>
#import <ThingModuleServices/ThingSocialProtocol.h>

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
    id<ThingSocialProtocol> impl = [[ThingSmartBizCore sharedInstance] serviceOfProtocol:@protocol(ThingSocialProtocol)];
    [impl application:application continueUserActivity:userActivity restorationHandler:restorationHandler];
    return YES;
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    if ([[url scheme] hasPrefix:@"wx"]) {
        id<ThingSocialProtocol> impl = [[ThingSmartBizCore sharedInstance] serviceOfProtocol:@protocol(ThingSocialProtocol)];
        return [impl application:app openURL:url options:options];
    }
    return YES;
}

Swift 示例

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    let impl = ThingSmartBizCore.sharedInstance().service(of: ThingSocialProtocol.self) as? ThingSocialProtocol
    let _ = impl?.application?(application, continue: userActivity, restorationHandler: restorationHandler)
    return true
}

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    let newOptions: [String: Any] = options.reduce(into: [:]) { (result, tmp) in
        result[tmp.key.rawValue] = tmp.value
    }

    if url.scheme?.hasPrefix("wx") == true {
        let impl = ThingSmartBizCore.sharedInstance().service(of: ThingSocialProtocol.self) as? ThingSocialProtocol
        return (impl?.application?(app, open: url, options: newOptions))!
    }
    return true
}

微信分享

Objective-C 示例

#import <ThingSmartBizCore/ThingSmartBizCore.h>
#import <ThingModuleServices/ThingSocialProtocol.h>

id<ThingSocialProtocol> impl = [[ThingSmartBizCore sharedInstance] serviceOfProtocol:@protocol(ThingSocialProtocol)];

/// 分享文本
if ([impl avaliableForType:ThingSocialWechat]) {
    ThingSocialShareModel *shareModel = [[ThingSocialShareModel alloc] initWithShareType:ThingSocialWechat];
    shareModel.content = @"<#content#>";
    shareModel.mediaType = ThingSocialShareContentText;
    [impl shareTo:ThingSocialWechat shareModel:shareModel success:^{

    } failure:^{

    }];
}

/// 分享图片
if ([impl avaliableForType:ThingSocialWechat]) {
    ThingSocialShareModel *shareModel = [[ThingSocialShareModel alloc] initWithShareType:ThingSocialWechat];
    shareModel.image = [UIImage imageNamed:@"<#imageName#>"];
    shareModel.mediaType = ThingSocialShareContenttImage;
    [impl shareTo:ThingSocialWechat shareModel:shareModel success:^{

    } failure:^{

    }];
}

/// 分享链接
if ([impl avaliableForType:ThingSocialWechat]) {
    ThingSocialShareModel *shareModel = [[ThingSocialShareModel alloc] initWithShareType:ThingSocialWechat];
    shareModel.title = @"<#title#>";
    shareModel.content = @"<#content#>";
    shareModel.url = @"<#url#>";
    shareModel.mediaType = ThingSocialShareContentH5;
    [impl shareTo:ThingSocialWechat shareModel:shareModel success:^{

    } failure:^{

    }];
}

Swift 示例

let impl = ThingSmartBizCore.sharedInstance().service(of: ThingSocialProtocol.self) as? ThingSocialProtocol

/// 分享文本
if impl?.avaliable?(for: ThingSocialType.wechat) == true {
    let shareModel = ThingSocialShareModel(share: ThingSocialType.wechat)!
    shareModel.content = "<#content#>"
    shareModel.mediaType = ThingSocialShareContentType.contentText
    impl?.share?(to:ThingSocialType.wechat, shareModel: shareModel, success: {

    }, failure: {

    })
}

/// 分享图片
if impl?.avaliable?(for: ThingSocialType.wechat) == true {
    let shareModel = ThingSocialShareModel(share: ThingSocialType.wechat)!
    shareModel.image = UIImage(named: "<#imageName#>")
    shareModel.mediaType = ThingSocialShareContentType.contenttImage
    impl?.share?(to:ThingSocialType.wechat, shareModel: shareModel, success: {

    }, failure: {

    })
}

/// 分享链接
if impl?.avaliable?(for: ThingSocialType.wechat) == true {
    let shareModel = ThingSocialShareModel(share: ThingSocialType.wechat)!
    shareModel.title = "<#title#>"
    shareModel.content = "<#content#>"
    shareModel.url = "<#url#>"
    shareModel.mediaType = ThingSocialShareContentType.contentH5
    impl?.share?(to:ThingSocialType.wechat, shareModel: shareModel, success: {

    }, failure: {

    })
}

QQ 分享

Objective-C 示例

/// 分享文本
if ([impl avaliableForType:ThingSocialQQ]) {
    ThingSocialShareModel *shareModel = [[ThingSocialShareModel alloc] initWithShareType:ThingSocialQQ];
    shareModel.content = @"<#content#>";
    shareModel.mediaType = ThingSocialShareContentText;
    [impl shareTo:ThingSocialQQ shareModel:shareModel success:^{

    } failure:^{

    }];
}

/// 分享图片
if ([impl avaliableForType:ThingSocialQQ]) {
    ThingSocialShareModel *shareModel = [[ThingSocialShareModel alloc] initWithShareType:ThingSocialQQ];
    shareModel.image = [UIImage imageNamed:@"<#imageName#>"];
    shareModel.mediaType = ThingSocialShareContenttImage;
    [impl shareTo:ThingSocialQQ shareModel:shareModel success:^{

    } failure:^{

    }];
}

/// 分享链接
if ([impl avaliableForType:ThingSocialQQ]) {
    ThingSocialShareModel *shareModel = [[ThingSocialShareModel alloc] initWithShareType:ThingSocialQQ];
    shareModel.content = @"<#content#>";
    shareModel.title = @"<#title#>";
    shareModel.url = @"<#url#>";
    shareModel.mediaType = ThingSocialShareContentH5;
    [impl shareTo:ThingSocialQQ shareModel:shareModel success:^{

    } failure:^{

    }];
}

Swift 示例

let impl = ThingSmartBizCore.sharedInstance().service(of: ThingSocialProtocol.self) as? ThingSocialProtocol

/// 分享文本
if impl?.avaliable?(for: ThingSocialType.QQ) == true {
    let shareModel = ThingSocialShareModel(share: ThingSocialType.QQ)!
    shareModel.content = "<#content#>"
    shareModel.mediaType = ThingSocialShareContentType.contentText
    impl?.share?(to:ThingSocialType.QQ, shareModel: shareModel, success: {

    }, failure: {

    })
}

/// 分享图片
if impl?.avaliable?(for: ThingSocialType.QQ) == true {
    let shareModel = ThingSocialShareModel(share: ThingSocialType.QQ)!
    shareModel.image = UIImage(named: "<#imageName#>")
    shareModel.mediaType = ThingSocialShareContentType.contenttImage
    impl?.share?(to:ThingSocialType.QQ, shareModel: shareModel, success: {

    }, failure: {

    })
}

/// 分享链接
if impl?.avaliable?(for: ThingSocialType.QQ) == true {
    let shareModel = ThingSocialShareModel(share: ThingSocialType.QQ)!
    shareModel.title = "<#title#>"
    shareModel.content = "<#content#>"
    shareModel.url = "<#url#>"
    shareModel.mediaType = ThingSocialShareContentType.contentH5
    impl?.share?(to:ThingSocialType.QQ, shareModel: shareModel, success: {

    }, failure: {

    })
}

短信分享

Objective-C 示例

/// 分享文本 & 图片 & 链接
if ([impl avaliableForType:ThingSocialSMS]) {
    ThingSocialShareModel *shareModel = [[ThingSocialShareModel alloc] initWithShareType:ThingSocialSMS];
    shareModel.content = @"<#content#>";
    shareModel.image = [UIImage imageNamed:@"<#imageName#>"];
    shareModel.url = @"<#url#>";
    shareModel.recipients = @[@"<#phoneNumber#>"];
    [impl shareTo:ThingSocialSMS shareModel:shareModel success:^{

    } failure:^{

    }];
}

/// 分享图片链接
if ([impl avaliableForType:ThingSocialSMS]) {
    ThingSocialShareModel *shareModel = [[ThingSocialShareModel alloc] initWithShareType:ThingSocialSMS];
    shareModel.content = @"<#content#>";
    shareModel.recipients = @[@"<#phoneNumber#>"];
    shareModel.imageUrl = @"<#imageUrl#>";
    shareModel.mediaType = ThingSocialShareContentImageUrl;
    [impl shareTo:ThingSocialSMS shareModel:shareModel success:^{

    } failure:^{

    }];
}

Swift 示例

let impl = ThingSmartBizCore.sharedInstance().service(of: ThingSocialProtocol.self) as? ThingSocialProtocol

/// 分享文本 & 图片 & 链接
if impl?.avaliable?(for: ThingSocialType.SMS) == true {
    let shareModel = ThingSocialShareModel(share: ThingSocialType.SMS)!
    shareModel.content = "<#content#>"
    shareModel.image = UIImage(named: "<#imageName#>")
    shareModel.url = "<#url#>"
    shareModel.recipients = ["<#phoneNumber#>"]
    impl?.share?(to:ThingSocialType.SMS, shareModel: shareModel, success: {

    }, failure: {

    })
}

/// 分享图片链接
if impl?.avaliable?(for: ThingSocialType.SMS) == true {
    let shareModel = ThingSocialShareModel(share: ThingSocialType.SMS)!
    shareModel.content = "<#content#>"
    shareModel.recipients = ["<#phoneNumber#>"]
    shareModel.imageUrl = "<#imageUrl#>"
    shareModel.mediaType = ThingSocialShareContentType.contentImageUrl
    impl?.share?(to:ThingSocialType.SMS, shareModel: shareModel, success: {

    }, failure: {

    })
}

邮箱分享

Objective-C 示例

/// 分享文本 & 图片 & 链接
if ([impl avaliableForType:ThingSocialEmail]) {
    ThingSocialShareModel *shareModel = [[ThingSocialShareModel alloc] initWithShareType:ThingSocialEmail];
    shareModel.title = @"<#title#>";
    shareModel.content = @"<#content#>";
    shareModel.url = @"<#url#>";
    shareModel.image = [UIImage imageNamed:@"<#imageName#>"];
    shareModel.recipients = @[@"<#email#>"];
    shareModel.ccRecipients = @[@"<#email#>"];
    shareModel.bccRecipients = @[@"<#email#>"];
    [impl shareTo:ThingSocialEmail shareModel:shareModel success:^{

    } failure:^{

    }];
}

Swift 示例

let impl = ThingSmartBizCore.sharedInstance().service(of: ThingSocialProtocol.self) as? ThingSocialProtocol

/// 分享文本 & 图片 & 链接
if impl?.avaliable?(for: ThingSocialType.email) == true {
    let shareModel = ThingSocialShareModel(share: ThingSocialType.email)!
    shareModel.title = "<#title#>"
    shareModel.content = "<#content#>"
    shareModel.url = "<#url#>"
    shareModel.image = UIImage(named: "<#imageName#>")
    shareModel.recipients = ["<#email#>"]
    shareModel.ccRecipients = ["<#email#>"]
    shareModel.bccRecipients = ["<#email#>"]
    impl?.share?(to:ThingSocialType.email, shareModel: shareModel, success: {

    }, failure: {

    })
}

更多分享

Objective-C 示例

/// 分享文本
if ([impl avaliableForType:ThingSocialMore]) {
    ThingSocialShareModel *shareModel = [[ThingSocialShareModel alloc] initWithShareType:ThingSocialMore];
    shareModel.title = @"<#title#>";
    shareModel.content = @"<#content#>";
    shareModel.mediaType = ThingSocialShareContentText;
    [impl shareTo:ThingSocialMore shareModel:shareModel success:^{

    } failure:^{

    }];
}

/// 分享图片
if ([impl avaliableForType:ThingSocialMore]) {
    ThingSocialShareModel *shareModel = [[ThingSocialShareModel alloc] initWithShareType:ThingSocialMore];
    shareModel.image = [UIImage imageNamed:@"<#imageName#>"];
    shareModel.mediaType = ThingSocialShareContenttImage;
    [impl shareTo:ThingSocialMore shareModel:shareModel success:^{

    } failure:^{

    }];
}

/// 分享链接
if ([impl avaliableForType:ThingSocialMore]) {
    ThingSocialShareModel *shareModel = [[ThingSocialShareModel alloc] initWithShareType:ThingSocialMore];
    shareModel.url = @"<#url#>";
    shareModel.mediaType = ThingSocialShareContentH5;
    [impl shareTo:ThingSocialMore shareModel:shareModel success:^{

    } failure:^{

    }];
}

Swift 示例

let impl = ThingSmartBizCore.sharedInstance().service(of: ThingSocialProtocol.self) as? ThingSocialProtocol

/// 分享文本
if impl?.avaliable?(for: ThingSocialType.more) == true {
    let shareModel = ThingSocialShareModel(share: ThingSocialType.more)!
    shareModel.title = "<#title#>"
    shareModel.content = "<#content#>"
    shareModel.mediaType = ThingSocialShareContentType.contentText
    impl?.share?(to:ThingSocialType.more, shareModel: shareModel, success: {

    }, failure: {

    })
}

/// 分享图片
if impl?.avaliable?(for: ThingSocialType.more) == true {
    let shareModel = ThingSocialShareModel(share: ThingSocialType.more)!
    shareModel.image = UIImage(named: "<#imageName#>")
    shareModel.mediaType = ThingSocialShareContentType.contenttImage
    impl?.share?(to:ThingSocialType.more, shareModel: shareModel, success: {

    }, failure: {

    })
}

/// 分享链接
if impl?.avaliable?(for: ThingSocialType.more) == true {
    let shareModel = ThingSocialShareModel(share: ThingSocialType.more)!
    shareModel.url = "<#url#>"
    shareModel.mediaType = ThingSocialShareContentType.contentH5
    impl?.share?(to:ThingSocialType.more, shareModel: shareModel, success: {

    }, failure: {

    })
}

微信分享接入步骤

第一步:配置应用的 Universal Links

  1. 根据 苹果官方文档 配置您应用的 Universal Links

    微信对 Universal Links 配置要求:

    • Universal Links 必须支持 HTTPS。
    • Universal Links 配置的 paths 不能携带 query 参数。
    • 微信使用 Universal Links 唤起第三方 App 时,会在 Universal Links 末尾拼接路径和参数,因此 App 配置的 paths 必须加上通配符 /*。

    示例:

    {
        "appID": "8P7343TG54.com.tencent.xin.SDKSample",
        "paths": ["/sdksample/*"]
    }
    
  2. 打开 Associated Domains 开关,将 Universal Links 域名加到配置上。

    分享 UI 业务包
  3. 检查确认 App 的 Universal Links 配置成功,通过 SDK 接入成功验证指引 操作。

第二步:注册应用 ID 和 Universal Links

请到 微信开发者应用登记页面 进行登记,登记并选择移动应用进行设置后,您将获得 AppID,用于开发。但根据微信平台的规定,应用登记完成后还需要提交审核,只有审核通过的应用才能正式发布使用。

第三步:搭建开发环境

  1. 在 Xcode 中,选择您的工程设置项。

  2. 选中 TARGETS 一栏,在 info 标签栏的 URL type,添加 URL Scheme 为您所注册的应用程序 ID。

    分享 UI 业务包
  3. 在 Xcode 中,选择您的工程设置项,选中 TARGETS 一栏,在 info 标签栏的 LSApplicationQueriesSchemes,添加 weixinweixinULAPI

    分享 UI 业务包

    更多信息,请参考 微信官方文档

QQ 分享接入步骤

第一步:配置 URL Scheme

  1. 在 Xcode 中,选择您的工程设置项。

  2. 选中 TARGETS 一栏。

  3. info 标签栏的 URL type,添加一条新的 URL scheme

    新的 URL Scheme 为 tencent + appid,如下所示:

    分享 UI 业务包

第二步:配置 LSApplicationQueriesSchemes

如下图,对照 SDK Demo 的配置,进行填写。

分享 UI 业务包

更多信息,请参考 QQ 官方文档