the start guide of skyepub sdk for ios in swift

23
The Start Guide of SkyEpub SDK for iOS in Swift The Start Guide of SkyEpub SDK for iOS in Swift SkyEpub SDK is the most advanced and widely used epub sdk, which provides almost every feature to create a high level epub reader/viewer with minimum efforts. This document is designed to show the easy way how to start and set your iOS application with SkyEpub sdk for iOS. and this start guide focuses on only making the skelecton of an epub reader for the very first time. The source code (SkyTS) of this document is also available at http://www.skyepub.net/downloads. You’d better refer to SkyEpub Advanced Demo for Swift or SkyEpub Advanced Demo for Objective-C to find the fully implemented epub reader demo using SkyEpub sdk. Prerequisite Software Xcode 12.4 or above SkyEpub SDK 8.5.x or above Document History 1.1 23 Feb 2021 1

Upload: others

Post on 02-Jul-2022

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

SkyEpub SDK is the most advanced and widely used epub sdk, which provides almost every feature to create a high level epub reader/viewer with minimum efforts. This document is designed to show the easy way how to start and set your iOS application with SkyEpub sdk for iOS. and this start guide focuses on only making the skelecton of an epub reader for the very first time. The source code (SkyTS) of this document is also available at http://www.skyepub.net/downloads. You’d better refer to SkyEpub Advanced Demo for Swift or SkyEpub Advanced Demo for Objective-C to find the fully implemented epub reader demo using SkyEpub sdk. Prerequisite Software

Xcode 12.4 or above SkyEpub SDK 8.5.x or above

Document History

1.1 23 Feb 2021

Page 2: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

Create Project  Choose New Project > iOS > App and press the Next button.

 Set the name of project to ‘SkyTS’ and choose Storyboard in Interface.  and set the language to ‘Swift’  after all settings, press Next.  

 

Page 3: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

  In the last step, press ‘Create’. Then the SkyTS folder will be created under the Xcode folder.       

Page 4: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

Import SkyEpub SDK  from http://www.skyepub.net/downloads download skyepub sdk for ios 

   Click the right button on SkyTS and choose New Group.  Set the name of the new group as ‘SkyEpub’                      

Page 5: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

   

 Drag all files downloaded to the ‘SkyEpub’ group which is created newly.                  

        

Page 6: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

 Put $(PROJECT_DIR)/SkyEpub in Project Setting > Build Setting > Search Paths > Library Search Paths  (if this is not set, unkwon errors will occur because Bridge.h can’t find SkyEpub.h.) 

                   

       

Page 7: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

Drag libskyepub.a onto Project Setting > Build Phases > Link Binary With Libraries 

    

Page 8: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

  

Create Bridge.h

                        

Page 9: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

 insert #import "SkyEpub.h" to Bridge.h 

                           

Page 10: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

Go to Project Setting > Build Setting > Swift Compiler - General and in Objective-c Bridging Header  put $(PROJECT_DIR)/Bridge.h  

                      

                     

10 

Page 11: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

Allow access to ‘localhost’ by setting App Transport Security Settings

 copy the xml below and past it to info.plist the name of newly added item is set as ‘App Transport Security Setting’  <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> 

<key>NSAllowsArbitraryLoads</key> <true/> <key>NSExceptionDomains</key> <dict> 

<key>localhost</key> <dict> 

<key>NSExceptionAllowsInsecureHTTPLoads</key> <true/> <key>NSExceptionMinimumTLSVersion</key> <string>TLSv1.2</string> <key>NSExceptionRequiresForwardSecrecy</key> <true/> <key>NSIncludesSubdomains</key> <true/> <key>NSRequiresCertificateTransparency</key> <false/> <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key> <true/> <key>NSThirdPartyExceptionMinimumTLSVersion</key> <string>TLSv1.2</string> <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> <true/> 

</dict> </dict> 

</dict> </plist>      

11 

Page 12: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

Check ‘App Transport Security Settings’ is properly set like the below.          

12 

Page 13: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

Import Sample epub file into this project.  Make the group and name it as ‘Samples’. and drag ‘Alice.epub’ into this group.  

    

    add ‘Alice.epub’ in Project Setting > Build Phases > Copy Bundle Resources 

     

13 

Page 14: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

Add the code below to AppDelegate.swift.   

import UIKit  @main class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. self.installSamples() return true } // get documents folder func getDocumentsPath() ->String { var documentsPath : String documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] return documentsPath } // create books folder to store epub books under documents folder. func createBooksDirectory() { let docPath = self.getDocumentsPath() let booksDir = docPath + "/books" let fileManager = FileManager.default if !fileManager.fileExists(atPath: booksDir) { do { try fileManager.createDirectory(atPath: booksDir, withIntermediateDirectories: true, attributes: nil) print(booksDir+" is created successfully") } catch { print("Couldn't create bools directory") } }else { print(booksDir+" has been already created!") } } func getBooksDirectory()->String { self.createBooksDirectory() let path = self.getDocumentsPath()+"/books" return path } func getBookPath(fileName:String) ->String { let booksDir = self.getBooksDirectory() let path = booksDir+"/"+fileName return path } func copyFileFromBundleToBooks(fileName:String) { let fileManager = FileManager.default

14 

Page 15: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

