iOS 利用iCloud上传文档 pdf

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/walkerwqp/article/details/88713276

1.#import <QuickLook/QuickLook.h>

2. 类方法

@interface ZZDocument : UIDocument
@property (nonatomic, strong) NSData *data;
@end
@implementation ZZDocument

- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError * _Nullable __autoreleasing *)outError {
    self.data = contents;
    return YES;
}


@end

typedef void(^downloadBlock)(id obj);

@interface iCloudManager : NSObject

+ (BOOL)iCloudEnable;

+ (void)downloadWithDocumentURL:(NSURL*)url callBack:(downloadBlock)block;

@end
@implementation iCloudManager

+ (BOOL)iCloudEnable {
    
    NSFileManager *manager = [NSFileManager defaultManager];
    
    NSURL *url = [manager URLForUbiquityContainerIdentifier:nil];
    
    if (url != nil) {
        return YES;
    }
    
    NSLog(@"iCloud 不可用");
    return NO;
}
+ (void)downloadWithDocumentURL:(NSURL*)url callBack:(downloadBlock)block {
    
    ZZDocument *iCloudDoc = [[ZZDocument alloc]initWithFileURL:url];
    
    [iCloudDoc openWithCompletionHandler:^(BOOL success) {
        if (success) {
            
            [iCloudDoc closeWithCompletionHandler:^(BOOL success) {
                NSLog(@"关闭成功");
            }];
            
            if (block) {
                block(iCloudDoc.data);
            }
            
        }
    }];
}

@end

3.遵守代理 UIDocumentPickerDelegate,QLPreviewControllerDataSource

4.

//上传文档内容
@property (nonatomic, strong) UILabel         *uploadWordLabel;
@property (nonatomic, strong) UIView          *wordView;

@property (nonatomic, strong) UIScrollView    *publishJobScrollView;

@property (nonatomic, strong) QLPreviewController *previewController;
@property (nonatomic, copy)   NSString *filePath;
@property (nonatomic, strong) NSURL    *fileURL;

@property (nonatomic, strong) UIImageView    *wordImgView;
@property (nonatomic, strong) NSString       *urlStr;

@property (nonatomic, assign) NSInteger     dataInteger;

5.上传文档

- (void)presentDocumentPicker {
    
    NSArray *documentTypes = @[@"public.content", @"public.text", @"public.source-code", @"public.audiovisual-content", @"com.adobe.pdf", @"com.apple.keynote.key", @"com.microsoft.word.doc", @"com.microsoft.excel.xls"];
    
    UIDocumentPickerViewController *documentPickerViewController = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:documentTypes
                                                                                                                          inMode:UIDocumentPickerModeOpen];
    documentPickerViewController.delegate = self;
    [self presentViewController:documentPickerViewController animated:YES completion:nil];
}
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
    
    NSArray *array = [[url absoluteString] componentsSeparatedByString:@"/"];
    NSString *fileName = [array lastObject];
    fileName = [fileName stringByRemovingPercentEncoding];
    self.fileURL = url;
    if ([iCloudManager iCloudEnable]) {
        [iCloudManager downloadWithDocumentURL:url callBack:^(id obj) {
            NSData *data = obj;
            
            //写入沙盒Documents
            self.filePath = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@",fileName]];
            [data writeToFile:self.filePath atomically:YES];
            [self.previewController reloadData];
            
            NSLog(@"%@",self.filePath);
            
            NSArray *arr = [self.filePath componentsSeparatedByString:@"."];
            NSString *smallString = [[arr lastObject] lowercaseString];
            NSLog(@"%@",smallString);
            
            if ([smallString isEqualToString:@"txt"]) {
                
                [WProgressHUD showErrorAnimatedText:@"不支持txt文件上传"];
                return;
            } else {
                if ([smallString isEqualToString:@"doc"] || [smallString isEqualToString:@"docx"]) {
                    self.wordImgView.image = [UIImage imageNamed:@"WORD"];
                } else if ([smallString isEqualToString:@"xls"] || [smallString isEqualToString:@"xlsx"]) {
                    self.wordImgView.image = [UIImage imageNamed:@"excl"];
                } else if ([smallString isEqualToString:@"pdf"]) {
                    self.wordImgView.image = [UIImage imageNamed:@"ppt"];
                }
                
                NSDictionary * params = @{@"key":[UserManager key],@"upload_type":@"annex"};
                [WProgressHUD showHUDShowText:@"加载中..."];
                [[HttpRequestManager sharedSingleton].sessionManger POST:WENJIANSHANGCHUANJIEKOU parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
                    [formData appendPartWithFileData:data name:[NSString stringWithFormat:@"file[0]"] fileName:self.filePath mimeType:smallString];
                } progress:^(NSProgress * _Nonnull uploadProgress) {
                } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                    [WProgressHUD hideAllHUDAnimated:YES];
                    if ([[responseObject objectForKey:@"status"] integerValue] == 200) {
                        NSDictionary *dic = [responseObject objectForKey:@"data"];
                        NSMutableArray *arr1 = [dic objectForKey:@"url"];
                        self.urlStr = [arr1 lastObject];
                        NSLog(@"%@",self.urlStr);
                        
                    } else {
                        if ([[responseObject objectForKey:@"status"] integerValue] == 401 || [[responseObject objectForKey:@"status"] integerValue] == 402) {
                            [UserManager logoOut];
                            [WProgressHUD showErrorAnimatedText:[responseObject objectForKey:@"msg"]];
                        } else {
                            [WProgressHUD showErrorAnimatedText:[responseObject objectForKey:@"msg"]];
                        }
                    }
                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                    NSLog(@"%@", error);
                    [WProgressHUD hideAllHUDAnimated:YES];
                }];
            }
            
        }];
    }
}

6.最重要的 首先证书要允许 使用iCloud。在这 capabilities 打开iCloud 前四个都打开 就可以喽

如需改进 望私信

猜你喜欢

转载自blog.csdn.net/walkerwqp/article/details/88713276
今日推荐