专业做网站 台州玉环,哈尔滨百度推广排名优化,淄博网站建设至信网络,整合营销传播的方法包括iOS中的沙盒可以让平台更加的安全#xff0c;这也是沙盒给用户带来的最主要好处。不过由于沙盒的严格限制#xff0c;导致程序之间共享数据比较麻烦。一般在程序间共享文档可以通过UIDocumentInteractionController类实现通讯。它支持在你的app中用其他app预览和显示文档。同… iOS中的沙盒可以让平台更加的安全这也是沙盒给用户带来的最主要好处。不过由于沙盒的严格限制导致程序之间共享数据比较麻烦。一般在程序间共享文档可以通过UIDocumentInteractionController类实现通讯。它支持在你的app中用其他app预览和显示文档。同时也支持文件关联允许其他app通过你的程序打开文件。这些技术包括了UIKit中提供的UIDocumentInteractionController类UIDocumentInteractionController Class Reference)以及Quick Look框架Quick Look Framework Reference)。 本文将就如何在应用之间进行文件共享进行基本探究。还请大牛勿喷。 苹果官方文档 效果图 预览文档和呈现选项菜单 如果你的app需要打开它不支持的文件(PDF文件、图像文件等等)或者需要将app的文件传输给另外一个允许接收此类型文件的app时。可以使用文件交互控制器(UIDocumentInteractionController类的实例)为用户提供可接收程序来处理文件说的简单点就是通过Quick Look框架判断文档是否能被另一个app打开和预览。 UIDocumentInteractionController在iOS3.2中就已经存在了使用起来非常灵活功能也比较强大。它除了支持同设备上app之间的文档共享外还可以实现文档的预览、打印、发邮件以及复制。 要使用一个文件交互控制器(UIDocumentInteractionController类的实例)需要以下步骤
为每个你想打开的文件创建一个UIDocumentInteractionController类的实例实现UIDocumentInteractionControllerDelegate代理显示预览窗口/显示菜单。 一、创建实例 DocumentInteraction Controller使用静态方法interactionControllerWithURL创建实例这个方法使用一个NSURL作为参数。
//创建实例
NSURL *filePath [NSURL fileURLWithPath:path];UIDocumentInteractionController *documentController [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];二、显示预览窗口 Document Interaction Controller对象使用presentPreviewAnimated方法弹出一个全屏的文档预览窗口。
BOOL b [documentController presentPreviewAnimated:YES];三、显示菜单 如果你不想在本应用里面打开文件那么可以通过第三方应用打开预览文件。通过OptionsMenu(选项菜单)显示能够接收该类型文件的应用由用户选择相应的操作。 显示菜单可以使用下列方法
- presentOptionsMenuFromRect:inView:animated:
- presentOptionsMenuFromBarButtonItem:animated:
- presentOpenInMenuFromRect:inView:animated:
- presentOpenInMenuFromBarButtonItem:animated:这些方法都是类似的只是显示位置有区别而已。以下代码演示其中一个方法的使用。
CGRect navRect self.navigationController.navigationBar.frame;
navRect.size CGSizeMake(1500.0f, 40.0f);
[documentController presentOptionsMenuFromRect:navRectinView:self.viewanimated:YES];四、使用委托 如果你显示一个Document Interaction Controller 则必需要为delegate属性用指定一个委托。让委托告诉DocumentInteraction Controller如何显示。
documentController.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 {
}功能一分享文件
- (void)shareFile {NSString *filePath [[NSBundle mainBundle] pathForResource:皮卡丘ofType:jpeg];//创建实例UIDocumentInteractionController *documentController [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];//设置代理documentController.delegate self;BOOL canOpen [documentController presentOpenInMenuFromRect:CGRectZeroinView:self.viewanimated:YES];if (!canOpen) {NSLog(沒有程序可以打開要分享的文件);}
}功能二预览文件(注册应用程序支持的文件类型)
- (void)preview {if (!_path) {return;}NSURL *fileURL [NSURL URLWithString:[NSString stringWithFormat:%%, [self getURL], [_path lastPathComponent]]];//创建实例UIDocumentInteractionController *documentController [UIDocumentInteractionControllerinteractionControllerWithURL:fileURL];//设置代理documentController.delegate self;[documentController presentPreviewAnimated:YES];
}配置Info.plist文件 如果你的程序能够打开某种文件你可以向系统进行注册。方便其他程序通过 iOS 的document interaction技术提供给用户一个选择从而调用你的程序处理这些文件。 这需要在程序的Info.plist文件中添加CFBundleDocumentTypes键(查看CoreFoundation Keys)。 系统将该键中包含的内容进行登记这样其他程序就可以通过document interaction controller访问到这些信息。 CFBundleDocumentTypes键是一个dictionary数组每个dictionary表示了一个指定的文档类型。一个文档类型通常与某种文件类型是一一对应的。 但是如果你的程序对多个文件类型采用同样的处理方式你也可以把这些类型都分成一个组统一视作一个文档类型。**例如你的程序中使用到的本地文档类型有一个是旧格式的还有一个新格式似乎是影射微软office文档则你可以将二者分成一组都放到同一个文档类型下。这样旧格式和新格式的文件都将显示为同一个文档类型并以同样的方式打开。** CFBundleDocumentTypes数组中的每个 dictionary 可能包含以下键 CFBundleTypeName 指定文档类型名称。 CFBundleTypeIconFiles 是一个数组包含多个图片文件名用于作为该文档的图标。 LSItemContentTypes 是一个数组包含多个UTI【Uniform Type Identifiers】类型的字符串。UTI类型是本文档类型组所包含的文件类型。 LSHandlerRank 表示应用程序是“拥有”还是仅仅是“打开”这种类型而已。 下表列出了Info.plist中的一个CFBundleTypeName官方示例。 自定义文件格式的文档类型 dict
keyCFBundleTypeName/key
stringMy File Format/string
keyCFBundleTypeIconFiles/keyarraystringMySmallIcon.png/stringstringMyLargeIcon.png/string/array
keyLSItemContentTypes/keyarraystringcom.example.myformat/string/array
keyLSHandlerRank/key
stringOwner/string
/dict自己程序配置文件 keyCFBundleDocumentTypes/key
arraydictkeyCFBundleTypeName/keystringcom.myapp.common-data/stringkeyLSItemContentTypes/keyarraystringcom.microsoft.powerpoint.ppt/stringstringpublic.item/stringstringcom.microsoft.word.doc/stringstringcom.adobe.pdf/stringstringcom.microsoft.excel.xls/stringstringpublic.image/stringstringpublic.content/stringstringpublic.composite-content/stringstringpublic.archive/stringstringpublic.audio/stringstringpublic.movie/stringstringpublic.text/stringstringpublic.data/string/array/dict
/array打开支持的文件类型 你可以在应用程序委托的application:didFinishLaunchingWithOptions:方法中获得该文件的信息。如果你的程序要处理某些自定义的文件类型你必须实现这个委托方法而不是applicationDidFinishLaunching: 方法) 并用这个方法启动应用程序。 application:didFinishLaunchingWithOptions:方法的option参数包含了要打开的文件的相关信息。尤其需要在程序中关心下列键 UIApplicationLaunchOptionsURLKey 包含了该文件的NSURL。 UIApplicationLaunchOptionsSourceApplicationKey 包含了发送请求的应用程序的 Bundle ID。 UIApplicationLaunchOptionsAnnotationKey 包含了源程序向目标程序传递的与该文件相关的属性列表对象。 如果UIApplicationLaunchOptionsURLKey键存在你的程序应当立即用该 URL 打开该文件并将内容呈现给用户。其他键可用于收集与打开的文件相关的参数和信息。 如果你的应用程序处于活跃状态此时application:didFinishLaunchingWithOptions:方法是不会被调用的。需要实现application:openURL:options:方法 【以下是本人的写法】
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {_vc [[ViewController alloc] init];UINavigationController *nav [[UINavigationController alloc] initWithRootViewController:_vc];self.window [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];self.window.backgroundColor [UIColor whiteColor];self.window.rootViewController nav;[self.window makeKeyAndVisible];if (launchOptions) {NSString *str [NSString stringWithFormat:\n发送请求的应用程序的 Bundle ID%\n\n文件的NSURL%\n\n文件相关的属性列表对象%,launchOptions[UIApplicationLaunchOptionsSourceApplicationKey],launchOptions[UIApplicationLaunchOptionsURLKey],launchOptions[UIApplicationLaunchOptionsSourceApplicationKey]];[[[UIAlertView alloc] initWithTitle:message:strdelegate:nilcancelButtonTitle:确定otherButtonTitles:nil, nil] show];_vc.path [launchOptions[UIApplicationLaunchOptionsURLKey] description];[_vc preview];}return YES;
}- (BOOL)application:(UIApplication *)application openURL:(nonnull NSURL *)url options:(nonnull NSDictionaryNSString *,id *)options {if (options) {NSString *str [NSString stringWithFormat:\n发送请求的应用程序的 Bundle ID%\n\n文件的NSURL%, options[UIApplicationOpenURLOptionsSourceApplicationKey], url];[[[UIAlertView alloc] initWithTitle:message:strdelegate:nilcancelButtonTitle:确定otherButtonTitles:nil, nil] show];_vc.path [url description];[_vc preview];}return YES;
}