【问题标题】:how importing pdf files in swift如何快速导入pdf文件
【发布时间】:2015-05-06 06:13:14
【问题描述】:

我是初学者 iOS 开发人员,我有 this book
它的书更大,但我没有找到答案。 我正在尝试尽可能多地搜索如何使用 swift 在 xcode6.1 中导入 pdf 文件。
这个想法是我的应用程序,用于为初学者学习一些英语课程,所以我将所有课程放在单独的 pdf 文件中,我希望用户点击课程名称,然后根据本课程打开 pdf 文件。

我希望你能理解这个想法。 请帮助我如何做到这一点

【问题讨论】:

  • 请展示您到目前为止所做的事情以及您遇到的问题。我们也是忙碌的开发人员。如果你展示你当前的代码和你需要帮助的地方,你得到帮助的机会就更大。
  • 我不知道如何开始我只需要一个起点即可:(

标签: ios swift pdf


【解决方案1】:
  1. 如果你想使用网络上的PDF-File,你可以在你的应用程序中下载这个文件,你可以使用AFNetworking是最简单的方法之一。请求应如下所示

    var configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    var man = AFURLSessionManager(sessionConfiguration: configuration)
    
    var URL = NSURL(string: "http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf")
    var request = NSURLRequest(URL:URL!)
    
    var downloadTask = man.downloadTaskWithRequest(request, progress: nil,
    destination:{(targetPath:NSURL!,response:NSURLResponse!) -> NSURL! in
    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
    let outPath = String(format: "%@/download/", documentsPath)
    var url:NSURL! = NSURL(fileURLWithPath: outPath)
    return url.URLByAppendingPathComponent(response.suggestedFilename as String!)
    },
    completionHandler:{(response:NSURLResponse!,filePath:NSURL!,error:NSError!)  in
        println(response.suggestedFilename)
    })
    
    downloadTask.resume();
    
  2. 如果你想在不下载的情况下显示 PDF,你可以使用 UIWebView

    let url : NSURL! = NSURL(string: "http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf")
            webView.loadRequest(NSURLRequest(URL: url))
    
  3. 如果您想使用本地 pdf 文件,您需要将它们下载到您的项目中,然后与此代码一起使用

    var filePath = NSBundle.mainBundle().pathForResource("PdfFile", ofType:"pdf")
    var data     = NSData(contentsOfFile:filePath)
    

【讨论】:

  • 是的,我想要什么,我将学习如何使用它。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-29
  • 2021-05-06
  • 2016-06-29
  • 1970-01-01
  • 2016-04-29
  • 1970-01-01
相关资源
最近更新 更多