美文网首页IOS知识整理
ios UIDocumentInterationControl

ios UIDocumentInterationControl

作者: fulen | 来源:发表于2016-12-16 11:30 被阅读228次
IMG_0001.JPG

文章主要写来日后自己回顾,不详细

1、在项目的info.plist里面添加相应的键值对,将事先下载好的pdf拖到项目中,如图1.0和1.1,注意不是TARGETS下的info里面添加,在那里面添加会导致程序crash

1.0 下载的pdf拖到项目中 图1.1 添加所需键值对

2、进入程序,遵循代理<UIDocumentInteractionControllerDelegate>

设置全局变量或者属性

    UIDocumentInteractionController *documentIntertactionController;
}

3、创建两个button,一个用来打开其他app,一个用来预览文件,如图3.1

图3.1

4、打开的方法实现

// 打开
- (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、点击打开,如图5.1,这是模拟器的效果,在手机上还有其他软件,比如微信qq等等

图5.1

6、预览对应的代码

// 预览
- (void)previewUIDocument{
    NSURL *pathUrl = [[NSBundle mainBundle] URLForResource:@"Steve" withExtension:@".pdf"];
    documentIntertactionController = [UIDocumentInteractionController interactionControllerWithURL:pathUrl];
    documentIntertactionController.delegate = self;
    [self presentUIDocument];
}

- (void)presentUIDocument{
    [documentIntertactionController presentPreviewAnimated:YES];
}
// 预览的时候需要加上系统的代理方法
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
    return self;
}

7、点击预览,如图7.1

图7.1 预览

8、以上为加载本地存在的文件,下面介绍根据网址打开和预览

9、open网上链接,代码如下

- (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、 预览,代码如下

- (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适配的话 打开第一个 */
    if ([[UIDevice currentDevice] systemVersion].floatValue >= 9.0)
    {
         return [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]];
    }
    else
    {
        return [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    }
}


效果如图7.1,这里是最简单的功能实现,没有考虑性能

简单的使用就暂时说到这里,功能远远不止这些,而且预览的时候要考虑手机是否支持等等,其他的注意事项以后再说

相关文章

网友评论

    本文标题:ios UIDocumentInterationControl

    本文链接:https://www.haomeiwen.com/subject/cjdnmttx.html