Sharing UI BizBundle

Last Updated on : 2024-07-16 06:28:46download

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 'ThingSocialWeChat', '~> 5.0.0'

    # Implements sharing to Tencent QQ. (optional)
    pod 'ThingSocialQQ', '~> 5.0.0'

    # Adds the Sharing UI BizBundle. (required)
    pod 'ThingSmartShareBizBundle', '~> 5.0.0'
end

Service protocol

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

@class ThingSocialShareModel;

typedef NS_ENUM(NSUInteger, ThingSocialType) {
    ThingSocialWechat          = 0,
    ThingSocialWechatMoment    = 1,   // Shared to Moments of WeChat.
    ThingSocialQQ              = 2,
    ThingSocialQQSpace         = 3,   // Shared to Tencent Qzone.
    ThingSocialEmail           = 7,
    ThingSocialSMS             = 8,
    ThingSocialMore            = 11,
};

typedef NS_ENUM(NSUInteger, ThingSocialShareContentType) {
    ThingSocialShareContentText,       // Sharing is unused.
    ThingSocialShareContenttImage,     // Shares a single image.
    ThingSocialShareContentH5,         // Shares a web page.
    ThingSocialShareContentImageUrl,   // Shares the URL of a single image.
};

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

@protocol ThingSocialProtocol <NSObject>

@optional

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

// Shares resources.
- (void)shareTo:(ThingSocialType)type shareModel:(ThingSocialShareModel *)shareModel success:(ThingSuccessHandler)success failure:(ThingFailureHandler)failure;

// Indicates whether initialization and installation are finished.
- (BOOL)avaliableForType:(ThingSocialType)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.

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

Rewrite continueUserActivity and openURL of AppDelegate

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

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
}

Share to WeChat

Objective-C:

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

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

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

    } failure:^{

    }];
}

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

    }];
}

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

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

    })
}

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

    })
}

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

    })
}

Share to Tencent QQ

Objective-C:

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

    } failure:^{

    }];
}

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

    }];
}

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

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

    })
}

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

    })
}

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

    })
}

Share to SMS

Objective-C:

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

    }];
}

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

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

    })
}

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

    })
}

Share to email

Objective-C:

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

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

    })
}

Additional sharing options

Objective-C:

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

    }];
}

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

    }];
}

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

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

    })
}

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

    })
}

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

    })
}

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 3: 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.