我想从pdf页面打开我的应用程序中的pdf文件, 但我没有在我的应用程序中打开pdf的任何选项。
这是我的info.plist文件
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>PDF</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>CFBundleTypeIconFiles</key>
<string>Icon.png</string>
<key>LSItemContentTypes</key>
<string>com.neosofttech.pdf</string>
<key>LSHandlerRank</key>
<string>Owner</string>
</dict>
</array>
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.pdf</string>
</array>
<key>UTTypeDescription</key>
<string>PDFReader File</string>
<key>UTTypeIdentifier</key>
<string>com.neosofttech.pdf</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>pdf</string>
</dict>
</dict>
请告诉我这里我错了,如何在我的应用程序中打开pdf文件。
答案 0 :(得分:49)
我假设您正在尝试根据您的plist在Mail中打开附件。由于您不拥有PDF文件类型,因此无法将其声明为导出类型。您只需将其声明为受支持的文档类型。
例如:
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>PDF</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>com.adobe.pdf</string>
</array>
</dict>
</array>
您可以找到所有系统提供的内容类型here或UTCoreTypes.h
中的列表。
当有人打开PDF文件并选择您的应用程序时,该文件将被复制到应用程序沙箱中Inbox
文件夹的Documents
子目录中。然后,您的应用将被打开,并且在您的应用委托application:didFinishLaunchingWithOptions:
内,您的launchOptions
词典将包含一个UIApplicationLaunchOptionsURLKey
密钥,用于指定沙盒中您可以使用的文件的URL。
答案 1 :(得分:2)
除非您执行以下操作,否则您无法执行您要执行的操作:
那么这意味着您在info.plist中register a custom url handler。例如,假设您使用coolapp://形式创建自定义URL处理程序。现在,在生成PDF的文档内部,当您创建超链接时,让它使用您的自定义URL,例如coolapp:// filedetails?pdfurl = mywebsite.com / path / to / pdffile.pdf&amp; pageno = 5。然后,您必须在-application:handleOpenURL:中解析此问题。下载 pdfurl 字段中指定的文件,然后跳转到 pageno 中指定的页面。
请注意,这假设您自己正在创建有问题的PDF。如果您不控制PDF内容创建过程,则无法执行您想要执行的操作。无法从任意PDF打开您的应用。必须使用自定义URL完成。
如果您需要澄清,请告诉我。
答案 2 :(得分:1)
将以下代码插入info.plist
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>"*"</string>
</array>
<key>CFBundleTypeName</key>
<string>Unknown</string>
<key>LSItemContentTypes</key>
<array>
<string>public.data</string>
</array>
</dict>
</array>
答案 3 :(得分:0)
旧帖子-但是我登陆这里也是在寻找一种将PDF传递到我的App的方法。
Ashley Clark描绘的plist帮助我将App注册为可以将PDF文件作为参数的App。为他+1:-)
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>PDF</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>com.adobe.pdf</string>
</array>
</dict>
</array>
但是,要完全实现此目的,我在AppDelegate.m文件中使用了以下方法:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
NSLog(@"sourceApplication: %@", sourceApplication);
NSLog(@"url: %@", url);
当我在运行Safari并打开要在此处共享的PDF时打开应用程序时,上面的内容就会运行。
上面,我可以确定是哪个应用程序通过了我的应用程序,以及它所在位置的URL。
运行时,它输出以下内容:
sourceApplication: com.apple.mobilesafari
url: file:///Users/me/Library/Developer/CoreSimulator/Devices/C395439F-7A0D-434F-8D78-D4615DB643E1/data/Containers/Data/Application/E3B181BC-6075-4F2C-923D-2ECC642DA555/Documents/Inbox/pdf-5.pdf
然后我可以使用URL打开PDF并在我的应用程序中对其进行任何处理。