单击按钮时打开 PDF 文档 [英] Opening a PDF document when clicking a button

查看:25
本文介绍了单击按钮时打开 PDF 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的家庭餐厅快速创建一个应用程序.我目前有一个按钮,您可以单击该按钮,该按钮也会将您带到我们菜单的 web 查看器页面.我想在应用程序中制作菜单,这样它就不会重定向到 safari.

I am trying to create an app for my family restaurant in swift. I currently have a button that you click on that takes you too a webviewer page of our menu. I want to make the menu with in the app so it does not redirect to safari.

基本上我想要做的是单击一个按钮,它会在应用程序中打开菜单 pdf 而不是 safari.

Basically what I want to do is click a button and it opens the menu pdf within the app instead safari.

我也使用的代码在浏览器中打开 PDF:

The code that I use too open the PDF in a browser:

    @IBAction func menu(sender: AnyObject) {
    if let url = NSURL(string:"http://nebula.wsimg.com/db5e994c02db104ea89bdf6e59550490?AccessKeyId=895454CA4A1E296ED3E3&disposition=0&alloworigin=1") {
        UIApplication.sharedApplication().openURL(url)
    }

推荐答案

您可以使用 QLPreviewController 来预览您的 pdf,但它需要是本地资源或在预览之前从网络下载:

You can use QLPreviewController to preview your pdf but it needs to be a local resource or downloaded from the web prior to previewing:

Swift 3 或更高版本

import UIKit
import QuickLook

class ViewController: UIViewController, QLPreviewControllerDataSource {
    let preview = QLPreviewController()
    func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
        return 1
    }
    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
        return Bundle.main.url(forResource: "menu", withExtension: "pdf")!
            as QLPreviewItem
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // set preview data source
        preview.dataSource = self
        // set current item index (only one = 0)
        preview.currentPreviewItemIndex = 0
    }
    @IBAction func showMenu(sender: UIButton) {
        present(preview, animated: true) {
            // code
        }
    }
}

这篇关于单击按钮时打开 PDF 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