大家在使用APP的时候一定会遇见过这样一个情况,就是在注册的时候.需要用户同意注册协议.今天就分享给大家这个简单的读取文档的功能.读取文档有很多方式,我们用的是系统提供的QuickLook进行文档读取,上代码.
第一步
#import <QuickLook/QuickLook.h>
@interface RegisterAgreementViewController ()<QLPreviewControllerDataSource, QLPreviewControllerDelegate>
@property(nonatomic, strong)NSString *myFilePath;
@property(nonatomic, strong)QLPreviewController *previewController;
第二步,当然协议是你文档在本地的路径
_myFilePath = [[NSBundle mainBundle] pathForResource:@"XXX注册协议" ofType:@"txt"];
if (![_myFilePath isEqualToString:@""])
{
_previewController = [[QLPreviewController alloc] init];
_previewController.dataSource = self;
_previewController.delegate = self;
_previewController.view.frame = self.view.frame;
[self addChildViewController:self.previewController];
[self.view addSubview:self.previewController.view];
}
else
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"文件丢失" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
}
第三部,实现两个代理方法
-(NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
return 1;
}
-(id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
if (![_myFilePath isEqualToString:@""])
{
return [NSURL fileURLWithPath:_myFilePath];
}
else
{
return nil;
}
}
简单吧,就可以进行文档读取了.
网友评论