当前位置:   article > 正文

Foxit Mobile PDF SDK 入门级(1)-显示一个PDF-Swift工程中的使用(Xcode 7.x, swift 2.x)_xkov

xkov


Swift 渐渐流行,Foxit的SDK是不是可以在Swift中很好的使用呢?答案当然是可以的啦。

SDK的下载申请:

英文网站:

http://www.foxitsdk.com/products/mobile-pdf-sdk/request-trial/

中文网站:

http://www.foxitsoftware.cn/products/sdk/mobile-pdf-sdk/


对iOS的开发我是小白,Swift的了解也是少之又少,不过有万能的Google/百度,现学现卖还是很快的。

因为我的机器系统比较旧,Xcode是7.x版本,所有就暂时试试Swift2.x了,打算这个尝试做完就升级系统, 升级Xocde了。

废话少说,直接写步骤吧!(其实只要几分钟的事情,截图是给小白看的 ,高手可能不用看这文章就知道咋做了,为什么截图呢,比较容易一步一步照着做,也因为我自己也是小白)

一样,准备好SDK包,然后创建Swift工程,

第一步:新建工程

打开XCode File->NewProject->Single View Application.创建一个新的Universal Swift app


选择Single View Application

填写Project名称,注意:语言选择Swift, Devices选择Universal,如下图

接着选择一个存放目录,点击Create就生成好工程了。

如下图:

第二步:导入库存及工程设置

导入库

和之前的Objective-C的步骤类似先到入库,我只是想做最基础的显示所有加入FoxitRDK.framework就可以了,如图:

选择从SDK包中lib 目录下的FoxitRDK.framework,并注意其他选项,如下图:





Add之后,工程如下图:


E mbedded Binaries

接着选中工程找到General下的 E mbedded Binaries,然后点”+”把 ”FoxitRDK.framework” 加入进来,这个和Objective-C下是一样的:



点击这里的"+"就会弹出如下界面,选择FoxitRDK.framework,然后点击Add,

加完之后Embedded Binaries就会出现FoxitRDK.framework了 如图:


加入测试文件:

用发布包中的Getting_started_ios.pdf吧,选中SwiftDemo工程,右键选择菜单中的”Add Files to “SwiftDemo”,然后选择文件加入

选择Getting_started_ios.pdf,完成后工程长这样:

创建和配置Bridging-Header.h

Swift与OC进行混编,首先要有一个.h文件,这里使用Bridging-Header.h然后设置项目的Build Settings->Swift Compiler->Objective-C Bridging Header内容为Bridging-Header.h,这个与Bridging-Header.h位置有关,从项目的根目录开始在Objective-C Bridging Header选项里面写入Bridging-Header.h相对路径。
File->New->File...

创建Bridging-Header.h


点击File...



Build Setting



接着,把需要用到的头文件写入到这个Bridging-Header.h

我写入的内容如下:
  1. //
  2. // Bridging-Header.h
  3. // SwiftDemo
  4. //
  5. // Created by Amy Lin on 16/11/2.
  6. // Copyright © 2016年 Amy Lin. All rights reserved.
  7. //
  8. #ifndef Bridging_Header_h
  9. #define Bridging_Header_h
  10. #import <FoxitRDK/FSPDFObjC.h>
  11. #import <FoxitRDK/FSPDFViewControl.h>
  12. #endif /* Bridging_Header_h */


设置Other Linker Flags -lstdc++


第三步,开始写代码

库的初始化

