All Products
Search
Document Center

Use SDK

Last Updated: Mar 24, 2021
Important: Since June 28, 2020, mPaaS has stopped support for the baseline 10.1.32. Please use 10.1.68 or 10.1.60 instead. For how to upgrade the baseline from version 10.1.32 to 10.1.68 or 10.1.60, see mPaaS 10.1.68 upgrade guide or mPaaS 10.1.60 upgrade guide.

After you add the MSS SDK, you must configure the project before using the SDK.

Prerequisites

The SDK version is 10.1.32 or later.

Note: You can view the current SDK version in the mpaas_sdk.config file.
 View the SDK version for iOS

Configure a project

Ensure that the meta.config file containing the MSS address and port number has been added to the project.

  • If you have used the latest plug-in to add the MSS SDK, the file will be generated automatically.

  • If your project does not contain the meta.config file, log in to the mPaaS console, choose Overview > Code configuration, download the .config file, rename it tometa.config, and add the file to your project.

    meta_config

Upgrade precautions

The Category file of the DTSyncInterface class does not need to be added since version 10.1.32. The middle tier implements package reading from meta.config. After an upgrade, check whether there is any configuration of the earlier version in the project. If yes, remove it. The following figure shows the Category file of the DTSyncInterface class to be removed from an upgraded version.

Configure a project

Code sample

To realize the logic for listening on the Sync service, you need to create a class, preferably a memory-resident service, to listen on Sync messages. The following code sample creates the MySyncService class to listen on the Sync service.

Before listening on the Sync service, you need define a Sync ID for the sync service (the sync ID will also be used when you create a push configuration on the mPaaS console). This sync ID is the link between you as the user and the service provider. The sync ID in the following example is SYNC-TRADE-DATA.

 
  1. #import <MPMssAdapter/MPSyncInterface.h>
  2. #define SYNC_BIZ_NAME "SYNC-TRADE-DATA";
  3. @implementation MySyncService
  4. + (instancetype)sharedInstance
  5. {
  6. static MySyncService *bizService;
  7. static dispatch_once_t llSOnceToken;
  8. dispatch_once(&llSOnceToken, ^{
  9. bizService = [[self alloc] init];
  10. });
  11. return bizService;
  12. }
  13. -(instancetype)init
  14. {
  15. self = [super init];
  16. if (self) {
  17. [MPSyncInterface initSync];
  18. BOOL registerSingleDeviceSync = [MPSyncInterface registerSyncBizWithName:SYNC_BIZ_NAME syncObserver:self selector:@selector(revSyncBizNotification:)];
  19. [MPSyncInterface bindUserWithSessionId:@"SESSION_DEMO"]; // In this function, **User** corresponds to userId that you specify in the console. It specifies the target to which the console delivers commands, and the value must be the same as that set in the userId function of MPaaSInterface. **SessionId** specifies the authorization token carried by the client. The user login system returns both userId and sessionId. If either changes, this function needs to be called again to ensure that a persistent connection is set up correctly.
  20. }
  21. return self;
  22. }
  23. -(void)revSyncBizNotification:(NSNotification*)notify
  24. {
  25. NSDictionary *userInfo = notify.userInfo;
  26. dispatch_async(dispatch_get_main_queue(), ^{
  27. // Process business data.
  28. [MySyncService handleSyncData:userInfo];
  29. // Call back SyncSDK, indicating that business data has been processed.
  30. [MPSyncInterface responseMessageNotify:userInfo];
  31. });
  32. }
  33. +(void)handleSyncData:(NSDictionary *)userInfo
  34. {
  35. NSString * stringOp = userInfo[@"op"];
  36. NSArray *op = [NSJSONSerialization JSONObjectWithData:[stringOp dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil];
  37. if([op isKindOfClass:[NSArray class]]){
  38. [op enumerateObjectsUsingBlock:^(NSDictionary * item, NSUInteger idx, BOOL *stop) {
  39. if([item isKindOfClass:[NSDictionary class]]){
  40. NSString * plString = item[@"pl"]; // Payload of the business data
  41. if(item[@"isB"]){
  42. NSData *dataPl = [[NSData alloc] initWithBase64EncodedString:plString options:kNilOptions];
  43. NSString *pl = [[NSString alloc] initWithData:dataPl encoding:NSUTF8StringEncoding];
  44. NSLog(@"biz payload data:%@,string:%@",dataPl,pl);
  45. }else{
  46. NSLog(@"biz payload:%@",plString);
  47. }
  48. }
  49. }];
  50. }
  51. }
  52. -(void)dealloc
  53. {
  54. BOOL unRegisterSingleDeviceSync = [MPSyncInterface unRegisterSyncBizWithName:SYNC_BIZ_NAME syncObserver:[MySyncService sharedInstance]];
  55. [MPSyncInterface removeSyncNotificationObserver:self];
  56. }
  57. @end

Follow-up steps