MGFaceLicenseHandle.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // MGLicenseHandle.m
  3. // MGSDKV2Test
  4. //
  5. // Created by 张英堂 on 16/9/7.
  6. // Copyright © 2016年 megvii. All rights reserved.
  7. //
  8. #import "MGFaceLicenseHandle.h"
  9. #import "MGFacepp.h"
  10. #import "MGNetAccount.h"
  11. @implementation MGFaceLicenseHandle
  12. + (BOOL)getLicense{
  13. NSDate *sdkDate = [self getLicenseDate];
  14. return [self compareSDKDate:sdkDate];
  15. }
  16. + (void)licenseForNetwokrFinish:(void(^)(bool License, NSDate *sdkDate))finish {
  17. // 检查 apk
  18. if ([MG_LICENSE_KEY isEqualToString:@""] || [MG_LICENSE_SECRET isEqualToString:@""]) {
  19. UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"API Key 或 secret 不能为空"
  20. message:@"请到官网申请 ‘https://www.faceplusplus.com.cn’"
  21. preferredStyle:UIAlertControllerStyleAlert];
  22. UIAlertAction *action = [UIAlertAction actionWithTitle:@"好"
  23. style:UIAlertActionStyleCancel
  24. handler:nil];
  25. [controller addAction:action];
  26. UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
  27. UIViewController *currentVC = [MGFaceLicenseHandle getCurrentVCFrom:rootViewController];
  28. [currentVC presentViewController:controller animated:YES completion:nil];
  29. if (finish) {
  30. finish(NO, nil);
  31. }
  32. return;
  33. }
  34. NSDate *licenSDKDate = [self getLicenseDate];
  35. if ([self compareSDKDate:licenSDKDate] == NO) {
  36. if (finish) {
  37. finish(YES, [self getLicenseDate]);
  38. }
  39. return;
  40. }
  41. NSString *version = [MGFacepp getSDKVersion];
  42. NSString *uuid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
  43. [MGLicenseManager getLicenseWithUUID:uuid
  44. version:version
  45. apiKey:MG_LICENSE_KEY
  46. apiSecret:MG_LICENSE_SECRET
  47. apiDuration:1
  48. URLString:MGLicenseURL_CN
  49. finish:^(bool License, NSError *error) {
  50. if (error) {
  51. NSLog(@"Auth error = %@", error);
  52. }
  53. if (License) {
  54. NSDate *nowSDKDate = [self getLicenseDate];
  55. if (finish) {
  56. finish(License, nowSDKDate);
  57. }
  58. }else{
  59. if (finish) {
  60. finish(License, licenSDKDate);
  61. }
  62. }
  63. }];
  64. }
  65. + (NSDate *)getLicenseDate {
  66. NSString *modelPath = [[NSBundle mainBundle] pathForResource:KMGFACEMODELNAME ofType:@""];
  67. NSData *modelData = [NSData dataWithContentsOfFile:modelPath];
  68. MGAlgorithmInfo *sdkInfo = [MGFacepp getSDKAlgorithmInfoWithModel:modelData];
  69. if (sdkInfo.needNetLicense) {
  70. NSString *version = [MGFacepp getSDKVersion];
  71. NSDate *date = [MGLicenseManager getExpiretime:version];
  72. NSLog(@"过期时间 : %@",date);
  73. return date;
  74. } else {
  75. NSLog(@"SDK 为非联网授权版");
  76. return sdkInfo.expireDate;
  77. }
  78. }
  79. + (BOOL)compareSDKDate:(NSDate *)sdkDate{
  80. NSDate *nowDate = [NSDate date];
  81. double result = [sdkDate timeIntervalSinceDate:nowDate];
  82. if (result >= 1*1*60*60.0) {
  83. return NO;
  84. }
  85. return YES;
  86. }
  87. + (BOOL)getNeedNetLicense{
  88. NSString *modelPath = [[NSBundle mainBundle] pathForResource:KMGFACEMODELNAME ofType:@""];
  89. NSData *modelData = [NSData dataWithContentsOfFile:modelPath];
  90. MGAlgorithmInfo *sdkInfo = [MGFacepp getSDKAlgorithmInfoWithModel:modelData];
  91. NSLog(@"\n************\nSDK 功能列表: %@\n是否需要联网授权: %d\n版本号:%@\n过期时间:%@ \n************", sdkInfo.SDKAbility, sdkInfo.needNetLicense, sdkInfo.version, sdkInfo.expireDate);
  92. return sdkInfo.needNetLicense;
  93. }
  94. + (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC
  95. {
  96. UIViewController *currentVC;
  97. if ([rootVC presentedViewController]) {
  98. // 视图是被presented出来的
  99. rootVC = [rootVC presentedViewController];
  100. }
  101. if ([rootVC isKindOfClass:[UITabBarController class]]) {
  102. // 根视图为UITabBarController
  103. currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
  104. } else if ([rootVC isKindOfClass:[UINavigationController class]]){
  105. // 根视图为UINavigationController
  106. currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
  107. } else {
  108. // 根视图为非导航类
  109. currentVC = rootVC;
  110. }
  111. return currentVC;
  112. }
  113. @end