FWZFileGetTool.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // FWZFileGetTool.m
  3. // smartRhino
  4. //
  5. // Created by taidi on 2019/12/10.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "FWZFileGetTool.h"
  9. @interface FWZFileGetTool()<UIDocumentPickerDelegate>{
  10. ChoosFileBlock _choosFileBlock;
  11. ShowFileBlock _showFileBlock;
  12. UIImagePickerControllerSourceType _photoType;
  13. BOOL _crop;
  14. }
  15. @end
  16. @implementation FWZFileGetTool
  17. static FWZFileGetTool *tool = nil;
  18. + (instancetype)sharedTool
  19. {
  20. static dispatch_once_t onceToken;
  21. dispatch_once(&onceToken, ^{
  22. tool = [FWZFileGetTool new];
  23. });
  24. return tool;
  25. }
  26. - (void)openFileWithCrop:(BOOL)crop showImgBlock:(ShowFileBlock)showFileBlock choosFileBlock:(ChoosFileBlock)choosFileBlock{
  27. UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc]
  28. initWithDocumentTypes:@[@"public.image",@"com.adobe.pdf",@"com.microsoft.excel.xls",@"public.data",@"public.movie",@"public.text",@"public.audio",@"public.archive",@"com.microsoft.powerpoint.ppt",@"public.item",@"com.apple.application public.data"] inMode:UIDocumentPickerModeImport];
  29. documentPicker.delegate = self;
  30. documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
  31. documentPicker.modalTransitionStyle = [self getCurrentViewController].navigationController.modalTransitionStyle;
  32. documentPicker.modalPresentationStyle = [UtilsTools modalPresentationStyle:[self getCurrentViewController].navigationController.modalPresentationStyle];
  33. [[self getCurrentViewController] presentViewController:documentPicker animated:YES completion:^{
  34. }];
  35. _choosFileBlock = choosFileBlock;
  36. _showFileBlock = showFileBlock;
  37. }
  38. - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
  39. if (controller.documentPickerMode == UIDocumentPickerModeImport) {
  40. NSString *alertMessage = [NSString stringWithFormat:@"Successfully imported %@", [[urls lastObject] lastPathComponent]];
  41. NSLog(@"%@",alertMessage);
  42. NSData *data = [NSData dataWithContentsOfURL:urls.lastObject];
  43. NSLog(@"%@",data);
  44. _showFileBlock([[urls lastObject] lastPathComponent]);
  45. _choosFileBlock(data,[[urls lastObject] lastPathComponent]);
  46. }
  47. }
  48. - (UIViewController *)getCurrentViewController {
  49. __block UIWindow *normalWindow = [[UIApplication sharedApplication].delegate window];
  50. if (normalWindow.windowLevel != UIWindowLevelNormal) {
  51. [[UIApplication sharedApplication].windows enumerateObjectsUsingBlock:^(__kindof UIWindow * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  52. if (obj.windowLevel == UIWindowLevelNormal) {
  53. normalWindow = obj;
  54. *stop = YES;
  55. }
  56. }];
  57. }
  58. return [self nextTopForViewController:normalWindow.rootViewController];
  59. }
  60. - (UIViewController *)nextTopForViewController:(UIViewController *)inViewController {
  61. while (inViewController.presentedViewController) {
  62. inViewController = inViewController.presentedViewController;
  63. }
  64. if ([inViewController isKindOfClass:[UITabBarController class]]) {
  65. UIViewController *selectedVC = [self nextTopForViewController:((UITabBarController *)inViewController).selectedViewController];
  66. return selectedVC;
  67. } else if ([inViewController isKindOfClass:[UINavigationController class]]) {
  68. UIViewController *selectedVC = [self nextTopForViewController:((UINavigationController *)inViewController).visibleViewController];
  69. return selectedVC;
  70. } else {
  71. return inViewController;
  72. }
  73. }
  74. @end