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 前四个都打开 就可以喽
如需改进 望私信
iOS 利用iCloud上传文档 pdf
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1. POST参数上传: 一种方式就是将图片转成base64字符串放到请求参数中上传 2.formData上传:这...
- import java.io.ByteArrayOutputStream; import java.io.File...
- Unity制作PDF文档 一、效果图 二、软件环境 Unity5.6.0链接:https://pan.baidu....
- 前言 在我们的开发中,有些像电子书类型的app的开发会涉及到pdf文档的加载与展示。由于笔者项目中正好涉及到这块,...