RQShareFunction.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. //
  2. // RQShareFunction.m
  3. // LN_School
  4. //
  5. // Created by 张嵘 on 2018/10/16.
  6. // Copyright © 2018 Danson. All rights reserved.
  7. //
  8. #import "RQShareFunction.h"
  9. #import "TZImagePickerController.h"//第三方相册
  10. #import "TabBarController.h"
  11. #import "GKPhotoBrowser.h"
  12. //#import <MOBFoundation/MobSDK+Privacy.h>
  13. #import "SMSDemoPolicyManager.h"
  14. #import "LocalNotificationManager.h"
  15. static RQShareFunction *_instance = nil;
  16. static dispatch_once_t onceToken;
  17. @interface RQShareFunction () <GKPhotoBrowserDelegate, IDMPhotoBrowserDelegate>
  18. @property (nonatomic, strong) NSArray *dataSource;
  19. @property (nonatomic, weak) GKPhotoBrowser *browser;
  20. @property (nonatomic, strong) UIPageControl *pageControl;
  21. @property (assign, readwrite, nonatomic) BOOL isCanSave;
  22. @end
  23. @implementation RQShareFunction
  24. + (instancetype)shareManager {
  25. return [[self alloc] init];
  26. }
  27. - (instancetype)init{
  28. dispatch_once(&onceToken, ^{
  29. _instance = [super init];
  30. });
  31. return _instance;
  32. }
  33. - (void)setShieldTopicIDArr:(NSArray *)shieldTopicIDArr {
  34. NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
  35. [userDefault setObject:shieldTopicIDArr forKey:@"shieldTopicIDArr"];
  36. [userDefault synchronize];
  37. }
  38. - (NSArray *)shieldTopicIDArr {
  39. NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
  40. NSArray *arr = (NSArray *)[userDefault objectForKey:@"shieldTopicIDArr"]? : [NSArray array];
  41. return arr;
  42. }
  43. - (void)setShieldPeopleIDArr:(NSArray *)shieldPeopleIDArr {
  44. NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
  45. [userDefault setObject:shieldPeopleIDArr forKey:@"shieldPeopleIDArr"];
  46. [userDefault synchronize];
  47. }
  48. - (NSArray *)shieldPeopleIDArr {
  49. NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
  50. NSArray *arr = (NSArray *)[userDefault objectForKey:@"shieldPeopleIDArr"]? : [NSArray array];
  51. return arr;
  52. }
  53. - (void)setAnnouncementIDArr:(NSArray *)announcementIDArr {
  54. NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
  55. [userDefault setObject:announcementIDArr forKey:@"announcementIDArr"];
  56. [userDefault synchronize];
  57. }
  58. - (NSArray *)announcementIDArr {
  59. NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
  60. NSArray *arr = (NSArray *)[userDefault objectForKey:@"announcementIDArr"]? : [NSArray array];
  61. return arr;
  62. }
  63. // 时间转换为时间戳,精确到微秒
  64. - (NSInteger)getTimeStampWithDate:(NSDate *)date {
  65. return [[NSNumber numberWithDouble:[date timeIntervalSince1970] * 1000 * 1000] integerValue];
  66. }
  67. // 时间戳转换为时间
  68. - (NSDate *)getDateWithTimeStamp:(NSInteger)timeStamp {
  69. return [NSDate dateWithTimeIntervalSince1970:timeStamp * 0.001 * 0.001];
  70. }
  71. // 一个时间戳与当前时间的间隔(s)
  72. - (NSInteger)getIntervalsWithTimeStamp:(NSInteger)timeStamp {
  73. return [[NSDate date] timeIntervalSinceDate:[self getDateWithTimeStamp:timeStamp]];
  74. }
  75. - (NSString *)getCurrentTimeString {
  76. NSDate *date = [NSDate date];
  77. NSDateFormatter *formatter = [NSDateFormatter rq_defaultDateFormatter];
  78. NSString *dateString = [formatter stringFromDate:date];
  79. NSString *timeString = dateString;
  80. return timeString;
  81. }
  82. - (NSString *)getCurrentTimeStringWithTimeStamp:(NSInteger)timeStamp {
  83. NSDate *date = [NSDate date];
  84. NSInteger timeStamp0 = [self getTimeStampWithDate:date];
  85. NSInteger timeStamp1 = timeStamp0 + (timeStamp * 1000 * 1000);
  86. NSDate *date1 = [self getDateWithTimeStamp:timeStamp1];
  87. NSDateFormatter *formatter = [NSDateFormatter rq_defaultDateFormatter];
  88. NSString *dateString = [formatter stringFromDate:date1];
  89. NSString *timeString = dateString;
  90. NSLog(@"%@",timeString);
  91. return timeString;
  92. }
  93. // 根据时间字符串和formatter获取时间戳(s)
  94. - (NSInteger)getTimeStampWithTimeStr:(NSString *)timeStr formatter:(NSString *)formatterStr {
  95. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  96. [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
  97. [formatter setDateFormat:formatterStr];
  98. NSDate *date = [formatter dateFromString:timeStr];
  99. NSInteger timeStamp = [self getTimeStampWithDate:date];
  100. return timeStamp * 1000;
  101. }
  102. #pragma mark - RQAlertViewController
  103. - (void)showAlertAtViewController:(nonnull UIViewController *)viewController
  104. WithTitle:(nullable NSString *)title
  105. message:(nullable NSString *)message
  106. alertControllerStyle:(UIAlertControllerStyle)alertControllerStyle
  107. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  108. otherButtonTitles:(nullable NSArray *)otherButtonTitles
  109. otherButtonStyles:(nullable NSDictionary *)otherButtonStyles
  110. preferredActionTitle:(nullable NSString *)preferredActionTitle
  111. completion:(nullable RQAlertViewCompletion)completion {
  112. __block UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:alertControllerStyle];
  113. void (^alertActionHandler) (UIAlertAction *) = [^(UIAlertAction *action) {
  114. if (completion) {
  115. if (action.style == UIAlertActionStyleCancel) {
  116. completion(NSNotFound);
  117. }else {
  118. NSUInteger index = [alertController.actions indexOfObject:action];
  119. completion(cancelButtonTitle? index - 1 : index);
  120. }
  121. }
  122. alertController = nil;
  123. } copy];
  124. if (cancelButtonTitle) {
  125. UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:alertActionHandler];
  126. if (@available(iOS 9.0, *)) {
  127. [cancleAction setValue:defGreen forKey:@"_titleTextColor"];
  128. } else {
  129. // Fallback on earlier versions
  130. }
  131. [alertController addAction:cancleAction];
  132. }
  133. if (otherButtonTitles.count == 0) {
  134. dispatch_async(dispatch_get_main_queue(), ^{
  135. [[RQ_SHARE_FUNCTION topViewController] presentViewController:alertController animated:YES completion:nil];
  136. });
  137. }else {
  138. [otherButtonTitles.rac_sequence.signal subscribeNext:^(NSString * buttonTitle) {
  139. NSNumber *actionStyleNumber = [otherButtonStyles valueForKey:buttonTitle];
  140. UIAlertActionStyle actionStyle = UIAlertActionStyleDefault;
  141. if (actionStyleNumber) {
  142. actionStyle = [actionStyleNumber integerValue];
  143. }
  144. UIAlertAction *action = [UIAlertAction actionWithTitle:buttonTitle
  145. style:actionStyle
  146. handler:alertActionHandler];
  147. if (@available(iOS 9.0, *)) {
  148. [action setValue:defGreen forKey:@"_titleTextColor"];
  149. } else {
  150. // Fallback on earlier versions
  151. }
  152. dispatch_async(dispatch_get_main_queue(), ^{
  153. [alertController addAction:action];
  154. });
  155. ///Support for iOS9 add preferredAction for highlights the text of that action
  156. if ([alertController respondsToSelector:@selector(setPreferredAction:)]) {
  157. if ([preferredActionTitle isEqualToString:buttonTitle]) {
  158. [alertController setPreferredAction:action];
  159. }
  160. }
  161. } completed:^{
  162. dispatch_async(dispatch_get_main_queue(), ^{
  163. [[RQ_SHARE_FUNCTION topViewController] presentViewController:alertController animated:YES completion:nil];
  164. });
  165. }];
  166. }
  167. }
  168. - (void)showAlertWithTitle:(nullable NSString *)title
  169. message:(nullable NSString *)message
  170. alertControllerStyle:(UIAlertControllerStyle)alertControllerStyle
  171. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  172. otherButtonTitles:(nullable NSArray *)otherButtonTitles
  173. otherButtonStyles:(nullable NSDictionary *)otherButtonStyles
  174. preferredActionTitle:(nullable NSString *)preferredActionTitle
  175. completion:(nullable RQAlertViewCompletion)completion{
  176. [self showAlertAtViewController:[RQ_SHARE_FUNCTION topViewController]
  177. WithTitle:title
  178. message:message
  179. alertControllerStyle:alertControllerStyle
  180. cancelButtonTitle:cancelButtonTitle
  181. otherButtonTitles:otherButtonTitles
  182. otherButtonStyles:otherButtonStyles
  183. preferredActionTitle:preferredActionTitle
  184. completion:completion];
  185. }
  186. - (void)showAlertWithTitle:(nullable NSString *)title
  187. message:(nullable NSString *)message
  188. alertControllerStyle:(UIAlertControllerStyle)alertControllerStyle
  189. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  190. otherButtonTitles:(nullable NSArray *)otherButtonTitles
  191. otherButtonStyles:(nullable NSDictionary *)otherButtonStyles
  192. completion:(nullable RQAlertViewCompletion)completion {
  193. [self showAlertAtViewController:[RQ_SHARE_FUNCTION topViewController]
  194. WithTitle:title
  195. message:message
  196. alertControllerStyle:alertControllerStyle
  197. cancelButtonTitle:cancelButtonTitle
  198. otherButtonTitles:otherButtonTitles
  199. otherButtonStyles:otherButtonStyles
  200. preferredActionTitle:nil
  201. completion:completion];
  202. }
  203. - (void)showAlertWithTitle:(nullable NSString *)title
  204. message:(nullable NSString *)message
  205. alertControllerStyle:(UIAlertControllerStyle)alertControllerStyle
  206. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  207. otherButtonTitles:(nullable NSArray *)otherButtonTitles
  208. otherButtonStyles:(nullable NSDictionary *)otherButtonStyles
  209. showInWindow:(BOOL)showInWindow
  210. completion:(nullable RQAlertViewCompletion)completion {
  211. [self showAlertAtViewController:[RQ_SHARE_FUNCTION topViewController]
  212. WithTitle:title
  213. message:message
  214. alertControllerStyle:alertControllerStyle
  215. cancelButtonTitle:cancelButtonTitle
  216. otherButtonTitles:otherButtonTitles
  217. otherButtonStyles:otherButtonStyles
  218. preferredActionTitle:nil
  219. showInWindow:showInWindow
  220. completion:completion];
  221. }
  222. - (void)showAlertAtViewController:(nonnull UIViewController *)viewController
  223. WithTitle:(nullable NSString *)title
  224. message:(nullable NSString *)message
  225. alertControllerStyle:(UIAlertControllerStyle)alertControllerStyle
  226. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  227. otherButtonTitles:(nullable NSArray *)otherButtonTitles
  228. otherButtonStyles:(nullable NSDictionary *)otherButtonStyles
  229. preferredActionTitle:(nullable NSString *)preferredActionTitle
  230. showInWindow:(BOOL)showInWindow
  231. completion:(nullable RQAlertViewCompletion)completion {
  232. @weakify(self)
  233. __block UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:alertControllerStyle];
  234. void (^alertActionHandler) (UIAlertAction *) = [^(UIAlertAction *action) {
  235. if (completion) {
  236. if (action.style == UIAlertActionStyleCancel) {
  237. completion(NSNotFound);
  238. }else {
  239. NSUInteger index = [alertController.actions indexOfObject:action];
  240. completion(cancelButtonTitle? index - 1 : index);
  241. }
  242. }
  243. alertController = nil;
  244. } copy];
  245. if (cancelButtonTitle) {
  246. UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:alertActionHandler];
  247. if (@available(iOS 9.0, *)) {
  248. [cancleAction setValue:RQ_MAIN_COLOR forKey:@"_titleTextColor"];
  249. } else {
  250. // Fallback on earlier versions
  251. }
  252. [alertController addAction:cancleAction];
  253. }
  254. if (otherButtonTitles.count == 0) {
  255. dispatch_async(dispatch_get_main_queue(), ^{
  256. @strongify(self)
  257. if (showInWindow) {
  258. [[self getKeyWindow].rootViewController presentViewController:alertController animated:YES completion:nil];
  259. } else {
  260. [[RQ_SHARE_FUNCTION topViewController] presentViewController:alertController animated:YES completion:nil];
  261. }
  262. });
  263. }else {
  264. [otherButtonTitles.rac_sequence.signal subscribeNext:^(NSString * buttonTitle) {
  265. NSNumber *actionStyleNumber = [otherButtonStyles valueForKey:buttonTitle];
  266. UIAlertActionStyle actionStyle = UIAlertActionStyleDefault;
  267. if (actionStyleNumber) {
  268. actionStyle = [actionStyleNumber integerValue];
  269. }
  270. UIAlertAction *action = [UIAlertAction actionWithTitle:buttonTitle
  271. style:actionStyle
  272. handler:alertActionHandler];
  273. if (@available(iOS 9.0, *)) {
  274. [action setValue:RQ_MAIN_COLOR forKey:@"_titleTextColor"];
  275. } else {
  276. // Fallback on earlier versions
  277. }
  278. [alertController addAction:action];
  279. ///Support for iOS9 add preferredAction for highlights the text of that action
  280. if ([alertController respondsToSelector:@selector(setPreferredAction:)]) {
  281. if ([preferredActionTitle isEqualToString:buttonTitle]) {
  282. if (@available(iOS 9.0, *)) {
  283. [alertController setPreferredAction:action];
  284. } else {
  285. // Fallback on earlier versions
  286. }
  287. }
  288. }
  289. } completed:^{
  290. dispatch_async(dispatch_get_main_queue(), ^{
  291. @strongify(self)
  292. if (showInWindow) {
  293. [[self getKeyWindow].rootViewController presentViewController:alertController animated:YES completion:nil];
  294. } else {
  295. [[RQ_SHARE_FUNCTION topViewController] presentViewController:alertController animated:YES completion:nil];
  296. }
  297. });
  298. }];
  299. }
  300. }
  301. #pragma mark - Custom Way
  302. - (UIViewController *)topViewController {
  303. return [RQ_SHARE_FUNCTION topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
  304. }
  305. - (UIViewController *)topViewController:(UIViewController*)rootViewController {
  306. if (rootViewController.presentedViewController == nil ||
  307. rootViewController.presentedViewController.beingDismissed) {
  308. return rootViewController;
  309. }
  310. if ([rootViewController.presentedViewController isMemberOfClass:[UINavigationController class]]) {
  311. UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
  312. UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
  313. return [RQ_SHARE_FUNCTION topViewController:lastViewController];
  314. }
  315. UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;
  316. return [RQ_SHARE_FUNCTION topViewController:presentedViewController];
  317. }
  318. - (UIViewController *)currentViewController {
  319. UIViewController *resultVC;
  320. resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
  321. /// RQ Fixed : 这里必须要判断一下,否则取出来永远都是 RQMainTabBarViewController。这是架构上小缺(特)陷(性)。因为RQMainTabBarViewController的子控制器是UITabBarController,所以需要递归UITabBarController的所有的子控制器
  322. if ([resultVC isKindOfClass:[TabBarController class]]) {
  323. TabBarController *mainVc = (TabBarController *)resultVC;
  324. resultVC = [self _topViewController:mainVc.tabBarController];
  325. }
  326. while (resultVC.presentedViewController) {
  327. resultVC = [self _topViewController:resultVC.presentedViewController];
  328. }
  329. return resultVC;
  330. }
  331. - (UIViewController *)_topViewController:(UIViewController *)vc {
  332. if ([vc isKindOfClass:[UINavigationController class]]) {
  333. return [self _topViewController:[(UINavigationController *)vc topViewController]];
  334. } else if ([vc isKindOfClass:[UITabBarController class]]) {
  335. return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
  336. } else {
  337. return vc;
  338. }
  339. }
  340. - (NSData *)compressQualityWithImage:(UIImage *)image MaxLength:(NSInteger)maxLength {
  341. CGFloat compression = 1;
  342. NSData *data = UIImageJPEGRepresentation(image, compression);
  343. if (data.length < maxLength) return data;
  344. CGFloat max = 1;
  345. CGFloat min = 0;
  346. NSData *lastData;
  347. for (int i = 0; i < 99; ++i) {
  348. compression = (max + min) / 2;
  349. data = UIImageJPEGRepresentation(image, compression);
  350. if (data.length == lastData.length) {
  351. return data;
  352. }else {
  353. lastData = data;
  354. }
  355. if (data.length < maxLength * 0.9) {
  356. min = compression;
  357. } else if (data.length > maxLength) {
  358. max = compression;
  359. } else {
  360. break;
  361. }
  362. }
  363. return data;
  364. }
  365. /**
  366. * 展示大图
  367. *
  368. * @author ZhangRong
  369. * @date 2020-06-16 14:30:55
  370. *
  371. * @param dataSource 图片数组(Url数组)
  372. * @param currentIndex 当前标签
  373. * @param isCanSave 是否保存图片
  374. */
  375. - (void)showPhotoBrowserWithDataSource:(NSArray *)dataSource currentIndex:(NSInteger)currentIndex isCanSave:(BOOL)isCanSave {
  376. if (dataSource.count == 0) {
  377. ShowMsg(@"暂无图片");
  378. return;
  379. }
  380. NSMutableArray *photos = [NSMutableArray new];
  381. self.dataSource = dataSource;
  382. self.isCanSave = isCanSave;
  383. [self.dataSource enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  384. if ([obj isKindOfClass:[NSDictionary class]]) {
  385. // GKPhoto *photo = [GKPhoto new];
  386. // photo.url = [NSURL URLWithString:obj[@"IMGPATH"]];
  387. // [photos addObject:photo];
  388. IDMPhoto *photo = [IDMPhoto photoWithFilePath:obj[@"IMGPATH"]];
  389. [photos addObject:photo];
  390. }else if ([obj isKindOfClass:[NSString class]]) {
  391. // // 替换为中等尺寸图片
  392. // if ([obj rangeOfString:@"thumbnail"].location != NSNotFound) {
  393. // //这个要了没用噢 除非以后我们有清晰的图
  394. // obj= [obj stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
  395. // }
  396. // NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
  397. // NSString *str = [obj stringByTrimmingCharactersInSet:whitespace];
  398. // if (str && ![str hasPrefix:@"http"]){
  399. // str = [imgPreFix stringByAppendingString:[obj stringByTrimmingCharactersInSet:whitespace]];
  400. // }
  401. // GKPhoto *photo = [GKPhoto new];
  402. // photo.url = [NSURL URLWithString:str];
  403. // [photos addObject:photo];
  404. IDMPhoto *photo = [IDMPhoto photoWithURL:[NSURL URLWithString:obj]];
  405. [photos addObject:photo];
  406. }else if ([obj isKindOfClass:[UIImage class]]) {
  407. // GKPhoto *photo = [GKPhoto new];
  408. // photo.image = obj;
  409. // [photos addObject:photo];
  410. IDMPhoto *photo = [IDMPhoto photoWithImage:obj];
  411. [photos addObject:photo];
  412. }
  413. }];
  414. [RQPhotoManager showPhotoBrowser:[RQ_SHARE_FUNCTION topViewController] photos:photos initialPageIndex:currentIndex delegate:self];
  415. // self.pageControl = [[UIPageControl alloc] init];
  416. // self.pageControl.numberOfPages = photos.count;
  417. // self.pageControl.currentPage = currentIndex;
  418. // self.pageControl.hidesForSinglePage = YES;
  419. //
  420. // self.browser = [GKPhotoBrowser photoBrowserWithPhotos:photos currentIndex:currentIndex];
  421. // _browser.delegate = self;
  422. // _browser.showStyle = GKPhotoBrowserShowStyleNone; // 缩放显示
  423. // _browser.hideStyle = GKPhotoBrowserHideStyleZoomScale; // 缩放隐藏
  424. // _browser.loadStyle = GKPhotoBrowserLoadStyleDeterminate; // 不明确的加载方式带阴影
  425. // _browser.maxZoomScale = 20.0f;
  426. // _browser.doubleZoomScale = 2.0f;
  427. // [_browser setupCoverViews:@[self.pageControl] layoutBlock:^(GKPhotoBrowser *photoBrowser, CGRect superFrame) {
  428. // CGFloat pointY = 0;
  429. // if (photoBrowser.isLandscape) {
  430. // pointY = superFrame.size.height - 20;
  431. // }else {
  432. // pointY = superFrame.size.height - 10 - kSafeAreaBottomHeight;
  433. // }
  434. //
  435. // self.pageControl.center = CGPointMake(superFrame.size.width * 0.5, pointY);
  436. // }];
  437. // [_browser showFromVC:[RQ_SHARE_FUNCTION topViewController]];
  438. }
  439. #pragma mark - GKPhotoBrowserDelegate
  440. - (void)photoBrowser:(GKPhotoBrowser *)browser didChangedIndex:(NSInteger)index {
  441. self.pageControl.currentPage = index;
  442. }
  443. - (void)photoBrowser:(GKPhotoBrowser *)browser longPressWithIndex:(NSInteger)index {
  444. if (!_isCanSave) return;
  445. [RQ_SHARE_FUNCTION showAlertWithTitle:nil message:nil alertControllerStyle:UIAlertControllerStyleActionSheet cancelButtonTitle:@"取消" otherButtonTitles:@[@"保存图片"] otherButtonStyles:nil completion:^(NSUInteger selectedOtherButtonIndex) {
  446. if (selectedOtherButtonIndex == 0) {
  447. GKPhoto *photo = self.browser.photos[self.browser.currentIndex];
  448. NSData *imageData = nil;
  449. if ([photo.image isKindOfClass:[SDAnimatedImage class]]) {
  450. imageData = [(SDAnimatedImage *)photo.image animatedImageData];
  451. }else {
  452. imageData = [photo.image sd_imageData];
  453. }
  454. if (!imageData) return;
  455. [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
  456. if (@available(iOS 9, *)) {
  457. PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAsset];
  458. [request addResourceWithType:PHAssetResourceTypePhoto data:imageData options:nil];
  459. request.creationDate = [NSDate date];
  460. }
  461. } completionHandler:^(BOOL success, NSError *error) {
  462. dispatch_async(dispatch_get_main_queue(), ^{
  463. if (success) {
  464. NSLog(@"保存照片成功");
  465. ShowMsg(@"图片保存成功");
  466. } else if (error) {
  467. ShowMsg(@"保存保存失败");
  468. NSLog(@"保存照片出错:%@",error.localizedDescription);
  469. }
  470. });
  471. }];
  472. }
  473. }];
  474. }
  475. /**
  476. * Mob隐私判断
  477. *
  478. * @author ZhangRong
  479. * @date 2020-06-16 14:30:55
  480. */
  481. - (void)checkMobPolicyOnResult:(void (^_Nullable)(BOOL success))handler {
  482. //获取隐私协议
  483. // [MobSDK getPrivacyPolicy:@"1" language:@"zh" compeletion:^(NSDictionary * _Nullable data, NSError * _Nullable error) {
  484. //
  485. // NSString *url = data[@"content"];
  486. // if(url) {
  487. // handler(YES);
  488. // [SMSDemoPolicyManager show:url compeletion:^(BOOL accept) {
  489. // //是否接受隐私协议
  490. // [MobSDK uploadPrivacyPermissionStatus:accept onResult:^(BOOL success) {
  491. // NSLog(@"%@",success? @"MobSDK隐私授权成功" : @"MobSDK隐私授权失败");
  492. // }];
  493. //
  494. // }];
  495. // }
  496. //
  497. // }];
  498. }
  499. - (CGFloat)RQADViewHeight {
  500. return RQ_SCREEN_WIDTH / 3.f;
  501. }
  502. - (void)getPhotosWithGetPhotosWay:(GetPhotosWay)getPhotosWay size:(CGSize)size maxLength:(NSUInteger)maxLength maxImagesCount:(NSUInteger)maxImagesCount isCheckBody:(BOOL)isCheckBody photosBlock:(PhotosBlock)photosBlock {
  503. if (getPhotosWay == GetPhotosWay_Camera) {
  504. [RQPhotoManager fetchPhotosFromCamera:RQ_SHARE_FUNCTION.topViewController allowCrop:NO completion:^(UIImage * _Nonnull image, id _Nonnull asset) {
  505. if (image) {
  506. NSMutableArray *imagesArr = [NSMutableArray arrayWithObject:image];
  507. NSData *data;
  508. if (CGSizeEqualToSize(size, CGSizeZero)) {
  509. data = [UIImage resetSizeOfImageData:image maxSize:maxLength];
  510. }else {
  511. data = [UIImage resetSizeOfImageData:[image scaledAndCutToSize:size] maxSize:maxLength];
  512. }
  513. NSString *imgString = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
  514. NSMutableArray *imagesDataStrArr = [NSMutableArray arrayWithObject:imgString];
  515. if (photosBlock) {
  516. photosBlock(imagesArr, imagesDataStrArr);
  517. }
  518. } else {
  519. if (photosBlock) {
  520. photosBlock(@[], @[]);
  521. }
  522. }
  523. }];
  524. return;
  525. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  526. AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  527. if (authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) {
  528. [RQ_SHARE_FUNCTION showAlertWithTitle:@"温馨提示" message:@"请在iPhone的“设置”-“隐私”-“相机”功能中,找到“极速驾培”打开相机访问权限" alertControllerStyle:UIAlertControllerStyleAlert cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] otherButtonStyles:nil completion:^(NSUInteger selectedOtherButtonIndex) {
  529. if (selectedOtherButtonIndex == 0) {
  530. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  531. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  532. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  533. }
  534. }
  535. }];
  536. if (photosBlock) {
  537. photosBlock(@[], @[]);
  538. }
  539. return;
  540. } else if (authStatus == AVAuthorizationStatusNotDetermined) {
  541. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
  542. if (granted) {
  543. SLShotViewController *vc = [[SLShotViewController alloc] init];
  544. vc.isCheckBody = isCheckBody;
  545. [vc initTakePhotoBlock:^(UIImage * _Nullable image) {
  546. if (image) {
  547. NSMutableArray *imagesArr = [NSMutableArray arrayWithObject:image];
  548. NSData *data;
  549. if (CGSizeEqualToSize(size, CGSizeZero)) {
  550. data = [UIImage resetSizeOfImageData:image maxSize:maxLength];
  551. }else {
  552. data = [UIImage resetSizeOfImageData:[image scaledAndCutToSize:size] maxSize:maxLength];
  553. }
  554. NSString *imgString = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
  555. NSMutableArray *imagesDataStrArr = [NSMutableArray arrayWithObject:imgString];
  556. if (photosBlock) {
  557. photosBlock(imagesArr, imagesDataStrArr);
  558. }
  559. } else {
  560. if (photosBlock) {
  561. photosBlock(@[], @[]);
  562. }
  563. }
  564. }];
  565. dispatch_async(dispatch_get_main_queue(), ^{
  566. vc.modalPresentationStyle = UIModalPresentationFullScreen;
  567. [[RQ_SHARE_FUNCTION topViewController] presentViewController:vc
  568. animated:NO
  569. completion:nil];
  570. });
  571. } else {
  572. if (photosBlock) {
  573. photosBlock(@[], @[]);
  574. }
  575. }
  576. }];
  577. return;
  578. } else if (authStatus == AVAuthorizationStatusAuthorized) {
  579. SLShotViewController *vc = [[SLShotViewController alloc] init];
  580. vc.isCheckBody = isCheckBody;
  581. [vc initTakePhotoBlock:^(UIImage * _Nullable image) {
  582. if (image) {
  583. NSMutableArray *imagesArr = [NSMutableArray arrayWithObject:image];
  584. NSData *data;
  585. if (CGSizeEqualToSize(size, CGSizeZero)) {
  586. data = [UIImage resetSizeOfImageData:image maxSize:maxLength];
  587. }else {
  588. data = [UIImage resetSizeOfImageData:[image scaledAndCutToSize:size] maxSize:maxLength];
  589. }
  590. NSString *imgString = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
  591. NSMutableArray *imagesDataStrArr = [NSMutableArray arrayWithObject:imgString];
  592. if (photosBlock) {
  593. photosBlock(imagesArr, imagesDataStrArr);
  594. }
  595. } else {
  596. if (photosBlock) {
  597. photosBlock(@[], @[]);
  598. }
  599. }
  600. }];
  601. dispatch_async(dispatch_get_main_queue(), ^{
  602. vc.modalPresentationStyle = UIModalPresentationFullScreen;
  603. [[RQ_SHARE_FUNCTION topViewController] presentViewController:vc
  604. animated:NO
  605. completion:nil];
  606. });
  607. } else {
  608. if (photosBlock) {
  609. photosBlock(@[], @[]);
  610. }
  611. return;
  612. }
  613. } else {
  614. NSLog(@"相机调用失败");
  615. if (photosBlock) {
  616. photosBlock(@[], @[]);
  617. }
  618. }
  619. }else if (getPhotosWay == GetPhotosWay_Album) {
  620. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
  621. TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:maxImagesCount ? maxImagesCount : 1 delegate:nil];
  622. imagePickerVc.naviBgColor = defGreen;
  623. imagePickerVc.iconThemeColor = defGreen;
  624. imagePickerVc.allowPickingVideo = NO;
  625. [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isStop) {
  626. NSMutableArray *imagesDataStrArr = [NSMutableArray array];
  627. @synchronized (imagesDataStrArr) {
  628. for (UIImage *image in photos) {
  629. NSData *data;
  630. if (CGSizeEqualToSize(size, CGSizeZero)) {
  631. data = [UIImage resetSizeOfImageData:image maxSize:maxLength];
  632. }else {
  633. data = [UIImage resetSizeOfImageData:[image scaledAndCutToSize:size] maxSize:maxLength];
  634. }
  635. NSString *imgString = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
  636. [imagesDataStrArr addObject:imgString];
  637. }
  638. }
  639. if (photosBlock) {
  640. photosBlock(photos, imagesDataStrArr);
  641. }
  642. }];
  643. dispatch_async(dispatch_get_main_queue(), ^{
  644. imagePickerVc.modalPresentationStyle = UIModalPresentationFullScreen;
  645. [[RQ_SHARE_FUNCTION topViewController] presentViewController:imagePickerVc animated:YES completion:nil];
  646. });
  647. }else{
  648. NSLog(@"相册调用失败");
  649. }
  650. }
  651. }
  652. - (void)saveObjectWithObject:(id)object ForKey:(NSString* )key {
  653. NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
  654. if (!object) {
  655. [ud removeObjectForKey:key];
  656. }else{
  657. [ud setObject:object forKey:key];
  658. }
  659. [ud synchronize];
  660. }
  661. - (id)getObjectWithKey:(NSString *)key {
  662. NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
  663. return [ud objectForKey:key];
  664. }
  665. // 检测抓包
  666. - (BOOL)checkProxySetting {
  667. NSDictionary *proxySettings = (__bridge NSDictionary *)(CFNetworkCopySystemProxySettings());
  668. NSArray *proxies = (__bridge NSArray *)(CFNetworkCopyProxiesForURL((__bridge CFURLRef _Nonnull)([NSURL URLWithString:@"https://www.baidu.com"]), (__bridge CFDictionaryRef _Nonnull)(proxySettings)));
  669. NSLog(@"\n%@",proxies);
  670. NSDictionary *settings = proxies[0];
  671. NSLog(@"%@",[settings objectForKey:(NSString *)kCFProxyHostNameKey]);
  672. NSLog(@"%@",[settings objectForKey:(NSString *)kCFProxyPortNumberKey]);
  673. NSLog(@"%@",[settings objectForKey:(NSString *)kCFProxyTypeKey]);
  674. if ([[settings objectForKey:(NSString *)kCFProxyTypeKey] isEqualToString:@"kCFProxyTypeNone"]) {
  675. NSLog(@"没设置代理");
  676. return NO;
  677. } else {
  678. NSLog(@"设置了代理");
  679. return YES;
  680. }
  681. }
  682. - (void)gotoWebViewWithUrlStr:(NSString *)url {
  683. RQWebViewViewController *vc = [[RQWebViewViewController alloc] init];
  684. vc.url = url;
  685. vc.modalPresentationStyle = UIModalPresentationFullScreen;
  686. vc.webType = WebTypeDefault;
  687. RQBaseNavigationController *nav = [[RQBaseNavigationController alloc] initWithRootViewController:vc];
  688. [RQ_SHARE_FUNCTION.topViewController presentViewController:nav animated:NO completion:nil];
  689. }
  690. // 前往小程序
  691. - (void)miniwithUserName:(NSString *)userName path:(NSString *)path {
  692. WXLaunchMiniProgramReq *launchMiniProgramReq = [WXLaunchMiniProgramReq object];
  693. launchMiniProgramReq.userName = userName; ////拉起的小程序的username
  694. launchMiniProgramReq.path = path; ////拉起小程序页面的可带参路径,不填默认拉起小程序首页,对于小游戏,可以只传入 query 部分,来实现传参效果,如:传入 "?foo=bar"。
  695. launchMiniProgramReq.miniProgramType = WXMiniProgramTypeRelease; ///拉起小程序的类型
  696. return [WXApi sendReq:launchMiniProgramReq completion:^(BOOL success) {
  697. }];
  698. }
  699. - (UIWindow *)getKeyWindow {
  700. if (@available(iOS 13.0, *)) {
  701. for (UIWindowScene* windowScene in [UIApplication sharedApplication].connectedScenes) {
  702. if (windowScene.activationState == UISceneActivationStateForegroundActive) {
  703. for (UIWindow *window in windowScene.windows) {
  704. if (window.isKeyWindow) {
  705. return window;
  706. break;
  707. }
  708. }
  709. }
  710. }
  711. } else {
  712. return [UIApplication sharedApplication].keyWindow;
  713. }
  714. return nil;
  715. }
  716. @end