SDKWrapper.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // SDKWrapper.m
  3. // polish_project-mobile
  4. //
  5. // Created by 杨欣 on 2018/10/20.
  6. //
  7. #import "SDKWrapper.h"
  8. #import "SDKDelegate.h"
  9. @interface SDKWrapper ()
  10. @property (nonatomic, strong) NSArray *sdkClasses;
  11. @end
  12. @implementation SDKWrapper
  13. #pragma mark -
  14. #pragma mark Singleton
  15. static SDKWrapper *mInstace = nil;
  16. + (instancetype)getInstance {
  17. static dispatch_once_t onceToken;
  18. dispatch_once(&onceToken, ^{
  19. mInstace = [[super allocWithZone:NULL] init];
  20. [mInstace initSDKWrapper];
  21. });
  22. return mInstace;
  23. }
  24. + (id)allocWithZone:(struct _NSZone *)zone {
  25. return [SDKWrapper getInstance];
  26. }
  27. + (id)copyWithZone:(struct _NSZone *)zone {
  28. return [SDKWrapper getInstance];
  29. }
  30. #pragma mark -
  31. #pragma mark Application lifecycle
  32. - (void)initSDKWrapper {
  33. [self loadSDKClass];
  34. }
  35. - (void)loadSDKClass {
  36. NSString *path = [NSString stringWithFormat:@"%@/project.json",
  37. [[NSBundle mainBundle] resourcePath]];
  38. NSData *data = [NSData dataWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:nil];
  39. id obj = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
  40. id dic = obj[@"serviceClassPath"];
  41. NSLog(@"%@",dic);
  42. NSMutableArray *sdks = [NSMutableArray array];
  43. for (NSString *str in dic) {
  44. @try {
  45. NSString *className = [[str componentsSeparatedByString:@"."] lastObject];
  46. NSLog(@"%@",className);
  47. Class c = NSClassFromString(className);
  48. id sdk = [[c alloc] init];
  49. [sdks addObject:sdk];
  50. NSLog(@"%@",sdk);
  51. } @catch (NSException *e) {
  52. NSLog(@"%@",@"NSException");
  53. NSLog(@"%@",e);
  54. }
  55. }
  56. self.sdkClasses = [NSArray arrayWithArray:sdks];
  57. }
  58. /**
  59. app显示给用户之前执行最后的初始化操作
  60. */
  61. - (void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  62. for (id <SDKDelegate> sdk in self.sdkClasses) {
  63. if ([sdk respondsToSelector:@selector(application:didFinishLaunchingWithOptions:)]) {
  64. [sdk application:application didFinishLaunchingWithOptions:launchOptions];
  65. }
  66. }
  67. }
  68. /**
  69. app已经切换到active状态后需要执行的操作
  70. */
  71. - (void)applicationDidBecomeActive:(UIApplication *)application {
  72. for (id sdk in self.sdkClasses) {
  73. if ([sdk respondsToSelector:@selector(applicationDidBecomeActive:)]) {
  74. [sdk applicationDidBecomeActive:application];
  75. }
  76. }
  77. }
  78. /**
  79. app将要从前台切换到后台时需要执行的操作
  80. */
  81. - (void)applicationWillResignActive:(UIApplication *)application {
  82. for (id sdk in self.sdkClasses) {
  83. if ([sdk respondsToSelector:@selector(applicationWillResignActive:)]) {
  84. [sdk applicationWillResignActive:application];
  85. }
  86. }
  87. }
  88. /**
  89. app已经进入后台后需要执行的操作
  90. */
  91. - (void)applicationDidEnterBackground:(UIApplication *)application {
  92. for (id sdk in self.sdkClasses) {
  93. if ([sdk respondsToSelector:@selector(applicationDidEnterBackground:)]) {
  94. [sdk applicationDidEnterBackground:application];
  95. }
  96. }
  97. }
  98. /**
  99. app将要从后台切换到前台需要执行的操作,但app还不是active状态
  100. */
  101. - (void)applicationWillEnterForeground:(UIApplication *)application {
  102. for (id sdk in self.sdkClasses) {
  103. if ([sdk respondsToSelector:@selector(applicationWillEnterForeground:)]) {
  104. [sdk applicationWillEnterForeground:application];
  105. }
  106. }
  107. }
  108. /**
  109. app将要结束时需要执行的操作
  110. */
  111. - (void)applicationWillTerminate:(UIApplication *)application {
  112. for (id sdk in self.sdkClasses) {
  113. if ([sdk respondsToSelector:@selector(applicationWillTerminate:)]) {
  114. [sdk applicationWillTerminate:application];
  115. }
  116. }
  117. }
  118. @end