All Products
Search
Document Center

mPaaS iOS framework

Last Updated: Mar 10, 2021

mPaaS iOS framework originates from the development framework of Alipay client. In line with the design idea of Framework, mPaaS iOS isolates business into multiple relatively independent modules and aims for achieving high cohesion and low coupling between modules.

mPaaS iOS framework directly takes over the lifecycle of application, and responsible for Host startup, managing application lifecycle, processing and distributing the delegate events of UIApplication, managing each business modules (MicroApplication and Services) in a unified way, etc.

This article gives a detailed introduction to the mPaaS iOS framework.

Host startup

Through the replacement of the main function of the program, the lifecycle of the application is directly taken over. The whole startup process is as follows:

  1. main -> DFClientDelegate -> Open Launcher application

Application lifecycle management

After you access mPaaS framework, it completely replaces AppDelegate. The entire lifecycle of the application is managed by the framework, but you can still implement the delegate methods in different stages of the application’s lifecycle. The framework provides the access method for all delegate methods in the UIApplicationDelegate, you only need to override the corresponding method in Category.

The life cycle method is declared as follows (see DTFrameworkInterface.h file for more information):

  1. /**
  2. * The framework needs to implement certain initialization logic in didFinishLaunching, but this method will be called before execution.
  3. */
  4. - (void)application:(UIApplication *)application beforeDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
  5. /**
  6. * The famework calls back this method, making the accessed application taking over its own didFinishLaunching logic.
  7. * When DTFrameworkCallbackResultReturnYES or DTFrameworkCallbackResultReturnNO is returned, they are directly returned to the system, without executing the subsequent logic.
  8. * This medhod is called back before starting BootLoader, application can make the framework exit in advance by returning DTFrameworkCallbackResultReturnYES or DTFrameworkCallbackResultReturnNO, without running the default BootLoader.
  9. * Use the default implementation in the framework, override is normally not required.
  10. *
  11. * @return : To continue run the framework, or return YES/NO to the system.
  12. */
  13. - (DTFrameworkCallbackResult)application:(UIApplication *)application handleDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
  14. /**
  15. * The framework needs to implement certain initialization logic in didFinishLaunching, but this method will be called after all the logics are done.
  16. */
  17. - (void)application:(UIApplication *)application afterDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
  18. /**
  19. * The framework calls back this method in advance, allowing the accessed application to process the notification message in advance.
  20. * When DTFrameworkCallbackResultContinue is returned, the framework broadcasts the message to global listeners through UIApplicationDidReceiveRemoteNotification, and calls completionHandler(UIBackgroundFetchResultNoData).
  21. * When DTFrameworkCallbackResultReturn is returned, it means the accessed application has completely processed the message, and the framework stops executing the subsequent logic.
  22. */
  23. - (DTFrameworkCallbackResult)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;
  24. /**
  25. * The framework calls back this method in advance, allowing the accessed application to process the notification message in advance.
  26. * When DTFrameworkCallbackResultContinue is returned, the framework broadcasts the message to global listeners through UIApplicationDidReceiveLocalNotification.
  27. * When DTFrameworkCallbackResultReturn is returned, it means the accessed application has completely processed the message, and the framework stops executing the subsequent logic.
  28. */
  29. - (DTFrameworkCallbackResult)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
  30. /**
  31. * The framework calls back this method in advance, allowing the accessed application to process the notification message in advance.
  32. * When DTFrameworkCallbackResultContinue is returned, the framework broadcasts the message to global listeners through UIApplicationDidReceiveLocalNotification, and calls completionHandler().
  33. * When DTFrameworkCallbackResultReturn is returned, it means the accessed application has completely processed the message, and the framework stops executing the subsequent logic.
  34. */
  35. - (DTFrameworkCallbackResult)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler;
  36. /**
  37. * The framework calls back this method in advance, allowing the accessed application to get deviceToken.
  38. * When DTFrameworkCallbackResultContinue is returned, the framework broadcasts the message to global listeners through UIApplicationDidRegisterForRemoteNotifications.
  39. * When DTFrameworkCallbackResultReturn is returned, it means the accessed application has completely processed, and the framework stops executing the subsequent logic.
  40. */
  41. - (DTFrameworkCallbackResult)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
  42. /**
  43. * The framework calls back this method in advance when it fails to obtain deviceToken.
  44. * When DTFrameworkCallbackResultContinue is returned, the framework continues to execute, no other logic currently.
  45. * When DTFrameworkCallbackResultReturn is returned, the framework stops executing the subsequent logic, no other logic currently.
  46. */
  47. - (DTFrameworkCallbackResult)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;
  48. /**
  49. * The framework notifies sharing component (if there is, and shouldAutoactivateShareKit returns YES) in advance, if the sharing component cannot process it, this method is called back to allow the accessed application to process openURL.
  50. * When DTFrameworkCallbackResultReturnYES or DTFrameworkCallbackResultReturnNO is returned, the framework directly returns to the system, without executing the subsequent logic.
  51. * When DTFrameworkCallbackResultContinue is returned, the framework continues to process the URL, and distribute it to SchemeHandler and other classes for further processing.
  52. *
  53. * Comparing with the system method, this method has an additional newURL parameter, allowing the application to return a different URL after processing. If the function returns DTFrameworkCallbackResultContinue and assign value to newURL, the framework will use the new URL for subsequent processing.
  54. */
  55. - (DTFrameworkCallbackResult)application:(UIApplication *)application openURL:(NSURL *)url newURL:(NSURL **)newURL sourceApplication:(NSString *)sourceApplication annotation:(id)annotation;
  56. /**
  57. * The framework calls back this method in advance.
  58. * When DTFrameworkCallbackResultContinue is returned, the framework continues to execute, no other logic currently.
  59. * When DTFrameworkCallbackResultReturn is returned, the framework stops executing the subsequent logic, no other logic currently.
  60. */
  61. - (DTFrameworkCallbackResult)applicationWillResignActive:(UIApplication *)application;
  62. /**
  63. * The framework calls back this method in advance.
  64. * When DTFrameworkCallbackResultContinue is returned, the framework continues to execute, no other logic currently.
  65. * When DTFrameworkCallbackResultReturn is returned, the framework stops executing the subsequent logic, no other logic currently.
  66. */
  67. - (DTFrameworkCallbackResult)applicationDidEnterBackground:(UIApplication *)application;
  68. /**
  69. * The framework calls back this method in advance.
  70. * When DTFrameworkCallbackResultContinue is returned, the framework continues to execute, no other logic currently.
  71. * When DTFrameworkCallbackResultReturn is returned, the framework stops executing the subsequent logic, no other logic currently.
  72. */
  73. - (DTFrameworkCallbackResult)applicationWillEnterForeground:(UIApplication *)application;
  74. /**
  75. * The framework calls back this method in advance.
  76. * When DTFrameworkCallbackResultContinue is returned, the framework continues to execute and give event to sharing component (if there is, and shouldAutoactivateShareKit returns YES). If the entire application is not loaded, BootLoader is called.
  77. * When DTFrameworkCallbackResultReturn is returned, the framework stops executing the subsequent logic, no other logic currently.
  78. */
  79. - (DTFrameworkCallbackResult)applicationDidBecomeActive:(UIApplication *)application;
  80. /**
  81. * The framework calls back this method in advance.
  82. * When DTFrameworkCallbackResultContinue is returned, the framework continues to execute, no other logic currently.
  83. * When DTFrameworkCallbackResultReturn is returned, the framework stops executing the subsequent logic, no other logic currently.
  84. */
  85. - (DTFrameworkCallbackResult)applicationWillTerminate:(UIApplication *)application;
  86. /**
  87. * The framework calls back this method in advance.
  88. * When DTFrameworkCallbackResultContinue is returned, the framework continues to execute, no other logic currently.
  89. * When DTFrameworkCallbackResultReturn is returned, the framework stops executing the subsequent logic, no other logic currently.
  90. */
  91. - (DTFrameworkCallbackResult)applicationDidReceiveMemoryWarning:(UIApplication *)application;
  92. /**
  93. * The framework calls back this method in advance, allowing the accessed application to process the Watch message in advance.
  94. * When DTFrameworkCallbackResultContinue is returned, the framework broadcasts the Watch message to global listeners through UIApplicationWatchKitExtensionRequestNotifications.
  95. * When DTFrameworkCallbackResultReturn is returned, it means the accessed application has completely processed the message, and the framework stops executing the subsequent logic.
  96. */
  97. - (DTFrameworkCallbackResult)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply;
  98. /**
  99. * The framework calls back this method in advance, allowing the accessed application to process the message in advance.
  100. * When DTFrameworkCallbackResultContinue is returned, the framework broadcasts the message to global listeners through UIApplicationUserActivityNotifications, and returns NO to the system in the end.
  101. * When DTFrameworkCallbackResultReturnYES or DTFrameworkCallbackResultReturnNO is returned, the framework directly returns to the system, without executing the subsequent logic.
  102. */
  103. - (DTFrameworkCallbackResult)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray *restorableObjects))restorationHandler;
  104. /**
  105. * The framework calls back this method in advance, allowing the accessed application to process the message of 3D Touch quick entry in advance.
  106. * When DTFrameworkCallbackResultContinue is returned, the framework processes the URL brought by shortcutItem, and calls completionHandler() to return whether is has been processed.
  107. * When DTFrameworkCallbackResultReturn is returned, the framework directly returns to the system, without executing the subsequent logic.
  108. */
  109. - (DTFrameworkCallbackResult)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler;
  110. /**
  111. * Background Fetch mechanism callback
  112. * completionHandler must be called back in 30 seconds, otherwise the process will be terminated.
  113. * To enable this mechanism, you need to configure the fetch option of Background Modes, and then call the following method in didFinishLaunching. See documentation for more information.
  114. * [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
  115. * The default implementation is null, you need to process it in your own way.
  116. */
  117. - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;

