Foxit Mobile PDF SDK 入門級(1)-顯示一個PDF-Swift工程中的使用(Xcode 7.x, swift 2.x)


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之後,工程如下圖:


Embedded Binaries

接着選中工程找到General下的Embedded 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

我寫入的內容如下:
//
//  Bridging-Header.h
//  SwiftDemo
//
//  Created by Amy Lin on 16/11/2.
//  Copyright © 2016年 Amy Lin. All rights reserved.
//

#ifndef Bridging_Header_h
#define Bridging_Header_h

#import <FoxitRDK/FSPDFObjC.h>
#import <FoxitRDK/FSPDFViewControl.h>

#endif /* Bridging_Header_h */


設置Other Linker Flags -lstdc++


第三步,開始寫代碼

庫的初始化

我在AppDelegate.swift這個文件中的application函數中做初始化
代碼如下:
<pre name="code" class="plain"> //init library
        let sn: String = ""
        let key: String = ""
        let eRet = FSLibrary.init(sn,key:key)
        if e_errSuccess != eRet {
            return false
        }

整個文件的代碼
<pre name="code" class="plain">//
//  AppDelegate.swift
//  SwiftDemo
//
//  Created by Amy Lin on 16/11/2.
//  Copyright © 2016年 Amy Lin. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        //init library
        let sn: String = "TtGWd61AP09nKyEIkWRip9/sdHEfet/NbaCW6bGFnaQdGdppuzvWHg=="
        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=="
        let eRet = FSLibrary.init(sn,key:key)
        if e_errSuccess != eRet {
            return false
        }
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        // 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.
        // 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.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // 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.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // 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.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // 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.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

文檔加載和顯示

在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 運行後效果


其他說明

本文中的代碼,是滿足顯示的最小代碼,如果實際開發,需要加入出錯判斷等。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章