3

我有一个可以打开 PDF 文件的应用程序。我已将此应用程序注册为外部 PDF 查看器。当我有一个 PDF 文件并单击共享按钮时,我会看到我的应用程序。如果我从外部源打开 PDF 文件,PDF 将被复制到内部应用程序存储“.../Documents/Inbox”。

如何检测我的应用程序是否以这种方式启动?我需要检测是否是,并使用新的 PDF 文件显示 PDFViewController。

我可以从“收件箱”读取数据,但无法检测到“应用程序启动类型”。以及如何检测新的 pdf 文件?按创建日期排序并获得第一?

应用委托:

func applicationWillEnterForeground(_ application: UIApplication) {
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "openPdfFromExternalSource"), object: nil)
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

在 PDFViewController 我有:

    override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(self.ShowPdfFile), name: NSNotification.Name(rawValue: "openPdfFromExternalSource"), object: nil)
}
4

1 回答 1

0

我找到了。“应用程序(_:open:options :)”总是被调用(当应用程序在后台时),我可以从共享选项打开一个pdf文件。

application(_ app: UIApplication, 
                 open url: URL, 
              options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool

url 参数包含 pdf 文件的路径。有关更多信息,请参阅 https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623112-application

于 2018-04-23T08:30:09.357 回答