Division of application modules

mPaaS framework has defined MicroApplication and Service to separate different modules. Take “whether it has an UI interface” as criteria, the Framework classifies different modules into MicroApplication and Service and implements lifecycle management on the modules via Context.

Terminology Definition
MicroApplication A micro application with UI on client at runtime
Service Lightweight abstracted service provided by client at runtime
Context Context of client micro-component at runtime

This section introduces the concepts of micro application, service, and context. See Create a micro application for more information.

MicroApplication

In the process of developing application based on mPaas iOS framework, we generally set the independent service with UI as a micro application (e.g.: transfer, mobile top-up and other services in Alipay) and isolate it from other services to achieve high independence and zero interdependence among micro applications.

Each micro application has its own lifecycle. The overall process is as follows:

applicationSchedule

The callback methods of micro application through the whole lifecycle (see DTMicroApplicationDelegate.h file for more information):

  1. @required
  2. /**
  3. * Request the delegate of application object to return root view controller.
  4. *
  5. * @param application: Application object.
  6. *
  7. * @return: The root view controller of application.
  8. */
  9. - (UIViewController *)rootControllerInApplication:(DTMicroApplication *)application;
  10. @optional
  11. /**
  12. * Notify the application delegate that the application object has been instantiated.
  13. *
  14. * @param application: Application object.
  15. */
  16. - (void)applicationDidCreate:(DTMicroApplication *)application;
  17. /**
  18. * Notify the application delegate that the application will be launched.
  19. *
  20. * @param application: Launched application object.
  21. * @param options: Running parameters of the application.
  22. */
  23. - (void)application:(DTMicroApplication *)application willStartLaunchingWithOptions:(NSDictionary *)options;
  24. /**
  25. * Notify the application delegate that the application is launched already.
  26. *
  27. * @param application: Launched application object.
  28. */
  29. - (void)applicationDidFinishLaunching:(DTMicroApplication *)application;
  30. /**
  31. * Notify the application delegate that the application will be paused and put into background.
  32. *
  33. * @param application: Launched application object.
  34. */
  35. - (void)applicationWillPause:(DTMicroApplication *)application;
  36. /**
  37. * Notify the application delegate that the application will be reactivated.
  38. *
  39. * @param application: The application object to be activated.
  40. */
  41. - (void)application:(DTMicroApplication *)application willResumeWithOptions:(NSDictionary *)options;
  42. /**
  43. * Notify the application delegate that the application has been activated.
  44. *
  45. * @param application: The application object to be activated.
  46. */
  47. - (void)applicationDidResume:(DTMicroApplication *)application;
  48. /**
  49. * Notify the application delegate that the application has been activated.
  50. *
  51. * @param application: The application object to be activated, together with parameter version.
  52. */
  53. - (void)application:(DTMicroApplication *)application didResumeWithOptions:(NSDictionary *)options;
  54. /**
  55. * Notify the application delegate that the application will quit.
  56. *
  57. * @param application: Application object.
  58. */
  59. - (void)applicationWillTerminate:(DTMicroApplication *)application;
  60. /**
  61. * Notify the application delegate that the application will quit.
  62. *
  63. * @param application: Application object.
  64. * @param animated: Whehter to quit in animated way.
  65. */
  66. - (void)applicationWillTerminate:(DTMicroApplication *)application animated:(BOOL)animated;
  67. /**
  68. * Inquire the application delegate whether the application can quit or not.
  69. * Note: The delegate returns **NO** in some special cases. If it defaults to **Yes**, the application can quit.
  70. *
  71. * @param application: Application object.
  72. *
  73. * @return: Whether the application can quit or not.
  74. */
  75. - (BOOL)applicationShouldTerminate:(DTMicroApplication *)application;

