(浏览文档)UIDocumentInteractionController打开自己的第三方应用、使用QLPreviewController浏览文档
2015-04-08 16:50
537 查看
UIDocumentInteractionController:
在.h文件中添加<<span style="line-height: 21px;">UIDocumentInteractionControllerDelegate>
然后在.m文件中,新建一个方法,用来响应你点击按钮弹出时弹出的popview,其中会罗列可打开当前文件的其他应用。
方法如下:
-(void)openDocumentIn{
documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:unEncodingURL]];
documentController.delegate = self;
documentController.UTI = @"com.microsoft.word.doc";
[documentController presentOpenInMenuFromRect:CGRectMake(760, 20, 100, 100)
inView:self.view animated:YES];
}
其中的 documentController.UTI 表示那些类型的文件支持第三方软件打开,此链接里面有各种类型文件对应的数https://developer.apple.com/library/mac/#documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html 可用根据unEncodingURl中的文件后缀名去动态设置这个值rect 根据自己的需求写
然后delegate里面的方法
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)interactionController
{
return self;
}
QLPreviewController:
iOS SDK包括了QLPreviewControllerAPI,组件允许用户浏览许多不同的文件类型,如XLS文件,Word文档文件,PDF文件。约翰已创建了一个示例应用程序演示使用QLPreviewController。在示范中,您可以查看几个不同的文件类型,甚至打印(使用无线打印机。)随着一个简短的教程,我们解释实施QLPreviewController的基础步骤,你可以找到约翰的例子::::对于过去的几个月中,我一直花一些时间检查出IOS快看文件预览 -接下来是一个短的应用程序,我写着是为了更熟悉QLPreviewControllerAPI。对于那些不熟悉的读者可以这么看,quick
look是一个框架,它提供快速预览的一系列文件类型 -支持的文件包括iWork文档,微软Office,RTF格式,PDF格式,图像,文本文件,并以逗号分隔(CSV)文件。接下来在演示的程序中,我用了三个不同文件类型,.xls/image/ms
office /pdf
为应用程序的接口文件如下所示,注意QL数据源的引用,使用的QLPreviewController时,你必须实现此协议QLPreviewControllerDataSource。这里的唯一的实例变量是一个数组,包涵每个被预览文件的文件字符串值。UITableViewController类将用于显示预览的文件列表,通过navigation到下一个预览界面。
[cpp] view plaincopy
#import
@interface TestViewController : UITableViewController ource>
{
NSArray *arrayOfDocuments;
}
@end
在本节中,我将展示一个选择适用于设立预览代码。创建表视图和填充相同的代码可以被视为在Xcode项目,你可以从下面的链接下载(如果要学习tableview的使用,可以参考其它的指南)。
初始化代码填入文件名的数组: 这样文件名全在数组了:
[cpp] view plaincopy
-(id)init
{
if (self = [super init])
{
arrayOfDocuments = [[NSArray alloc] initWithObjects:
@"iOSDevTips.png", @"Remodel.xls", @"Core J2ME Technology.pdf", nil];
}
return self;
}
下面的方法是采用QLPreviewControllerDataSource协议时,必要的两个之一,此方法通知预览控制器,如何在预览导航列表中的呈现多少个项目是:
[cpp] view plaincopy
- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{
return [arrayOfDocuments count];
}
这种呢,算是question type的events,就是问 文件从什么地方来,NSURL
[cpp] view plaincopy
- (id )previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
// Break the path into its components (filename and extension)
NSArray *fileComponents = [[arrayOfDocuments objectAtIndex: index] componentsSeparatedByString:@"."];
// Use the filename (index 0) and the extension (index 1) to get path
NSString *path = [[NSBundle mainBundle] pathForResource:[fileComponents objectAtIndex:0] ofType:[fileComponents objectAtIndex:1]];
//这个代码就体现了灵活性,你也可以写成 ofType .pdf
return [NSURL fileURLWithPath:path];
}
项目中的其余代码是典型的iPhone/的iOS的东西,创建应用程序委托,委托的UIWindow中添加一个子视图(导航控制器),使窗口可见。我从下面的委托代码,在这里你可以得到更大的图片视图我如何设置此应用程序的视图控制器。
[cpp] view plaincopy
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
// Create and initialize the window
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Create test view controller
vc = [[TestViewController alloc] init];
// Create navigation controller
nav = [[UINavigationController alloc] initWithRootViewController:vc];
[window addSubview:[nav view]];
[window makeKeyAndVisible];
}
在选中特定行的时候,初始化QLPreviewController:
[cpp] view plaincopy
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// When user taps a row, create the preview controller
QLPreviewController *previewer = [[[QLPreviewController alloc] init] autorelease];
// Set data source
[previewer setDataSource:self];
// Which item to preview
[previewer setCurrentPreviewItemIndex:indexPath.row];
// Push new viewcontroller, previewing the document
[[self navigationController] pushViewController:previewer animated:YES];
}
值得一提的是预览控制器工作时,你有两种不同的选择。首先,你可以推到使用一个UINavigationController对象,你可以看到预览控制器的对象是我做了什么。预览我的应用程序生命在TestViewController这个对象控制器设置为导航控制器的根视图控制器。
第二种方法来显示预览控制器是模态,使用方法presentModalViewController。 //这个和我上篇fastpdfkit的讲法是一致的:
[cpp] view plaincopy
#pragma mark -
#pragma mark QLPreviewControllerDataSource
// Returns the number of items that the preview controller should preview
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController
{
return 5; //30//you can increase/decrease the this
}
// returns the item that the preview controller should preview
- (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)idx
{
return fileURL;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
以上就是QLPreviewController的一些delegate,首先是预览页面数目,其次是我需要的URL(NSURL),最后是这个view的支持rotation程度
在.h文件中添加<<span style="line-height: 21px;">UIDocumentInteractionControllerDelegate>
然后在.m文件中,新建一个方法,用来响应你点击按钮弹出时弹出的popview,其中会罗列可打开当前文件的其他应用。
方法如下:
-(void)openDocumentIn{
documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:unEncodingURL]];
documentController.delegate = self;
documentController.UTI = @"com.microsoft.word.doc";
[documentController presentOpenInMenuFromRect:CGRectMake(760, 20, 100, 100)
inView:self.view animated:YES];
}
其中的 documentController.UTI 表示那些类型的文件支持第三方软件打开,此链接里面有各种类型文件对应的数https://developer.apple.com/library/mac/#documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html 可用根据unEncodingURl中的文件后缀名去动态设置这个值rect 根据自己的需求写
然后delegate里面的方法
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)interactionController
{
return self;
}
QLPreviewController:
iOS SDK包括了QLPreviewControllerAPI,组件允许用户浏览许多不同的文件类型,如XLS文件,Word文档文件,PDF文件。约翰已创建了一个示例应用程序演示使用QLPreviewController。在示范中,您可以查看几个不同的文件类型,甚至打印(使用无线打印机。)随着一个简短的教程,我们解释实施QLPreviewController的基础步骤,你可以找到约翰的例子::::对于过去的几个月中,我一直花一些时间检查出IOS快看文件预览 -接下来是一个短的应用程序,我写着是为了更熟悉QLPreviewControllerAPI。对于那些不熟悉的读者可以这么看,quick
look是一个框架,它提供快速预览的一系列文件类型 -支持的文件包括iWork文档,微软Office,RTF格式,PDF格式,图像,文本文件,并以逗号分隔(CSV)文件。接下来在演示的程序中,我用了三个不同文件类型,.xls/image/ms
office /pdf
为应用程序的接口文件如下所示,注意QL数据源的引用,使用的QLPreviewController时,你必须实现此协议QLPreviewControllerDataSource。这里的唯一的实例变量是一个数组,包涵每个被预览文件的文件字符串值。UITableViewController类将用于显示预览的文件列表,通过navigation到下一个预览界面。
[cpp] view plaincopy
#import
@interface TestViewController : UITableViewController ource>
{
NSArray *arrayOfDocuments;
}
@end
在本节中,我将展示一个选择适用于设立预览代码。创建表视图和填充相同的代码可以被视为在Xcode项目,你可以从下面的链接下载(如果要学习tableview的使用,可以参考其它的指南)。
初始化代码填入文件名的数组: 这样文件名全在数组了:
[cpp] view plaincopy
-(id)init
{
if (self = [super init])
{
arrayOfDocuments = [[NSArray alloc] initWithObjects:
@"iOSDevTips.png", @"Remodel.xls", @"Core J2ME Technology.pdf", nil];
}
return self;
}
下面的方法是采用QLPreviewControllerDataSource协议时,必要的两个之一,此方法通知预览控制器,如何在预览导航列表中的呈现多少个项目是:
[cpp] view plaincopy
- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{
return [arrayOfDocuments count];
}
这种呢,算是question type的events,就是问 文件从什么地方来,NSURL
[cpp] view plaincopy
- (id )previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
// Break the path into its components (filename and extension)
NSArray *fileComponents = [[arrayOfDocuments objectAtIndex: index] componentsSeparatedByString:@"."];
// Use the filename (index 0) and the extension (index 1) to get path
NSString *path = [[NSBundle mainBundle] pathForResource:[fileComponents objectAtIndex:0] ofType:[fileComponents objectAtIndex:1]];
//这个代码就体现了灵活性,你也可以写成 ofType .pdf
return [NSURL fileURLWithPath:path];
}
项目中的其余代码是典型的iPhone/的iOS的东西,创建应用程序委托,委托的UIWindow中添加一个子视图(导航控制器),使窗口可见。我从下面的委托代码,在这里你可以得到更大的图片视图我如何设置此应用程序的视图控制器。
[cpp] view plaincopy
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
// Create and initialize the window
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Create test view controller
vc = [[TestViewController alloc] init];
// Create navigation controller
nav = [[UINavigationController alloc] initWithRootViewController:vc];
[window addSubview:[nav view]];
[window makeKeyAndVisible];
}
在选中特定行的时候,初始化QLPreviewController:
[cpp] view plaincopy
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// When user taps a row, create the preview controller
QLPreviewController *previewer = [[[QLPreviewController alloc] init] autorelease];
// Set data source
[previewer setDataSource:self];
// Which item to preview
[previewer setCurrentPreviewItemIndex:indexPath.row];
// Push new viewcontroller, previewing the document
[[self navigationController] pushViewController:previewer animated:YES];
}
值得一提的是预览控制器工作时,你有两种不同的选择。首先,你可以推到使用一个UINavigationController对象,你可以看到预览控制器的对象是我做了什么。预览我的应用程序生命在TestViewController这个对象控制器设置为导航控制器的根视图控制器。
第二种方法来显示预览控制器是模态,使用方法presentModalViewController。 //这个和我上篇fastpdfkit的讲法是一致的:
[cpp] view plaincopy
#pragma mark -
#pragma mark QLPreviewControllerDataSource
// Returns the number of items that the preview controller should preview
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController
{
return 5; //30//you can increase/decrease the this
}
// returns the item that the preview controller should preview
- (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)idx
{
return fileURL;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
以上就是QLPreviewController的一些delegate,首先是预览页面数目,其次是我需要的URL(NSURL),最后是这个view的支持rotation程度
相关文章推荐
- UIDocumentInteractionController 第三方应用中打开自己的文件 方法
- 使用第三方APP打开本地文档 (UIDocumentInteractionController)
- UIDocumentInteractionController 第三方应用中打开自己的文件 方法
- iOS 第三方应用中打开自己的文件(UIDocumentInteractionController)
- UIDocumentInteractionController的使用之打开和预览文档
- iOS指南系列:使用QLPreviewController浏览文档
- 【iOS功能实现】之利用UIDocumentInteractionController打开和预览文档
- UIDocumentInteractionController -iOS预览、打开文档(doc、pdf等)
- 之利用UIDocumentInteractionController打开和预览文档
- ios中通过UIDocumentInteractionControllers调用第三方应用打开本地压缩文件
- iOS UIDocumentInteractionController 设置第三方打开方式的 参数详解
- UIDocumentInteractionController 预览和打开文档
- 消息,调用,UIDocumentInteractionController,QLPreviewController
- 【iOS功能实现】之利用UIDocumentInteractionController打开和预览文档
- iOS利用UIDocumentInteractionController和Quick Look打开或预览文档
- iOS利用UIDocumentInteractionController和Quick Look打开或预览文档
- UIDocumentInterRactionController 使用时的注意事项(其它应用打开)
- iOS 利用UIDocumentInteractionController打开和预览文档
- iOS 调起第三方程序打开文件 ( UIDocumentInteractionController )
- iOS UIDocumentInteractionController 第三方打开详解