English
English
简体中文
Contact Us
Register
Log In
layoutIndex

Sharing UI BizBundle

Last Updated on : 2022-09-16 09:07:38download

Sharing UI BizBundle provides the capabilities to share resources to third parties such as SMS, email, WeChat, or Tencent QQ.

Integrate with the UI BizBundle

Add the components of the Sharing UI BizBundle to the Podfile and run the command 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

	# Implements sharing to WeChat. (optional)
	pod 'TYSocialWeChat', '0.2.5'

	# Implements sharing to Tencent QQ. (optional)
	pod 'TYSocialQQ', '0.3.2'

	# Add the Sharing UI BizBundle. (required)
	pod 'TuyaSmartShareBizBundle'
end

Service protocols

The Sharing UI BizBundle relies on the implementation of the protocol TYSocialProtocol to provide services. You can view the TYSocialProtocol.h file in the TYModuleServices component.

@class TYSocialShareModel;

typedef NS_ENUM(NSUInteger, TYSocialType) {
    TYSocialWechat          = 0,
    TYSocialWechatMoment    = 1,   // Shared to Moments of WeChat.
    TYSocialQQ              = 2,
    TYSocialQQSpace         = 3,   // Shared to Tencent Qzone.
    TYSocialEmail           = 7,
    TYSocialSMS             = 8,
    TYSocialMore            = 11,
};

typedef NS_ENUM(NSUInteger, TYSocialShareContentType) {
    TYSocialShareContentText,       // Sharing is unused.
    TYSocialShareContenttImage,     // Shares a single image.
    TYSocialShareContentH5,         // Shares a web page.
    TYSocialShareContentImageUrl,   // Shares the URL of a single image.
};

typedef void (^TYSuccessHandler)(void);
typedef void (^TYFailureHandler)(void);

@protocol TYSocialProtocol <NSObject>

@optional

// Registers a type of sharing.
- (void)registerWithType:(TYSocialType)type appKey:(NSString *)appKey appSecret:(NSString *)appSecret;
- (void)registerWithType:(TYSocialType)type appKey:(NSString *)appKey appSecret:(NSString *)appSecret universalLink:(NSString *)universalLink;

// Shares resources.
- (void)shareTo:(TYSocialType)type shareModel:(TYSocialShareModel *)shareModel success:(TYSuccessHandler)success failure:(TYFailureHandler)failure;

// Indicates whether initialization and installation are finished.
- (BOOL)avaliableForType:(TYSocialType)type;

// The redirection feature.
- (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

Usage instruction

Things to note

Before the call of any API method, make sure that the user has logged in to the app.

Register the service

To enable sharing to WeChat or Tencent QQ, you must register the sharing service on the official website of WeChat or Tencent QQ and get appKey and appSecret.

ObjC:

#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#>"];

Swift:

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#>")

Rewrite continueUserActivity and openURL of AppDelegate

To enable sharing to WeChat or Tencent QQ, the methods continueUserActivity and openURL of AppDelegate must be written.

ObjC:

#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
}

Share to WeChat

ObjC:

#import <TuyaSmartBizCore/TuyaSmartBizCore.h>
#import <TYModuleServices/TYSocialProtocol.h>

id<TYSocialProtocol> impl = [[TuyaSmartBizCore sharedInstance] serviceOfProtocol:@protocol(TYSocialProtocol)];

/// Shares text.
if ([impl avaliableForType:TYSocialWechat]) {
    TYSocialShareModel *shareModel = [[TYSocialShareModel alloc] initWithShareType:TYSocialWechat];
    shareModel.content = @"<#content#>";
    shareModel.mediaType = TYSocialShareContentText;
    [impl shareTo:TYSocialWechat shareModel:shareModel success:^{

    } failure:^{

    }];
}

/// Shares images.
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:^{

    }];
}

/// Shares URLs.
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

/// Shares text.
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: {

    })
}

/// Shares images.
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: {

    })
}

/// Shares URLs.
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: {

    })
}

Share to Tencent QQ

ObjC:

/// Shares text.
if ([impl avaliableForType:TYSocialQQ]) {
    TYSocialShareModel *shareModel = [[TYSocialShareModel alloc] initWithShareType:TYSocialQQ];
    shareModel.content = @"<#content#>";
    shareModel.mediaType = TYSocialShareContentText;
    [impl shareTo:TYSocialQQ shareModel:shareModel success:^{

    } failure:^{

    }];
}

/// Shares images.
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:^{

    }];
}

/// Shares URLs.
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

/// Shares text.
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: {

    })
}

/// Shares images.
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: {

    })
}

/// Shares URLs.
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: {

    })
}

Share to SMS

ObjC:

/// Shares text, images, and URLs.
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:^{

    }];
}

/// Shares image URLs.
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

/// Shares text, images, and URLs.
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: {

    })
}

/// Shares image URLs.
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: {

    })
}

Share to email

ObjC:

/// Shares text, images, and URLs.
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

/// Shares text, images, and URLs.
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: {

    })
}

Additional sharing options

ObjC:

/// Shares text.
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:^{

    }];
}

/// Shares images.
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:^{

    }];
}

/// Shares URLs.
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

/// Shares text.
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: {

    })
}

/// Shares images.
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: {

    })
}

/// Shares URLs.
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: {

    })
}

Integrate with sharing to WeChat

Step 1: Set Universal Links

  1. Set Universal Links as described in Apple’s documentation.

    The settings of Universal Links for WeChat must meet the following requirements:

    • Universal Links must support URLs starting with https.
    • The paths of Universal Links cannot include query parameters.
    • When WeChat relies on Universal Links to pull up a third-party app, the value of Universal Links is suffixed with a path and parameters. Therefore the wildcard characters (/*) must be added to paths for the app.

    Example:

    {
    	"appID": "8P7343TG54.com.tencent.xin.SDKSample",
    	"paths": ["/sdksample/*"]
    }
    
  2. Enable Associated Domains and set the domains for Universal Links.

    Sharing UI BizBundle

  3. Check whether the settings of Universal Links for the app are successful. For more information, see SDK Access Success Verification Guidelines.

Step 2: Register the app ID and Universal Links

Register your account on the WeChat developer registration page and set your mobile app. Then, you can get the AppID required for app development. In line with WeChat’s regulations, after app registration, you must submit app information for review. Your app can be launched only after it is approved by WeChat.

Step 2: Set up development environment

  1. Select your project in Xcode.

  2. Choose TARGETS > info > URL type, and set URL Scheme to your registered app ID.

    Sharing UI BizBundle

  3. In Xcode, choose TARGETS > info, and add weixin and weixinULAPI to LSApplicationQueriesSchemes.

    Sharing UI BizBundle

    For more information, see WeChat documentation.

Integrate with sharing to Tencent QQ

Step 1: Set URL Scheme

  1. Select your project in Xcode.

  2. Go to TARGETS.

  3. Choose info > URL type, and set URL Scheme.

    The following example shows the URL Scheme field value consists of tencent and appid.

    Sharing UI BizBundle

Step 2: Configure LSApplicationQueriesSchemes

The following figure shows the sample settings.

Sharing UI BizBundle

For more information, see Tencent QQ documentation.