Service

mPaaS iOS framework regards the Framework without UI as service. The differences between microapplication and service are as follows:

  • Microapplication serves as an independent business process while service is used to provide general service.
  • Service is stateful. Once started, the service exists through the whole lifecycle of the client and can be acquired at any time; microapplication will be destroyed after exit.

Relevant interfaces of service management (see DTService.h file for more information):

  1. @required
  2. /**
  3. * Start a service.
  4. * Note:
  5. * The framework will call the method after initialization.
  6. * The service can start an application only when the method is called.
  7. */
  8. - (void)start;
  9. @optional
  10. /**
  11. * A service is created.
  12. */
  13. - (void)didCreate;
  14. /**
  15. * The service will be destroyed.
  16. */
  17. - (void)willDestroy;

Context

Context is the control center of the whole client framework, performing unified management on the interaction and jumps among micro applications and services, with the following responsibilities:

  • Provide an interface for starting micro application. Users can quickly find, close and manage the jumps of the micro application through using name;
  • Provide an interface for starting service, managing the registration, discovery and deregistration of services.

Manage micro application

  • Relevant interfaces of micro application management (see DTContext.h file for more information):
  1. /**
  2. * Start an application as per the given name.
  3. *
  4. * @param name: Name of the application to be started.
  5. * @param params: The parameters need to be forwarded to another application when an application is started.
  6. * @param animated: Specify whether to display animation when starting an application.
  7. *
  8. * @return: Return YES if the application is successfully started, otherwise NO.
  9. */
  10. - (BOOL)startApplication:(NSString *)name params:(NSDictionary *)params animated:(BOOL)animated;
  11. /**
  12. * Start an application as per the given name.
  13. *
  14. * @param name: Name of the application to be started.
  15. * @param params: The parameters need to be forwarded to another application when an application is started.
  16. * @param launchMode: Specify the method of starting application.
  17. *
  18. * @return: Return YES if the application is successfully started, otherwise NO.
  19. */
  20. - (BOOL)startApplication:(NSString *)name params:(NSDictionary *)params launchMode:(DTMicroApplicationLaunchMode)launchMode;
  21. /**
  22. * Find the specified application.
  23. *
  24. * @param name: Name of the application to find.
  25. *
  26. * @return: Return corresponding application object if the specified application is in the application stack, otherwise nil.
  27. */
  28. - (DTMicroApplication *)findApplicationByName:(NSString *)name;
  29. /**
  30. * Return the application which is on the top of the stack currently, namely the application visible to users.
  31. *
  32. * @return: Current visible applications.
  33. */
  34. - (DTMicroApplication *)currentApplication;
  • Micro application starting process:

    app-launcher

Service management

  • Relevant interfaces of service management (see DTContext.h file for more information):
  1. /**
  2. * Find a service as per the given name.
  3. *
  4. * @param name: Service name
  5. *
  6. * @return: Return a service object if the service with given name is found, otherwise null.
  7. */
  8. - (id)findServiceByName:(NSString *)name;
  9. /**
  10. * Register a service.
  11. *
  12. * @param name: Service name
  13. */
  14. - (BOOL)registerService:(id)service forName:(NSString *)name;
  15. /**
  16. * Deregister an existing service.
  17. *
  18. * @param name: Service name
  19. */
  20. - (void)unregisterServiceForName:(NSString *)name;
  • Service starting process:

    service-Launcher

    The UML class diagram illustrating how context manages micro application and service is shown below:

    uml