app 之间发送文件 ios

本文转载至 http://www.51094.com/?p=212

第一种: 发送一个正常的  pdf 文件,只要是能读取pdf 的都能得到响应

-(IBAction)openDocumentIn:(id)sender {

    NSString * filePath = [[NSBundle mainBundlepathForResource:@"ASIHttpRequest" ofType:@"pdf"];

    UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];

    documentController.delegate = self;

    [documentController retain];

    documentController.UTI = @"com.ddsfsd.TestForDocument.pdf";//com.ddsfsd.TestForDocument.pdf 可以为随意字符 目前没发现用处

    

    //一下三选一

    //    [documentController presentOptionsMenuFromRect:CGRectZero inView:self.view  animated:YES];

    //    [documentController presentPreviewAnimated:YES];

    [documentController presentOptionsMenuFromBarButtonItem:barBtnItem  animated:YES];

    

}

- (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];

}

接收方:设置

<key>CFBundleDocumentTypes</key>

<array>

<dict>

<key>CFBundleTypeName</key>文档类型名称

<string>pdf</string>

<key>LSHandlerRank</key> //是拥有此类型文档,还是仅用于打开

<string>Default</string>

</dict>

</array>

一般而言一个文档类型和一个文件类型对应,当然也可以是多个文件类型例如。doc/。docxword文档在两个不同版本下的文件后缀。这样你可以把这两个文件类型组合在一个文档类型中

A uniform type identifier (UTI) is a string that identifies a class of entities with a type. UTIs are typically used to identify the format for files or in-memory data types and to identify the hierarchical layout of directories, volumes or packages

这里有个基本的对照表,文件后缀和UTI的串

https://developer.apple.com/library/mac/#documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html

* Item0类型为Dictionary

    * CFBundleTypeName:文档类型名

    * LSHandleRank:应用的所有者(以上声明的这种文件类型的创建者),有Alternate(这种文件类型的另外一个浏览者)、NoneDefault

    * CFBundleTypeRole:应用通过什么方式处理这种文件:EditorViewerShellNone

    * LSItemContentTpyes:Array类型,它包含表示这种文件类型的UTI数组

通过以上设置就可以向IOS注册你的应用,以便可以处理PDF文档。

    

    当一个PDF文档传到应用中后,应用就会自动调用方法:application:openURL:sourceApplication:annotation: 该方法必须在application delegate中实现。

 第二种:发送一个自定义后缀名的文件,对其他应用隐藏、 假定后缀为 pdf1 文件, 则发送的 uti 需要对应

-(IBAction)openDocumentIntwo:(id)sender {

    NSString * filePath = [[NSBundle mainBundlepathForResource:@"ASIHttpRequest" ofType:@"pdf1"];

    UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];

    documentController.delegate = self;

    [documentController retain];

    documentController.UTI = @"com.ddsfsd.TestForDocument.pdf1";//com.ddsfsd.TestForDocument.pdf 可以为随意字符 目前没发现用处

转载请标明出处,黄志勇的个人博客!

    //一下三选一

    //    [documentController presentOptionsMenuFromRect:CGRectZero inView:self.view  animated:YES];

    //    [documentController presentPreviewAnimated:YES];

    [documentController presentOptionsMenuFromBarButtonItem:barBtnItem  animated:YES];

    

}

- (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];

}

接收方:设置

图片

<key>UTExportedTypeDeclarations</key>

<array>

<dict>

<key>UTTypeIdentifier</key>

<string>com.ddsfsd.TestForDocument.pdf1</string>//应用的唯一标识 com+bundle Identifier,

<key>UTTypeTagSpecification</key>

<dict>

<key>public.filename-extension</key>//文件扩展名

<string>pdf1</string>                                      //使用pdf1

</dict>

</dict>

</array>

参考:

http://developer.apple.com/library/IOS/#documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref/doc/uid/TP40009259-SW1

http://stackoverflow.com/questions/4186401/how-do-i-register-a-custom-filetype-in-IOS

<key>CFBundleDocumentTypes</key>

<array>

<dict>

<key>CFBundleTypeName</key>文档类型名称

<string>pdf</string>

<key>LSHandlerRank</key> //是拥有此类型文档,还是仅用于打开

<string>Default</string>

</dict>

</array>

完成以上配置,则再呼叫其他程序时,只能找到自己定义的应用,及实现自己的应用间相互传递文件,而屏蔽掉其他应用

 

 
原文地址:https://www.cnblogs.com/Camier-myNiuer/p/3640821.html