//
//  HomeTestViewController.m
//  smartRhino
//
//  Created by taidi on 2019/12/9.
//  Copyright © 2019 tederen. All rights reserved.
//

#import "HomeTestViewController.h"

@interface HomeTestViewController ()<UIDocumentPickerDelegate>

@end

@implementation HomeTestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addButton:@"预览" withX:50 Withy:50 WithWidth:150 withHeight:50];
    [self addButton:@"三方列表" withX:50 Withy:150 WithWidth:150 withHeight:50];
    [self addButton:@"导入" withX:50 Withy:250 WithWidth:150 withHeight:50];
    [self addButton:@"导出" withX:50 Withy:350 WithWidth:150 withHeight:50];
}
- (void)addButton:(NSString*)string withX:(CGFloat)x Withy:(CGFloat)y WithWidth:(CGFloat)w withHeight:(CGFloat)h{
    UIButton *addButton = [[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)];
    [addButton setTitle:string forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(importFile) forControlEvents:UIControlEventTouchUpInside];
    [addButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.view addSubview:addButton];
}

/// 道出
-(void)importFile2{
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"App" withExtension:@"pdf"];
    UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithURL:url
                                                                                                  inMode:UIDocumentPickerModeExportToService];
    documentPicker.delegate = self;
    documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentViewController:documentPicker animated:YES completion:nil];
}
-(void)importFile{
    UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc]
                                                             initWithDocumentTypes:@[@"public.image",@"com.adobe.pdf"] inMode:UIDocumentPickerModeImport];
    documentPicker.delegate = self;
    documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentViewController:documentPicker animated:YES completion:nil];
}
#pragma mark - UIDocumentPickerDelegate
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
    if (controller.documentPickerMode == UIDocumentPickerModeImport) {
        NSString *alertMessage = [NSString stringWithFormat:@"Successfully imported %@", [[urls lastObject] lastPathComponent]];
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertController *alertController = [UIAlertController
                                                  alertControllerWithTitle:@"Import"
                                                  message:alertMessage
                                                  preferredStyle:UIAlertControllerStyleAlert];
            [alertController addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:nil]];
            [self presentViewController:alertController animated:YES completion:nil];
        });
    }
}
@end