Objective c 支持开放的。。。iOS Mail和Safari应用程序中的菜单项
我需要让我的应用程序从Safari和邮件应用程序中打开文档,并在Objective c 支持开放的。。。iOS Mail和Safari应用程序中的菜单项,objective-c,ios,cocoa-touch,uidocumentinteraction,Objective C,Ios,Cocoa Touch,Uidocumentinteraction,我需要让我的应用程序从Safari和邮件应用程序中打开文档,并在UIDocumentInteractionController类中使用“在…”打开。我如何做到这一点?这个问题有一个极好的答案。为了清楚起见,我复制了下面的一些答案,但是你应该参考这个问题来获得完整的答案 文件类型处理是iPhone OS 3.2的新功能,与现有的自定义URL方案不同。您可以注册应用程序来处理特定的文档类型,任何使用文档控制器的应用程序都可以将这些文档的处理交给您自己的应用程序 要注册支持,您需要在Info.plis
UIDocumentInteractionController
类中使用“在…”打开。我如何做到这一点?这个问题有一个极好的答案。为了清楚起见,我复制了下面的一些答案,但是你应该参考这个问题来获得完整的答案
文件类型处理是iPhone OS 3.2的新功能,与现有的自定义URL方案不同。您可以注册应用程序来处理特定的文档类型,任何使用文档控制器的应用程序都可以将这些文档的处理交给您自己的应用程序
要注册支持,您需要在Info.plist中包含以下内容:
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array>
<string>Document-molecules-320.png</string>
<string>Document-molecules-64.png</string>
</array>
<key>CFBundleTypeName</key>
<string>Molecules Structure File</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSHandlerRank</key>
<string>Owner</string>
<key>LSItemContentTypes</key>
<array>
<string>com.sunsetlakesoftware.molecules.pdb</string>
<string>org.gnu.gnu-zip-archive</string>
</array>
</dict>
</array>
CbundleDocumentTypes
CbundleTypeConfigiles
Document-policles-320.png
Document-policles-64.png
CbundleTypeName
分子结构文件
CbundleTypeRole
观众
伊斯汉德兰克
所有者
lsItemContentType
com.sunsetlakesoftware.molecules.pdb
org.gnu.gnu-zip-archive
上面示例中使用的一个UTI是系统定义的,但另一个是特定于应用程序的UTI。需要导出特定于应用程序的UTI,以便系统上的其他应用程序可以知道它。为此,您需要向Info.plist中添加一个部分,如下所示:
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.plain-text</string>
<string>public.text</string>
</array>
<key>UTTypeDescription</key>
<string>Molecules Structure File</string>
<key>UTTypeIdentifier</key>
<string>com.sunsetlakesoftware.molecules.pdb</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>pdb</string>
<key>public.mime-type</key>
<string>chemical/x-pdb</string>
</dict>
</dict>
</array>
#import <UIKit/UIKit.h>
@interface OfflineReaderViewController : UIViewController
<UIDocumentInteractionControllerDelegate> {
IBOutlet UIWebView *webView;
}
-(void)openDocumentIn;
-(void)handleDocumentOpenURL:(NSURL *)url;
-(void)displayAlert:(NSString *) str;
-(void)loadFileFromDocumentsFolder:(NSString *) filename;
-(void)listFilesFromDocumentsFolder;
- (IBAction) btnDisplayFiles;
@end
UTExportedTypeDeclarations
Uttypecommisto
公共文本
public.text
UTTypeDescription
分子结构文件
UTTypeIdentifier
com.sunsetlakesoftware.molecules.pdb
Uttypetag规范
public.filename-extension
pdb
public.mime-type
化学/x-pdb
此特定示例导出具有.pdb文件扩展名的com.sunsetlakesoftware.molecules.pdb
UTI,对应于MIME类型chemical/x-pdb
有了它,您的应用程序将能够处理附加到电子邮件或系统上其他应用程序的文档。在Mail中,您可以点击并按住以打开可打开特定附件的应用程序列表
打开附件后,您的应用程序将启动,您需要在
-application:didfishlaunchingwithoptions:
应用程序委托方法中处理此文件。以这种方式从邮件中加载的文件似乎被复制到应用程序的Documents目录下,该目录对应于它们到达的邮箱。我知道这对我作为一名初级程序员,甚至是一名中等技能的程序员来说都是非常令人沮丧的。通过邮件和Safari应用程序进行文件I/O涉及非常。。。有趣的是,应用程序本身中的命名约定。所以,让我们用iPhone的Xcode项目来解决问题。打开Xcode(本教程我将使用4.2)并选择“单一视图”应用程序模板(或者创建一个空项目,然后添加一个带有.xib的单一视图)
在新创建的应用程序中,将视图控制器(以及相关的xib)重命名为OfflineReaderServiceController
,然后我们将开始讨论代码。(除了前缀头和main.m之外,我们将处理所有文件,因此请注意,您需要所有的文件!)
输入AppDelegate标题并将以下代码粘贴到其中:
#import <UIKit/UIKit.h>
@class OfflineReaderViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) OfflineReaderViewController *viewController;
@end
这:
-(BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
if (url != nil && [url isFileURL]) {
[self.viewController handleDocumentOpenURL:url];
}
return YES;
}
是本教程最重要的部分。将其分解为各个部分:-(BOOL)应用程序:(UIApplication*)应用程序
是我们的示例应用程序openURL:(NSURL*)url
是发送来告诉我们要打开什么的urlsourceApplication:(NSString*)sourceApplication
是发送链接的应用程序;而annotation:(id)annotation
是一个我们不会涉及的额外特性
现在,我们必须布局我们的xib。输入xib(其标题应为“OfflineReaderServiceController”,但与xib无关,除非我们调用initWithNibName:
(我们不会这样做)),并使其看起来如下图所示:
进入UIWebView
的属性并选中“根据需要缩放页面”,这一点非常重要,因为这让我们可以通过收缩放大和缩小网页。暂时不要担心连接,我们将很快创建这些连接
输入OfflineReaderServiceController
标题并粘贴到以下内容中:
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.plain-text</string>
<string>public.text</string>
</array>
<key>UTTypeDescription</key>
<string>Molecules Structure File</string>
<key>UTTypeIdentifier</key>
<string>com.sunsetlakesoftware.molecules.pdb</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>pdb</string>
<key>public.mime-type</key>
<string>chemical/x-pdb</string>
</dict>
</dict>
</array>
#import <UIKit/UIKit.h>
@interface OfflineReaderViewController : UIViewController
<UIDocumentInteractionControllerDelegate> {
IBOutlet UIWebView *webView;
}
-(void)openDocumentIn;
-(void)handleDocumentOpenURL:(NSURL *)url;
-(void)displayAlert:(NSString *) str;
-(void)loadFileFromDocumentsFolder:(NSString *) filename;
-(void)listFilesFromDocumentsFolder;
- (IBAction) btnDisplayFiles;
@end
那些积极关注并不仅仅复制我告诉你的一切(只是开玩笑)的人会知道这行代码:[[NSBundle mainBundle]pathForResource:@“Minore”of type:@“pdf”]代码>将给我们一个SIGABRT,因为,嗯,文件不存在!因此,将你从任何地方提取的任何通用PDF文件拖入其中(我推荐,因为谁不花空闲时间阅读大量文档?),然后复制它的标题并粘贴到其中,去掉后缀(.PDF);类型:@“pdf”
的部分为我们解决了这个问题。完成后,该行应该如下所示:[[NSBundle mainBundle]pathForResource:@//file name//”of type:@“pdf”]代码>
现在回到xib并连接那些IBOutlets
!总而言之,以下是“文件所有者”选项卡的外观:
看来我们结束了…但是等等!我们没有做任何事情让“打开…”菜单启动并运行!事实证明,在.plist文件中有一些乱七八糟的东西。打开app.plist(快速右键单击,然后选择Open As>Source Code)并粘贴以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIconFiles</key>
<array/>
<key>CFBundleIdentifier</key>
<string>CodaFi.${PRODUCT_NAME:rfc1034identifier}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIFileSharingEnabled</key>
<true/>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>PDF Document</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSItemContentTypes</key>
<array>
<string>com.adobe.pdf</string>
</array>
</dict>
</array>
</dict>
</plist>
CfBundledDevelopmentRegion
EN
CbundleDisplayName
${产品名称
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIconFiles</key>
<array/>
<key>CFBundleIdentifier</key>
<string>CodaFi.${PRODUCT_NAME:rfc1034identifier}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIFileSharingEnabled</key>
<true/>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>PDF Document</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSItemContentTypes</key>
<array>
<string>com.adobe.pdf</string>
</array>
</dict>
</array>
</dict>
</plist>