Просмотр исходного кода

1.修改iOS Apex Mobile Result文档预览菜单,增加分享。

Pen Li 8 лет назад
Родитель
Сommit
5d16f9b261
1 измененных файлов с 145 добавлено и 30 удалено
  1. 145 30
      Apex Mobile/Apex Mobile/AMResultViewController.m

+ 145 - 30
Apex Mobile/Apex Mobile/AMResultViewController.m

@@ -16,9 +16,10 @@
 #pragma clang diagnostic push
 #pragma clang diagnostic ignored"-Wdeprecated-declarations"
 
-@interface AMResultViewController ()
+@interface AMResultViewController () <UIDocumentInteractionControllerDelegate>
 
 @property (nonatomic,assign) BOOL dirty;
+@property (nonatomic,strong) UIDocumentInteractionController *documentController;
 
 @end
 
@@ -70,40 +71,13 @@
 //    [self resetContraint];
     [self configureTableView];
     
-    UIBarButtonItem *saveBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ic_save"] style:UIBarButtonItemStylePlain target:self action:@selector(saveDocumentClick:)];
-    self.quickLook.navigationItem.rightBarButtonItem = saveBtn;
+    UIBarButtonItem *previewMenuBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ic_menu"] style:UIBarButtonItemStylePlain target:self action:@selector(quickLookMenuClick:)];
+    self.quickLook.navigationItem.rightBarButtonItem = previewMenuBtn;
     
 //    self.quickLook.toolbarItems = nil;
 //    self.quickLook.accessibilityCustomActions = nil;
 }
 
-- (void)saveDocumentClick:(id)sender {
-    
-    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
-    NSString *path = [paths objectAtIndex:0];
-    NSString *filePath = [path stringByAppendingPathComponent:[self.documentPath lastPathComponent]];
-    
-    NSError *err;
-    
-    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
-        [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
-    }
-    [[NSFileManager defaultManager] moveItemAtPath:self.documentPath toPath:filePath error:&err];
-    
-    UIAlertController *alertVC;
-    UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-        
-    }];
-    if (err) {
-        alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:[NSString stringWithFormat:@"save document failed:\n%@",err.localizedDescription] preferredStyle:UIAlertControllerStyleAlert];
-    } else {
-        alertVC = [UIAlertController alertControllerWithTitle:nil message:@"save document success" preferredStyle:UIAlertControllerStyleAlert];
-    }
-    [alertVC addAction:action];
-    
-    [self presentViewController:alertVC animated:YES completion:nil];
-}
-
 - (void)viewWillAppear:(BOOL)animated {
     [super viewWillAppear:animated];
     
@@ -565,6 +539,89 @@
     [self presentViewController:alertControl animated:NO completion:nil];
 }
 
+#pragma mark - Quick Look Menu Action
+
+- (void)saveDocument {
+    
+    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
+    NSString *path = [paths objectAtIndex:0];
+    NSString *filePath = [path stringByAppendingPathComponent:[self.documentPath lastPathComponent]];
+    
+    NSError *err;
+    
+    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
+        [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
+    }
+    [[NSFileManager defaultManager] moveItemAtPath:self.documentPath toPath:filePath error:&err];
+    
+    UIAlertController *alertVC;
+    UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+        
+    }];
+    if (err) {
+        alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:[NSString stringWithFormat:@"save document failed:\n%@",err.localizedDescription] preferredStyle:UIAlertControllerStyleAlert];
+    } else {
+        alertVC = [UIAlertController alertControllerWithTitle:nil message:@"save document success" preferredStyle:UIAlertControllerStyleAlert];
+    }
+    [alertVC addAction:action];
+    
+    [self presentViewController:alertVC animated:YES completion:nil];
+}
+
+- (void)shareDocument {
+    
+    // 默认为保存,取保存路径
+    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
+    NSString *path = [paths objectAtIndex:0];
+    NSString *filePath = [path stringByAppendingPathComponent:[self.documentPath lastPathComponent]];
+    
+    // 没有保存的情况
+    if ([[NSFileManager defaultManager] fileExistsAtPath:self.documentPath]) {
+        filePath = self.documentPath;
+    }
+    NSURL *fileURL = [NSURL fileURLWithPath:filePath];
+    
+    if (self.documentController == nil) {
+        UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
+        documentController.delegate = self;
+        documentController.UTI = @"com.adobe.pdf";
+        self.documentController = documentController;
+    } else {
+        self.documentController.URL = fileURL;
+    }
+    self.documentController.annotation = @{@"_toRecipients" : @[@"676034647@qq.com"]};
+    self.documentController.name = @"Test";
+    
+//    [self.documentController presentOpenInMenuFromRect:self.navigationController.topViewController.view.bounds
+//                                                inView:self.navigationController.topViewController.view
+//                                              animated:YES];
+    
+    [self.documentController presentOptionsMenuFromRect:self.navigationController.topViewController.view.bounds
+                                                 inView:self.navigationController.topViewController.view
+                                               animated:YES];
+}
+
+- (void)quickLookMenuClick:(id)sender {
+    
+    UIAlertController *menuAlert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
+    
+    __weak typeof(self) weakSelf = self;
+    UIAlertAction *saveAction = [UIAlertAction actionWithTitle:@"Save Document" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+        [weakSelf saveDocument];
+    }];
+    UIAlertAction *shareAction = [UIAlertAction actionWithTitle:@"Share Document" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+        [weakSelf shareDocument];
+    }];
+    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
+        
+    }];
+    
+    [menuAlert addAction:saveAction];
+    [menuAlert addAction:shareAction];
+    [menuAlert addAction:cancelAction];
+    
+    [self presentViewController:menuAlert animated:YES completion:nil];
+}
 
 #pragma mark - GridResult
 
@@ -662,6 +719,64 @@
     //    return header;
 }
 
+#pragma mark - UIDocumentInteractionControllerDelegate
+
+- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
+    return self.navigationController.topViewController;
+}
+
+- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller {
+    return self.navigationController.topViewController.view.bounds;
+}
+
+- (nullable UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller {
+    return self.navigationController.topViewController.view;
+}
+
+// Preview presented/dismissed on document.  Use to set up any HI underneath.
+- (void)documentInteractionControllerWillBeginPreview:(UIDocumentInteractionController *)controller {
+    
+}
+
+- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller {
+    
+}
+
+// Options menu presented/dismissed on document.  Use to set up any HI underneath.
+- (void)documentInteractionControllerWillPresentOptionsMenu:(UIDocumentInteractionController *)controller {
+    
+}
+
+- (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller {
+    
+}
+
+// Open in menu presented/dismissed on document.  Use to set up any HI underneath.
+- (void)documentInteractionControllerWillPresentOpenInMenu:(UIDocumentInteractionController *)controller {
+    
+}
+- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller {
+    
+}
+
+// Synchronous.  May be called when inside preview.  Usually followed by app termination.  Can use willBegin... to set annotation.
+- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(nullable NSString *)application {
+    
+}
+
+- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(nullable NSString *)application {
+    
+}
+
+// Used to handle additional menu items that can be performed on the item specified by URL.  Currently only supports the "copy:", "print:" and "saveToCameraRoll:" actions.
+- (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller canPerformAction:(nullable SEL)action {
+    return YES;
+}
+
+- (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller performAction:(nullable SEL)action {
+    return YES;
+}
+
 @end
 
 #pragma clang diagnostic pop