我在AppDelegate.swift这个文件中的 application函数中做初始化
代码如下:
  1. <pre name="code" class="plain"> //init library
  2. let sn: String = ""
  3. let key: String = ""
  4. let eRet = FSLibrary.init(sn,key:key)
  5. if e_errSuccess != eRet {
  6. return false
  7. }
 整个文件的代码 
  1. <pre name="code" class="plain">//
  2. // AppDelegate.swift
  3. // SwiftDemo
  4. //
  5. // Created by Amy Lin on 16/11/2.
  6. // Copyright © 2016年 Amy Lin. All rights reserved.
  7. //
  8. import UIKit
  9. @UIApplicationMain
  10. class AppDelegate: UIResponder, UIApplicationDelegate {
  11. var window: UIWindow?
  12. func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
  13. // Override point for customization after application launch.
  14. //init library
  15. let sn: String = "TtGWd61AP09nKyEIkWRip9/sdHEfet/NbaCW6bGFnaQdGdppuzvWHg=="
  16. let key: String = "ezJvj93HtBp39Js1IV0+hIBmZehlKDiKb8RKO8M5RZ7SkoE5DnYhZ2kCHF8rv3HJbfvMjg9nnncPclJpsPsT3myZuPtAromUOHhJSHx9Yar/ANfHXTuFa8+AC+00R0GfFZs8LHgxu2kK+uaE8KiJiURZuDYxGzqKZkAgv52o1qs7FSeP0P+1jzVy5q/2xlz1xKovfnAISs1l/ezggBocDP8DLmFtWU3LQ/LFTVKTWtu2XUgJ5TulzhayQfzI58679RBMK2gfzq1611T+wtP3ZX57S8d5PHKMpAXaiN/qN14aCN0YvQsBCc3XhSAdP+IGkS/vpqVTXLCW5NXpwFMCQxhNt6ggR0w+51HNZ//lyBHCnhfs+L1G5ENqn0hPwpalMrY6Z8L1csnoVPhV7hUCO4csnyDbIWi3ynoh2hvfwlmBqsmyLiaSYK2AcH4RgNjlG8gF4RhD7Qg0PG8nSR+RpFaHuNiYvhF7cdcID5mXqgnKbh6EHHjoXSd3VM7hLDOIPGcD+3eCJLPQBSFny5I0VPHLV8C20T4I915rbKfh0lHwW1eXntygMBcS1mpr7mrZqXqoGdZTFLGBIp3gWS2KXbqRJAu+8bFMJLE07K6KS+crxW37gJA84uuNT7vhVc5hIk9zcOR7R+eC4o0Vp1IDSN0SZXttRezUddPQWG+6+CbygXTYzlqotyunjSklgwVk1mJD1VrgzkMC8lQyGyBoBKIIcSsmwWB3zdKD9GkGVkwbAM0HQbRh6X7pKx+KBCHqXokzpblz9tmtM4XpYFrZv6lJWo8DikZzLA4UDav0HPZB5TGDWlgghpq2pVspcCxXYn+lgXcJ05AgM9TQwYebJ5T9FKvUJXPl3oIfNYSndDoOeAfdtAD+uxfktPOxWAOaSTJhkv1AMjAjI31rGSmTKEWqHh+rKLOA1DAvimpKH6FCSlquwNojPtL4+czCVjEk04CZHDbDA29zy5mAeVlh2tJXB4PpObh35b/vWENTboO+G+HaOmqW0X2Zl0lcTuTXrJzNKfh6i59FnoRxYDqMY9n8GuGV13mAPan+WrCKIFdLnz4m3DrVmbqyu5AIHIcTDBajE5yQ0DIrP0u9ZuTQvPocydKzxUvDmho8D0oMCBXeO8HzCYzL/ISE9nKPjgYnpSU6DM84WQruswXLgYJxhWwLINEgo9PLnPS3gP4UfqM4FVvQZ1s7dBjUoBjJIg=="
  17. let eRet = FSLibrary.init(sn,key:key)
  18. if e_errSuccess != eRet {
  19. return false
  20. }
  21. return true
  22. }
  23. func applicationWillResignActive(application: UIApplication) {
  24. // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
  25. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
  26. }
  27. func applicationDidEnterBackground(application: UIApplication) {
  28. // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
  29. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  30. }
  31. func applicationWillEnterForeground(application: UIApplication) {
  32. // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
  33. }
  34. func applicationDidBecomeActive(application: UIApplication) {
  35. // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  36. }
  37. func applicationWillTerminate(application: UIApplication) {
  38. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  39. }
  40. }
 

文档加载和显示

在ViewController.swift这个文件中 

1. 在UIViewController中定义 pdfViewCtrl

    var pdfViewCtrl: FSPDFViewCtrl!

2.LoadDoc And Show

viewDidLoad函数中加入以下代码

        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.pdfViewCtrl = FSPDFViewCtrl(frame:self.view.bounds)
        //Load Doc
        let pdfPath = NSBundle.mainBundle().pathForResource("getting_started_ios", ofType: "pdf")
        let doc = FSPDFDoc.createFromFilePath(pdfPath)
        if e_errSuccess == doc!.load(nil) {
            self.pdfViewCtrl.setDoc(doc)
            self.view.insertSubview(self.pdfViewCtrl, atIndex: 0)
           }
ViewController.swift整个文件的代码如下

//
//  ViewController.swift
//  SwiftDemo
//
//  Created by Amy Lin on 16/11/2.
//  Copyright © 2016年 Amy Lin. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    var pdfViewCtrl: FSPDFViewCtrl!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.pdfViewCtrl = FSPDFViewCtrl(frame:self.view.bounds)
        //Load Doc
        let pdfPath = NSBundle.mainBundle().pathForResource("getting_started_ios", ofType: "pdf")
        let doc = FSPDFDoc.createFromFilePath(pdfPath)
        if e_errSuccess == doc!.load(nil) {
            self.pdfViewCtrl.setDoc(doc)
            self.view.insertSubview(self.pdfViewCtrl, atIndex: 0)
           }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


build 运行后效果


其他说明

本文中的代码,是满足显示的最小代码,如果实际开发,需要加入出错判断等。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/243024
推荐阅读
相关标签
  

闽ICP备14008679号