文章主要寫來日后自己回顧毛秘,不詳細(xì)
1颅和、在項(xiàng)目的info.plist里面添加相應(yīng)的鍵值對,將事先下載好的pdf拖到項(xiàng)目中茎辐,如圖1.0和1.1宪郊,注意不是TARGETS下的info里面添加,在那里面添加會導(dǎo)致程序crash
2拖陆、進(jìn)入程序弛槐,遵循代理<UIDocumentInteractionControllerDelegate>
設(shè)置全局變量或者屬性
UIDocumentInteractionController *documentIntertactionController;
}
3、創(chuàng)建兩個(gè)button依啰,一個(gè)用來打開其他app乎串,一個(gè)用來預(yù)覽文件,如圖3.1
4速警、打開的方法實(shí)現(xiàn)
// 打開
- (void)openUIDocument{
NSURL *pathUrl = [[NSBundle mainBundle] URLForResource:@"Steve" withExtension:@".pdf"];
documentIntertactionController = [UIDocumentInteractionController interactionControllerWithURL:pathUrl];
documentIntertactionController.delegate = self;
[self presentOptionsMenu];
}
- (void)presentOptionsMenu{
[documentIntertactionController presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
}
以下是上面方法的官方解釋
application to pass to receiver (must be a plist object). default is nil.
// This is the default method you should call to give your users the option to quick look, open, or copy the document.
// Presents a menu allowing the user to Quick Look, open, or copy the item specified by URL.
// This automatically determines the correct application or applications that can open the item at URL.
// Returns NO if the options menu contained no options and was not opened.
// Note that you must implement the delegate method documentInteractionControllerViewControllerForPreview: to get the Quick Look menu item.
5叹誉、點(diǎn)擊打開,如圖5.1闷旧,這是模擬器的效果桂对,在手機(jī)上還有其他軟件,比如微信qq等等
6鸠匀、預(yù)覽對應(yīng)的代碼
// 預(yù)覽
- (void)previewUIDocument{
NSURL *pathUrl = [[NSBundle mainBundle] URLForResource:@"Steve" withExtension:@".pdf"];
documentIntertactionController = [UIDocumentInteractionController interactionControllerWithURL:pathUrl];
documentIntertactionController.delegate = self;
[self presentUIDocument];
}
- (void)presentUIDocument{
[documentIntertactionController presentPreviewAnimated:YES];
}
// 預(yù)覽的時(shí)候需要加上系統(tǒng)的代理方法
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
return self;
}
7蕉斜、點(diǎn)擊預(yù)覽,如圖7.1
8缀棍、以上為加載本地存在的文件宅此,下面介紹根據(jù)網(wǎng)址打開和預(yù)覽
9、open網(wǎng)上鏈接爬范,代碼如下
- (void)openUIDocument{
[self openDocument];
}
- (void)openDocument{
[self pathUrl]; // 該方法在最下面
NSData *data = [NSData dataWithContentsOfURL:_pathUrl];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"Python核心編程.pdf"];
BOOL isWriteSuccess = [data writeToFile:filePath atomically:YES];
if(isWriteSuccess) {//success
NSURL *url = [NSURL fileURLWithPath:filePath];
[self previewPdfWithUrl:url];
[self presentOptionsMenu];
}
else{
NSLog(@"file not written");
}
}
- (void)previewPdfWithUrl:(NSURL*)url{
documentIntertactionController = [UIDocumentInteractionController interactionControllerWithURL:url];
documentIntertactionController.delegate = self;
}
- (void)presentOptionsMenu{
[documentIntertactionController presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
}
效果如圖5.1
10父腕、 預(yù)覽,代碼如下
- (void)previewUIDocument{
[self previewDocument];
}
- (void)previewDocument{
[self pathUrl];
NSData *data = [NSData dataWithContentsOfURL:_pathUrl];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"Python核心編程.pdf"];
BOOL isWriteSuccess = [data writeToFile:filePath atomically:YES];
if(isWriteSuccess) {//success
NSLog(@"%@",filePath);
NSURL *url = [NSURL fileURLWithPath:filePath];
[self previewPdfWithUrl:url];
[self presentUIDocument];
}
else{
NSLog(@"file not written");
}
}
- (void)previewPdfWithUrl:(NSURL*)url{
documentIntertactionController = [UIDocumentInteractionController interactionControllerWithURL:url];
documentIntertactionController.delegate = self;
}
- (NSURL *)pathUrl{
if (_pathUrl== nil) {
NSString *urlString = @"http://platform-test.lejuwanjia.com/api/v1/web/content/sale.order.picture/158/picture/Python核心編程.pdf";
NSString *urlStr = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
// _pathUrl = [NSURL URLWithString:urlStr];
_pathUrl = [NSURL URLWithString:urlStr];
NSLog(@"pathUrl:%@",_pathUrl);
}
return _pathUrl;
}
- (void)presentUIDocument{
[documentIntertactionController presentPreviewAnimated:YES];
}
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
return self;
}
#pragma mark - url 中文格式化
+ (NSString *)strUTF8Encoding:(NSString *)str
{
/*! ios9適配的話 打開第一個(gè) */
if ([[UIDevice currentDevice] systemVersion].floatValue >= 9.0)
{
return [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]];
}
else
{
return [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
}
11 網(wǎng)上文件的預(yù)覽需要先緩存到本地青瀑,這時(shí)候可以建一個(gè)緩存目錄璧亮,以便于以后刪除緩存
11.1 如果是請求網(wǎng)絡(luò)然后返回了一個(gè)NSData對象萧诫,這時(shí)候可以先保存到某個(gè)目錄下面,下一個(gè)預(yù)覽的時(shí)候判斷此目錄下是否有文件來減少網(wǎng)絡(luò)請求
- (void)previewWithModel:(OSPAttachFileModel *)model {
// 此model為點(diǎn)擊某行cell時(shí)對應(yīng)的model
NSString *filePath = [self getFilePathWithModel:model];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir = NO;
BOOL existed = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
if (existed) {
NSURL *url = [NSURL fileURLWithPath:filePath];
[self previewPdfWithUrl:url];
}else {
[OSPToast show];
[[OSPServiceCallsService sharedInstance] queryDocumentWithDocumentId:model.documentId equipmentId:model.equipmentId completion:^(NSData *data,NSHTTPURLResponse * response, NSError * error) {
[OSPToast dismiss];
if (data.length) {
[self previewDocumentWithFilePath:filePath data:data];
}
}];
}
}
- (NSString *)getFilePathWithModel:(OSPAttachFileModel *)model {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataFilePath = [documentsDirectory stringByAppendingPathComponent:OSPAttchFile];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir = NO;
BOOL existed = [fileManager fileExistsAtPath:dataFilePath isDirectory:&isDir];
if (!(isDir && existed)) {
[fileManager createDirectoryAtPath:dataFilePath withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString *documentIdStr = [NSString stringWithFormat:@"%zd", model.documentId];
NSString *filePath = [NSString stringWithFormat:@"%@/%@-%@", dataFilePath,documentIdStr,model.documentName];
return filePath;
}
- (void)previewPdfWithUrl:(NSURL*)url {
_documentInteraction = [UIDocumentInteractionController interactionControllerWithURL:url];
_documentInteraction.delegate = self;
[_documentInteraction presentPreviewAnimated:YES];
}
- (void)previewDocumentWithFilePath:(NSString *)filePath data:(NSData *)data {
BOOL isWriteSuccess = [data writeToFile:filePath atomically:YES];
if(isWriteSuccess) {//success
NSURL *url = [NSURL fileURLWithPath:filePath];
[self previewPdfWithUrl:url];
}else {
DLog(@"file not written");
}
}
點(diǎn)擊其中一個(gè)文件枝嘶,先根據(jù)model中的文件名以及id找到對應(yīng)路徑下是否存在文件帘饶,如果存在直接preview,不存在的話根據(jù)model生成的路徑及文件名將下載的NSData write to file 群扶,成功之后preview
根據(jù)model生成的文件及刻,OSPAttachFile為創(chuàng)建的文件夾,易于管理
根據(jù)文件filepath竞阐,直接preview