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

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

问题描述

我正试图为我的家庭餐馆快速创建一个应用程序.我目前有一个单击的按钮,它也使您进入菜单的webviewer页面.我想在应用程序中使用菜单,因此它不会重定向到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,但是它必须是本地资源,或者需要从Web上下载才能进行预览:

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

迅速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天全站免登陆