1. How to open items in other iOS Apps

1.  How to open items in other iOS Apps


iOS提供了使用其他app预览文件的支持,这就是Document Interaction Controller。此外,iOS也支持文件关联,允许其他程序调用你的app打开某种文件。



-、创建实例

DocumentInteraction Controller使用静态方法interactionControllerWithURL创建实例,这个方法使用一个NSURL作为参数。
代码:
NSURL *url=[NSURL fileURLWithPath:path];
controller = [UIDocumentInteractionController  interactionControllerWithURL:url];
二、显示预览窗口
Document Interaction Controller对象使用presentPreviewAnimated方法弹出一个全屏的文档预览窗口。
代码:
 BOOL b=[controller presentPreviewAnimated:YES];
 
三、显示菜单
 
如果你不想直接弹出预览窗口,你可以显示一个选项菜单给用户,由用户选择相应的操作。显示菜单可以使用下列方法:
 
–presentOptionsMenuFromRect:inView:animated:
–presentOptionsMenuFromBarButtonItem:animated:
–presentOpenInMenuFromRect:inView:animated:
–presentOpenInMenuFromBarButtonItem:animated:
 
这些方法都是类似的,只是显示位置有区别而已。以下代码演示其中一个方法的使用。
 
代码:
 
CGRect navRect = self.navigationController.navigationBar.frame;
 
navRect.size = CGSizeMake(1500.0f, 40.0f);
 
[controller presentOptionsMenuFromRect:navRect inView:self.view  animated:YES];
 
四、使用委托
 
如果你显示一个Document Interaction Controller ,则必需要为delegate属性用指定一个委托。让委托告诉DocumentInteraction Controller如何显示。
 
代码:
 
controller.delegate =self;
 
委托对象需要实现一系列委托方法,最常见的包括:
 
–documentInteractionControllerViewControllerForPreview:
–documentInteractionControllerViewForPreview:
–documentInteractionControllerRectForPreview:
 
这3个方法在用户点击“快速查看”菜单时依次调用。
 
代码:
 
- (UIViewController*)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController*)controller
{return self;
}
- (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller{
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller
{
return self.view.frame;
}
// 点击预览窗口的“Done”(完成)按钮时调用
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController*)_controller
{
    [_controller autorelease];
}
 +=+ +=+ +=+ +=+ +=+ +=+ +=+ +=+ +=+ +=+ +=+ +=+ +=+ +=+ +=+ +=+

其实很简单 

1.  .h文件   

    <UIDocumentInteractionControllerDelegate>

UIDocumentInteractionController *documentController;

2. .m文件

- (void)openInotherApp{

    NSString *documentPath = [[NSBundle mainBundle] pathForResource:@"xcode" ofType:@"pdf"];

    NSURL *documentURL = [NSURL fileURLWithPath:documentPath isDirectory:NO];

    documentController = [[UIDocumentInteractionController alloc] init];

    documentController.delegate = self;

    documentController.URL = documentURL;

    //documentController.UTI = @"com.microsoft.word.doc";//@"public.png"; //预览用

    [documentController presentOpenInMenuFromRect:CGRectMake(760, 20, 100, 100) inView:self.view animated:YES];

}


- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {

    return  self;

}


- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application {

    NSLog(@"Starting to send this puppy to %@", application);

}

- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application {

    NSLog(@"We're done sending the document.");

}




 

参考:http://pinkstone.co.uk/how-to-open-items-in-other-ios-apps/



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值