123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- //
- // FWZFileGetTool.m
- // smartRhino
- //
- // Created by taidi on 2019/12/10.
- // Copyright © 2019 tederen. All rights reserved.
- //
- #import "FWZFileGetTool.h"
- @interface FWZFileGetTool()<UIDocumentPickerDelegate>{
- ChoosFileBlock _choosFileBlock;
- ShowFileBlock _showFileBlock;
- UIImagePickerControllerSourceType _photoType;
- BOOL _crop;
- }
- @end
- @implementation FWZFileGetTool
- static FWZFileGetTool *tool = nil;
- + (instancetype)sharedTool
- {
- static dispatch_once_t onceToken;
-
- dispatch_once(&onceToken, ^{
-
- tool = [FWZFileGetTool new];
-
- });
-
- return tool;
- }
- - (void)openFileWithCrop:(BOOL)crop showImgBlock:(ShowFileBlock)showFileBlock choosFileBlock:(ChoosFileBlock)choosFileBlock{
-
- UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc]
- 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];
-
- documentPicker.delegate = self;
-
- documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
-
- documentPicker.modalTransitionStyle = [self getCurrentViewController].navigationController.modalTransitionStyle;
-
- documentPicker.modalPresentationStyle = [UtilsTools modalPresentationStyle:[self getCurrentViewController].navigationController.modalPresentationStyle];
- [[self getCurrentViewController] presentViewController:documentPicker animated:YES completion:^{
-
- }];
-
- _choosFileBlock = choosFileBlock;
- _showFileBlock = showFileBlock;
-
- }
- - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
-
- if (controller.documentPickerMode == UIDocumentPickerModeImport) {
- NSString *alertMessage = [NSString stringWithFormat:@"Successfully imported %@", [[urls lastObject] lastPathComponent]];
-
- NSLog(@"%@",alertMessage);
- NSData *data = [NSData dataWithContentsOfURL:urls.lastObject];
- NSLog(@"%@",data);
- _showFileBlock([[urls lastObject] lastPathComponent]);
- _choosFileBlock(data,[[urls lastObject] lastPathComponent]);
- }
-
- }
- - (UIViewController *)getCurrentViewController {
- __block UIWindow *normalWindow = [[UIApplication sharedApplication].delegate window];
- if (normalWindow.windowLevel != UIWindowLevelNormal) {
- [[UIApplication sharedApplication].windows enumerateObjectsUsingBlock:^(__kindof UIWindow * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
- if (obj.windowLevel == UIWindowLevelNormal) {
- normalWindow = obj;
- *stop = YES;
- }
- }];
- }
-
- return [self nextTopForViewController:normalWindow.rootViewController];
- }
- - (UIViewController *)nextTopForViewController:(UIViewController *)inViewController {
- while (inViewController.presentedViewController) {
- inViewController = inViewController.presentedViewController;
- }
-
- if ([inViewController isKindOfClass:[UITabBarController class]]) {
- UIViewController *selectedVC = [self nextTopForViewController:((UITabBarController *)inViewController).selectedViewController];
- return selectedVC;
- } else if ([inViewController isKindOfClass:[UINavigationController class]]) {
- UIViewController *selectedVC = [self nextTopForViewController:((UINavigationController *)inViewController).visibleViewController];
- return selectedVC;
- } else {
- return inViewController;
- }
- }
- @end
|