let bookPath = self.getBookPath(fileName:fileName) let bundle = Bundle.main let path = bundle.resourcePath! + "/" + fileName do { try fileManager.copyItem(atPath: path, toPath: bookPath) }catch { print(error) } } func installSamples() { let fileName = "Alice.epub" let samplePath = self.getBookPath(fileName: fileName) let fileManager = FileManager.default if !fileManager.fileExists(atPath: samplePath) { createBooksDirectory() copyFileFromBundleToBooks(fileName: fileName) }else { print(fileName+" has been already installed!") } } // MARK: UISceneSession Lifecycle func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { // Called when a new scene session is being created. // Use this method to select a configuration to create the new scene with. return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) } func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) { // Called when the user discards a scene session. // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. // Use this method to release any resources that were specific to the discarded scenes, as they will not return. } }

      

15 

Page 16: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

Create new ViewController named ‘BookViewController.swift’

     

16 

Page 17: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

In Storyboard,  Press ⌘ + L and open Library panel.  and choose View Controller.

                    

 Choose newly created ViewController,  in Identity Inspector, select ‘BookViewController’ which is created above in Class item. 

put ‘BookViewController’ in Storyboard ID.  

17 

Page 18: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

  Press ⌘ + L and call the Library Panel again.  choose UIView and drag it onto the ViewController screen.   set top, left, right, bottom constraints to 0.   

                   

   

18 

Page 19: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

Drag the UIView newly created to class declaration by dragging while pressing control key.  Create Outlet and name it as ‘skyepubView’  

    

19 

Page 20: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

Add a Button to MainViewController. Make IBAction and name it as ‘openBookPressed’.  

 complete openBookPressed function.   

@IBAction func openBookPressed(_ sender: Any) { let bvc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "BookViewController") as! BookViewController bvc.modalPresentationStyle = .fullScreen let bi = BookInformation() bi.bookCode = 0 bi.position = 0 bi.fileName = "Alice.epub" bvc.bookInformation = bi self.present(bvc, animated: false, completion: nil) }

 Now when this button is pressed, it will open BookViewController.     

20 

Page 21: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

 

Complete the Code of BookViewController.  

import UIKit class BookViewController: UIViewController,ReflowableViewControllerDataSource,ReflowableViewControllerDelegate,SkyProviderDataSource { @IBOutlet weak var skyepubView: UIView! var bookCode:Int = -1 var bookInformation:BookInformation! var rv:ReflowableViewController! var ad:AppDelegate! override func viewDidLoad() { super.viewDidLoad() ad = UIApplication.shared.delegate as? AppDelegate self.makeBookViewer() // Do any additional setup after loading the view. } func getBookPath()->String { let bookPath:String = "\(rv.baseDirectory!)/\(rv.fileName!)" return bookPath } func makeBookViewer() { // make ReflowableViewController object for epub. rv = ReflowableViewController(startPagePositionInBook: self.bookInformation.position) // set the color for blank screen. rv.setBlankColor(UIColor.white) // set the inital background color. rv.changeBackgroundColor(UIColor.white) // global pagination mode 0 rv.setPagingMode(0) // set rv's datasource to self. rv.dataSource = self // set rv's delegate to self. rv.delegate = self // set filename and bookCode to open. rv.fileName = self.bookInformation!.fileName rv.bookCode = self.bookInformation!.bookCode self.bookCode = Int(self.bookInformation!.bookCode) // set baseDirector of rv to booksDirectory rv.baseDirectory = ad.getBooksDirectory() // since 8.5.0, the path of epub can be set by setBookPath. rv.setBookPath(self.getBookPath()) // set the font of rv rv.fontSize = Int32(self.getRealFontSize(fontSizeIndex: 2)) // set lineSpacing of rv

21 

Page 22: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

rv.lineSpacing = Int32(self.getRealLineSpacing(3)) rv.fontName = "Book Fonts" // 0: none, 1:slide transition, 2: curling transition. rv.transitionType = 2 // if true, sdk will show 2 pages when screen is landscape. rv.setDoublePagedForLandscape(true) // if true, sdk will use gloabal pagination. rv.setGlobalPaging(false) // 25% space (in both left most and right most margins) rv.setHorizontalGapRatio(0.25) // 20% space (in both top and bottom margins) rv.setVerticalGapRatio(0.10) // set License Key for Reflowable Layout rv.setLicenseKey("0000-0000-0000-0000"); // make SkyProvider object to read epub reader. let skyProvider:SkyProvider = SkyProvider() // set skyProvider datasource skyProvider.dataSource = self // set skyProvider book to rv's book skyProvider.book = rv.book // set the content provider of rv as skyProvider rv.setContentProvider(skyProvider) // set the coordinates and size of rv rv.view.frame = self.skyepubView.bounds rv.view.autoresizingMask = [.flexibleWidth,.flexibleHeight] // add tv to skyepubView which is made in story board as a container of epub viewer. self.skyepubView.addSubview(rv.view) self.addChild(rv) self.view.autoresizesSubviews = true } // returns real font size for given font size Index func getRealFontSize(fontSizeIndex:Int) ->Int { var rs:Int = 0 switch fontSizeIndex { case 0: rs = 15 case 1: rs = 17 case 2: rs = 20 case 3: rs = 24 case 4: rs = 27 default: rs = 20 } return rs } // returns real line spacing for given line spacing Index func getRealLineSpacing(_ lineSpaceIndex:Int) ->Int { var rs:Int = 0

22 

Page 23: The Start Guide of SkyEpub SDK for iOS in Swift

The Start Guide of SkyEpub SDK for iOS in Swift

switch lineSpaceIndex { case 0: rs = -1 case 1: rs = 125 case 2: rs = 150 case 3: rs = 165 case 4: rs = 180 case 5: rs = 200 default: rs = 150 } return rs } } 

23