更新时间:2023-07-13 07:14:43下载pdf
分享 UI 业务包支持三方分享能力,用户可以将信息通过微信、QQ、短信、邮箱等渠道分享给其他好友。
在工程的 Podfile
文件中添加分享业务包组件,并执行 pod update
命令:
source 'https://github.com/tuya/tuya-private-specs.git'
source 'https://cdn.cocoapods.org/'
platform :ios, '11.0'
target 'your_target_name' do
# 微信分享需引入(可选)
pod 'TYSocialWeChat', '0.2.5'
# QQ 分享需引入(可选)
pod 'TYSocialQQ', '0.3.2'
# 添加分享业务包(必选)
pod 'TuyaSmartShareBizBundle'
end
分享业务包实现 TYSocialProtocol
协议以提供服务。您可以在 TYModuleServices
组件中查看 TYSocialProtocol.h
的协议文件内容:
@class TYSocialShareModel;
typedef NS_ENUM(NSUInteger, TYSocialType) {
TYSocialWechat = 0,
TYSocialWechatMoment = 1, //微信朋友圈
TYSocialQQ = 2,
TYSocialQQSpace = 3, //qq 空间
TYSocialEmail = 7,
TYSocialSMS = 8,
TYSocialMore = 11,
};
typedef NS_ENUM(NSUInteger, TYSocialShareContentType) {
TYSocialShareContentText, //未使用
TYSocialShareContenttImage, //单图分享
TYSocialShareContentH5, //web 分享
TYSocialShareContentImageUrl, //单图 URL 分享
};
typedef void (^TYSuccessHandler)(void);
typedef void (^TYFailureHandler)(void);
@protocol TYSocialProtocol <NSObject>
@optional
// 注册
- (void)registerWithType:(TYSocialType)type appKey:(NSString *)appKey appSecret:(NSString *)appSecret;
- (void)registerWithType:(TYSocialType)type appKey:(NSString *)appKey appSecret:(NSString *)appSecret universalLink:(NSString *)universalLink;
// 分享
- (void)shareTo:(TYSocialType)type shareModel:(TYSocialShareModel *)shareModel success:(TYSuccessHandler)success failure:(TYFailureHandler)failure;
// 是否初始化&&已安装
- (BOOL)avaliableForType:(TYSocialType)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 官方平台进行注册,申请 appKey
和 appSecret
。
Objective-C 示例
#import <TuyaSmartBizCore/TuyaSmartBizCore.h>
#import <TYModuleServices/TYSocialProtocol.h>
id<TYSocialProtocol> impl = [[TuyaSmartBizCore sharedInstance] serviceOfProtocol:@protocol(TYSocialProtocol)];
[impl registerWithType:TYSocialWechat appKey:@"<#appKey#>" appSecret:@"<#appSecret#>" universalLink:@"<#universalLink#>"];
[impl registerWithType:TYSocialQQ appKey:@"<#appKey#>" appSecret:@"<#appSecret#>" universalLink:@"<#universalLink#>"];
Swfit 示例
let impl = TuyaSmartBizCore.sharedInstance().service(of: TYSocialProtocol.self) as? TYSocialProtocol
impl?.register?(with: TYSocialType.wechat, appKey: "<#appKey#>", appSecret: "<#appSecret#>", universalLink:"<#universalLink#>")
impl?.register?(with: TYSocialType.QQ, appKey: "<#appKey#>", appSecret: "<#appSecret#>", universalLink:"<#universalLink#>")
如果使用微信分享、QQ 分享,需要重写 AppDelegate 的 continueUserActivity
和 openURL
方法。
Objective-C 示例
#import <TuyaSmartBizCore/TuyaSmartBizCore.h>
#import <TYModuleServices/TYSocialProtocol.h>
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
id<TYSocialProtocol> impl = [[TuyaSmartBizCore sharedInstance] serviceOfProtocol:@protocol(TYSocialProtocol)];
[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<TYSocialProtocol> impl = [[TuyaSmartBizCore sharedInstance] serviceOfProtocol:@protocol(TYSocialProtocol)];
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 = TuyaSmartBizCore.sharedInstance().service(of: TYSocialProtocol.self) as? TYSocialProtocol
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 = TuyaSmartBizCore.sharedInstance().service(of: TYSocialProtocol.self) as? TYSocialProtocol
return (impl?.application?(app, open: url, options: newOptions))!
}
return true
}
Objective-C 示例
#import <TuyaSmartBizCore/TuyaSmartBizCore.h>
#import <TYModuleServices/TYSocialProtocol.h>
id<TYSocialProtocol> impl = [[TuyaSmartBizCore sharedInstance] serviceOfProtocol:@protocol(TYSocialProtocol)];
/// 分享文本
if ([impl avaliableForType:TYSocialWechat]) {
TYSocialShareModel *shareModel = [[TYSocialShareModel alloc] initWithShareType:TYSocialWechat];
shareModel.content = @"<#content#>";
shareModel.mediaType = TYSocialShareContentText;
[impl shareTo:TYSocialWechat shareModel:shareModel success:^{
} failure:^{
}];
}
/// 分享图片
if ([impl avaliableForType:TYSocialWechat]) {
TYSocialShareModel *shareModel = [[TYSocialShareModel alloc] initWithShareType:TYSocialWechat];
shareModel.image = [UIImage imageNamed:@"<#imageName#>"];
shareModel.mediaType = TYSocialShareContenttImage;
[impl shareTo:TYSocialWechat shareModel:shareModel success:^{
} failure:^{
}];
}
/// 分享链接
if ([impl avaliableForType:TYSocialWechat]) {
TYSocialShareModel *shareModel = [[TYSocialShareModel alloc] initWithShareType:TYSocialWechat];
shareModel.title = @"<#title#>";
shareModel.content = @"<#content#>";
shareModel.url = @"<#url#>";
shareModel.mediaType = TYSocialShareContentH5;
[impl shareTo:TYSocialWechat shareModel:shareModel success:^{
} failure:^{
}];
}
Swift 示例
let impl = TuyaSmartBizCore.sharedInstance().service(of: TYSocialProtocol.self) as? TYSocialProtocol
/// 分享文本
if impl?.avaliable?(for: TYSocialType.wechat) == true {
let shareModel = TYSocialShareModel(share: TYSocialType.wechat)!
shareModel.content = "<#content#>"
shareModel.mediaType = TYSocialShareContentType.contentText
impl?.share?(to:TYSocialType.wechat, shareModel: shareModel, success: {
}, failure: {
})
}
/// 分享图片
if impl?.avaliable?(for: TYSocialType.wechat) == true {
let shareModel = TYSocialShareModel(share: TYSocialType.wechat)!
shareModel.image = UIImage(named: "<#imageName#>")
shareModel.mediaType = TYSocialShareContentType.contenttImage
impl?.share?(to:TYSocialType.wechat, shareModel: shareModel, success: {
}, failure: {
})
}
/// 分享链接
if impl?.avaliable?(for: TYSocialType.wechat) == true {
let shareModel = TYSocialShareModel(share: TYSocialType.wechat)!
shareModel.title = "<#title#>"
shareModel.content = "<#content#>"
shareModel.url = "<#url#>"
shareModel.mediaType = TYSocialShareContentType.contentH5
impl?.share?(to:TYSocialType.wechat, shareModel: shareModel, success: {
}, failure: {
})
}
Objective-C 示例
/// 分享文本
if ([impl avaliableForType:TYSocialQQ]) {
TYSocialShareModel *shareModel = [[TYSocialShareModel alloc] initWithShareType:TYSocialQQ];
shareModel.content = @"<#content#>";
shareModel.mediaType = TYSocialShareContentText;
[impl shareTo:TYSocialQQ shareModel:shareModel success:^{
} failure:^{
}];
}
/// 分享图片
if ([impl avaliableForType:TYSocialQQ]) {
TYSocialShareModel *shareModel = [[TYSocialShareModel alloc] initWithShareType:TYSocialQQ];
shareModel.image = [UIImage imageNamed:@"<#imageName#>"];
shareModel.mediaType = TYSocialShareContenttImage;
[impl shareTo:TYSocialQQ shareModel:shareModel success:^{
} failure:^{
}];
}
/// 分享链接
if ([impl avaliableForType:TYSocialQQ]) {
TYSocialShareModel *shareModel = [[TYSocialShareModel alloc] initWithShareType:TYSocialQQ];
shareModel.content = @"<#content#>";
shareModel.title = @"<#title#>";
shareModel.url = @"<#url#>";
shareModel.mediaType = TYSocialShareContentH5;
[impl shareTo:TYSocialQQ shareModel:shareModel success:^{
} failure:^{
}];
}
Swift 示例
let impl = TuyaSmartBizCore.sharedInstance().service(of: TYSocialProtocol.self) as? TYSocialProtocol
/// 分享文本
if impl?.avaliable?(for: TYSocialType.QQ) == true {
let shareModel = TYSocialShareModel(share: TYSocialType.QQ)!
shareModel.content = "<#content#>"
shareModel.mediaType = TYSocialShareContentType.contentText
impl?.share?(to:TYSocialType.QQ, shareModel: shareModel, success: {
}, failure: {
})
}
/// 分享图片
if impl?.avaliable?(for: TYSocialType.QQ) == true {
let shareModel = TYSocialShareModel(share: TYSocialType.QQ)!
shareModel.image = UIImage(named: "<#imageName#>")
shareModel.mediaType = TYSocialShareContentType.contenttImage
impl?.share?(to:TYSocialType.QQ, shareModel: shareModel, success: {
}, failure: {
})
}
/// 分享链接
if impl?.avaliable?(for: TYSocialType.QQ) == true {
let shareModel = TYSocialShareModel(share: TYSocialType.QQ)!
shareModel.title = "<#title#>"
shareModel.content = "<#content#>"
shareModel.url = "<#url#>"
shareModel.mediaType = TYSocialShareContentType.contentH5
impl?.share?(to:TYSocialType.QQ, shareModel: shareModel, success: {
}, failure: {
})
}
Objective-C 示例
/// 分享文本 & 图片 & 链接
if ([impl avaliableForType:TYSocialSMS]) {
TYSocialShareModel *shareModel = [[TYSocialShareModel alloc] initWithShareType:TYSocialSMS];
shareModel.content = @"<#content#>";
shareModel.image = [UIImage imageNamed:@"<#imageName#>"];
shareModel.url = @"<#url#>";
shareModel.recipients = @[@"<#phoneNumber#>"];
[impl shareTo:TYSocialSMS shareModel:shareModel success:^{
} failure:^{
}];
}
/// 分享图片链接
if ([impl avaliableForType:TYSocialSMS]) {
TYSocialShareModel *shareModel = [[TYSocialShareModel alloc] initWithShareType:TYSocialSMS];
shareModel.content = @"<#content#>";
shareModel.recipients = @[@"<#phoneNumber#>"];
shareModel.imageUrl = @"<#imageUrl#>";
shareModel.mediaType = TYSocialShareContentImageUrl;
[impl shareTo:TYSocialSMS shareModel:shareModel success:^{
} failure:^{
}];
}
Swift 示例
let impl = TuyaSmartBizCore.sharedInstance().service(of: TYSocialProtocol.self) as? TYSocialProtocol
/// 分享文本 & 图片 & 链接
if impl?.avaliable?(for: TYSocialType.SMS) == true {
let shareModel = TYSocialShareModel(share: TYSocialType.SMS)!
shareModel.content = "<#content#>"
shareModel.image = UIImage(named: "<#imageName#>")
shareModel.url = "<#url#>"
shareModel.recipients = ["<#phoneNumber#>"]
impl?.share?(to:TYSocialType.SMS, shareModel: shareModel, success: {
}, failure: {
})
}
/// 分享图片链接
if impl?.avaliable?(for: TYSocialType.SMS) == true {
let shareModel = TYSocialShareModel(share: TYSocialType.SMS)!
shareModel.content = "<#content#>"
shareModel.recipients = ["<#phoneNumber#>"]
shareModel.imageUrl = "<#imageUrl#>"
shareModel.mediaType = TYSocialShareContentType.contentImageUrl
impl?.share?(to:TYSocialType.SMS, shareModel: shareModel, success: {
}, failure: {
})
}
Objective-C 示例
/// 分享文本 & 图片 & 链接
if ([impl avaliableForType:TYSocialEmail]) {
TYSocialShareModel *shareModel = [[TYSocialShareModel alloc] initWithShareType:TYSocialEmail];
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:TYSocialEmail shareModel:shareModel success:^{
} failure:^{
}];
}
Swift 示例
let impl = TuyaSmartBizCore.sharedInstance().service(of: TYSocialProtocol.self) as? TYSocialProtocol
/// 分享文本 & 图片 & 链接
if impl?.avaliable?(for: TYSocialType.email) == true {
let shareModel = TYSocialShareModel(share: TYSocialType.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:TYSocialType.email, shareModel: shareModel, success: {
}, failure: {
})
}
Objective-C 示例
/// 分享文本
if ([impl avaliableForType:TYSocialMore]) {
TYSocialShareModel *shareModel = [[TYSocialShareModel alloc] initWithShareType:TYSocialMore];
shareModel.title = @"<#title#>";
shareModel.content = @"<#content#>";
shareModel.mediaType = TYSocialShareContentText;
[impl shareTo:TYSocialMore shareModel:shareModel success:^{
} failure:^{
}];
}
/// 分享图片
if ([impl avaliableForType:TYSocialMore]) {
TYSocialShareModel *shareModel = [[TYSocialShareModel alloc] initWithShareType:TYSocialMore];
shareModel.image = [UIImage imageNamed:@"<#imageName#>"];
shareModel.mediaType = TYSocialShareContenttImage;
[impl shareTo:TYSocialMore shareModel:shareModel success:^{
} failure:^{
}];
}
/// 分享链接
if ([impl avaliableForType:TYSocialMore]) {
TYSocialShareModel *shareModel = [[TYSocialShareModel alloc] initWithShareType:TYSocialMore];
shareModel.url = @"<#url#>";
shareModel.mediaType = TYSocialShareContentH5;
[impl shareTo:TYSocialMore shareModel:shareModel success:^{
} failure:^{
}];
}
Swift 示例
let impl = TuyaSmartBizCore.sharedInstance().service(of: TYSocialProtocol.self) as? TYSocialProtocol
/// 分享文本
if impl?.avaliable?(for: TYSocialType.more) == true {
let shareModel = TYSocialShareModel(share: TYSocialType.more)!
shareModel.title = "<#title#>"
shareModel.content = "<#content#>"
shareModel.mediaType = TYSocialShareContentType.contentText
impl?.share?(to:TYSocialType.more, shareModel: shareModel, success: {
}, failure: {
})
}
/// 分享图片
if impl?.avaliable?(for: TYSocialType.more) == true {
let shareModel = TYSocialShareModel(share: TYSocialType.more)!
shareModel.image = UIImage(named: "<#imageName#>")
shareModel.mediaType = TYSocialShareContentType.contenttImage
impl?.share?(to:TYSocialType.more, shareModel: shareModel, success: {
}, failure: {
})
}
/// 分享链接
if impl?.avaliable?(for: TYSocialType.more) == true {
let shareModel = TYSocialShareModel(share: TYSocialType.more)!
shareModel.url = "<#url#>"
shareModel.mediaType = TYSocialShareContentType.contentH5
impl?.share?(to:TYSocialType.more, shareModel: shareModel, success: {
}, failure: {
})
}
根据 苹果官方文档 配置您应用的 Universal Links
微信对 Universal Links 配置要求:
示例:
{
"appID": "8P7343TG54.com.tencent.xin.SDKSample",
"paths": ["/sdksample/*"]
}
打开 Associated Domains 开关,将 Universal Links 域名加到配置上。
检查确认 App 的 Universal Links 配置成功,通过 SDK 接入成功验证指引 操作。
请到 微信开发者应用登记页面 进行登记,登记并选择移动应用进行设置后,您将获得 AppID,用于开发。但根据微信平台的规定,应用登记完成后还需要提交审核,只有审核通过的应用才能正式发布使用。
在 Xcode 中,选择您的工程设置项。
选中 TARGETS 一栏,在 info 标签栏的 URL type,添加 URL Scheme 为您所注册的应用程序 ID。
在 Xcode 中,选择您的工程设置项,选中 TARGETS 一栏,在 info 标签栏的 LSApplicationQueriesSchemes,添加 weixin
和 weixinULAPI
。
更多信息,请参考 微信官方文档。
在 XCode 中,选择您的工程设置项。
选中 TARGETS 一栏。
在 info 标签栏的 URL type,添加一条新的 URL scheme。
新的 URL Scheme 为 tencent
+ appid
,如下所示:
如下图对照 SDK Demo 的配置,进行填写。
更多信息,请参考 QQ 官方文档。
该内容对您有帮助吗?
是意见反馈该内容对您有帮助吗?
是意见反馈