All Products
Search
Document Center

Use SDK

Last Updated: May 28, 2021

After you add the SDK for the Social sharing component, you need to configure the project. You can start to use this component after it has been initialized. This topic describes how to use the Social sharing SDK of the baseline version 10.1.60 and later based on the Official demo.

Configure the project

Configure the allowlist of third-party applications

In iOS 9 and later, configure the Scheme list to support application operations such as social sharing and authorization. The system will automatically check whether the Scheme of the application is set in theinfo.plist file of the project. If the Scheme of the target application is not configured, the current application will be unable to jump to the target application.SDK

Configure URL Scheme

To ensure that you can jump back to the current application from other channel applications, you need to add the urlScheme value in the Info.plist file of the current project. The value is obtained from the corresponding social sharing channel.

  • The scheme for WeChat is the assigned key.
  • The scheme for Weibo is "wb" + key.
  • The scheme for QQ is "tencent" + APPID.
  • The Identifier for Alipay is alipayShare, and the scheme for it is 'ap' + APPID.

Initialization

When using the Social sharing component, you first need to create the application information in the corresponding third-party platform, then use this information for registration. The information includes appId, appSecret, and universalLink. The methods used for registration are as follows.

When using mPaaS framework

Implement the following method in the classification of DTFrameworkInterface, and return the values of key and secret in the form of a dictionary in this method.

Note: In version 10.1.60 and later of the mPaaS SDK, the WeChat SDK has been updated to version 1.8.6.1, which required Universal Link verification. Therefore, you need to configure the corresponding Universal Link at the same time when configuring the key.

 
  1. - (void)application:(UIApplication *)application beforeDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. NSDictionary *configDic = @{
  4. @"weixin" : @{@"key":@"wxc5c09c98c276ac86", @"secret":@"d56057d8a43031bdc178991f6eb8dcd5", @"universalLink":@"https://mpaas.demo.com/"},
  5. @"weibo" : @{@"key":@"1877934830", @"secret":@"1067b501c42f484262c1803406510af0"},
  6. @"qq" : @{@"key":@"1104122330", @"secret":@"WyZkbNmE6d0rDTLf"},
  7. @"alipay": @{@"key":@"2015060900117932"}/*The bundleID for the key is "com.alipay.share.demo". If you want to use it for testing, modify it to the key you a pplied or modify the bundleID to "com.alipay.share.demo"*/}
  8. @"dingTalk": @{@"key":@"dingoaa4aipzuf2yifw17s"}};
  9. [APSKClient registerAPPConfig:configDic];
  10. }

When not using mPaaS framework

Register the key in the initialization method of the accessed application.

Note: In version 10.1.60 and later of the mPaaS SDK, the WeChat SDK has been updated to version 1.8.6.1, which requires Universal Link verification. Therefore, you need to configure the corresponding Universal Link at the same time when configuring the key.

 
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. NSDictionary *dic = @{
  4. @"weixin" : @{@"key":@"wxc5c09c98c276ac86", @"secret":@"d56057d8a43031bdc178991f6eb8dcd5",@"universalLink":@"https://mpaas.demo.com/"},
  5. @"weibo" : @{@"key":@"1877934830", @"secret":@"1067b501c42f484262c1803406510af0"},
  6. @"qq" : @{@"key":@"1104122330", @"secret":@"WyZkbNmE6d0rDTLf"},
  7. @"alipay" : @{@"key":@"2015060900117932"}/*The bundleID for the key is "com.alipay.share.demo". If you want to use it for testing, modify it to the key you applied or modify the bundleID to "com.alipay.share.demo"*/}
  8. @"dingTalk": @{@"key":@"dingoaa4aipzuf2yifw17s"}};
  9. [APSKClient registerAPPConfig:dic];
  10. }

Basic functions

Social sharing

Trigger the Social sharing panel

You can specify the channel to be displayed while triggering the Social sharing panel.

 
  1. NSArray *channelArr = @[kAPSKChannelQQ, kAPSKChannelLaiwangContacts, kAPSKChannelLaiwangTimeline, kAPSKChannelWeibo, kAPSKChannelWeixin, kAPSKChannelCopyLink,kAPSKChannelDingTalkSession];
  2. self.launchPad = [[APSKLaunchpad alloc] initWithChannels:channelArr sort:NO];
  3. self.launchPad.delegate = self;
  4. [self.launchPad showForView:[[UIApplication sharedApplication] keyWindow] animated:YES];

Complete social sharing

Execute the social sharing operation in the callback of the sharingLaunchpad method in @protocol APSKLaunchpadDelegate.

 
  1. - (void)sharingLaunchpad:(APSKLaunchpad *)launchpad didSelectChannel:(NSString *)channelName
  2. {
  3. [self shareUrl:channelName];
  4. [self.launchPad dismissAnimated:YES];
  5. }
  6. - (void)shareUrl:(NSString*)channelName
  7. {
  8. // Generate data and call the corresponding channel for social sharing.
  9. APSKMessage *message = [[APSKMessage alloc] init];
  10. message.contentType = @"url";//The types are "text", "image", and "url".
  11. message.content = [NSURL URLWithString:@"www.sina.com.cn"];
  12. message.icon = [UIImage imageNamed:@"1"];
  13. message.title = @"Title";
  14. message.desc = @"Description";
  15. APSKClient *client = [[APSKClient alloc] init];
  16. [client shareMessage:message toChannel:channelName completionBlock:^(NSError *error, NSDictionary *userInfo) {
  17. // userInfo is the extension information.
  18. if(!error)
  19. {
  20. //your logistic
  21. }
  22. NSLog(@"error = %@", error);
  23. }];
  24. }

Jump back from the channel application

  • When using the mPaaS framework, you do not need to manually handle it.
  • When not using the mPaaS framework, refer to the following code snippet.
 
  1. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
  2. {
  3. // After the social sharing is complete, return from the channel application to the source application.
  4. BOOL ret;
  5. ret = [APSKClient handleOpenURL:url];
  6. return ret;
  7. }

Login authorization

The current channel that supports login authorization is WeChat.

Obtain the auth code for the third-party channel

Obtain the auth code through third-party authorization only. The auth code is in exchange for information such as access token and openId.

 
  1. APSKClient *client = [[APSKClient alloc] init];
  2. [client getAuthCodeForChannel:kAPSKChannelWeixin completionHandler:^(NSError *error, NSString *code) {
  3. // The results returned.
  4. }];

Authorize access to user information

As a network request is required when authorizing access to user information, please confirm if the Mobile gateway service component has been added or not.
The Social sharing component provides two ways to obtain user information:

  • The client sends the request directly.
  • Custom request. Obtain user data through business service.
 
  1. - (void)getUserInfo {
  2. APSKClient *client = [[APSKClient alloc] init];
  3. [client requestAuthUserInfoForChannel:kAPSKChannelWeixin delegate:self];
  4. }
  5. /**
  6. Use the auth code to assembly a custom request.
  7. Custom request. Obtain user data through business service. If this is not implemented, then the client sends the request by default.
  8. @param code The auth code.
  9. @return The custom request object.
  10. */
  11. - (NSURLRequest *)customRequestWithAuthCode:(NSString *)code {
  12. // The custom request object.
  13. NSURLRequest *request;
  14. // ...
  15. return request;
  16. }
  17. /**
  18. After obtaining the auth code, you can request user information through UI operations.
  19. If UI operations are not required, you do not need to implement them.
  20. @param channelName The current channel that provides authorization.
  21. */
  22. - (void)getUserInfoWillRequest:(NSString*)channelName {
  23. // Set operations such as loading.
  24. }
  25. /**
  26. Obtain user's authorization results
  27. @param response The data returned, including the user information and the auth code. nil is returned if an error occurs.
  28. @param code The auth code.
  29. @param error The error message.
  30. @param channelName The current channel that provides authorization.
  31. */
  32. - (void)getUserInfoDidFinishWithResponse:(NSDictionary *)response
  33. authCode:(NSString *)code
  34. error:(NSError*)error
  35. channel:(NSString*)channelName {
  36. // The results returned. If it succeeded, the response field will contain the authorized user information.
  37. }

Open service

Through open interfaces of third-party services, you can call other open services provided by the Social sharing SDK of the third-party channel. The open services that are currently supported are One-time subscription information and Launch Mini Program services provided by WeChat.

Operating procedure for requesting open services

The following code snippet shows the operating procedure for requesting open services:

 
  1. // 1. Create the request object.
  2. APSKOpenServiceRequest *req = [APSKOpenServiceRequest new];
  3. // 2. Set the request type.
  4. req.requestType = APSKOpenServiceRequestTypeLaunchMini;
  5. // 3. Set the required parameters.
  6. MPSKLaunchMiniProgramParam *param = [[MPSKLaunchMiniProgramParam alloc] init];
  7. param.userName = @"xxxxxxxx";
  8. param.path = @"/index.html";
  9. param.miniProgramType = MPSKWXMiniProgramTypeTest;
  10. req.param = param;
  11. // 4. Create the client object
  12. APSKClient *client = [APSKClient new];
  13. // 5. Request service
  14. [client requestOpenService:req toChannel:kAPSKChannelWeixin completionBlock:^(NSError *error, NSDictionary *userInfo) {
  15. // 6. Callback.
  16. if (error) {
  17. NSLog(@"%@", error);
  18. }
  19. }];

Jump back from the channel application

After you jump back from the channel application, the handling depends on whether the mPaaS framework is used on the client.

  • If the mPaaS framework is used on the client, the framework will handle it after the jump back.
  • If the mPaaS framework is not used on the client, refer to the following code snippet to handle it after the jump back.
 
  1. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
  2. {
  3. // Return from the channel application to the source application.
  4. BOOL ret;
  5. ret = [APSKClient handleOpenURL:url];
  6. return ret;
  7. }