|
| 1 | +--- |
| 2 | +layout: default-layout |
| 3 | +title: User Guide - Dynamsoft Code Parser SDK iOS Edition |
| 4 | +description: This is the user guide of Dynamsoft Code Parser SDK iOS Edition. |
| 5 | +keywords: user guide, iOS |
| 6 | +needAutoGenerateSidebar: true |
| 7 | +needGenerateH3Content: true |
| 8 | +noTitleIndex: true |
| 9 | +--- |
| 10 | + |
| 11 | +# Dynamsoft Code Parser - iOS User Guide |
| 12 | + |
| 13 | +- [Dynamsoft Code Parser - iOS User Guide](#dynamsoft-code-parser---ios-user-guide) |
| 14 | + - [Requirements](#requirements) |
| 15 | + - [Add the xcframeworks](#add-the-xcframeworks) |
| 16 | + - [Add the xcframeworks Manually](#add-the-xcframeworks-manually) |
| 17 | + - [Add the xcframeworks via CocoaPods](#add-the-xcframeworks-via-cocoapods) |
| 18 | + - [Build Your First Application](#build-your-first-application) |
| 19 | + - [Create a New Project](#create-a-new-project) |
| 20 | + - [Include the Library](#include-the-library) |
| 21 | + - [Initialize the License](#initialize-the-license) |
| 22 | + - [Initialize Code Parser And Parse the Code Bytes](#initialize-code-parser-and-parse-the-code-bytes) |
| 23 | + - [Display Parsed Results](#display-parsed-results) |
| 24 | + - [Build and Run the Project](#build-and-run-the-project) |
| 25 | + |
| 26 | +## Requirements |
| 27 | + |
| 28 | +- Supported OS: iOS 11 or higher (iOS 13 and higher recommended). |
| 29 | +- Supported ABI: arm64 and x86_64. |
| 30 | +- Development Environment: Xcode 13 and above (Xcode 14.1+ recommended). |
| 31 | + |
| 32 | +## Add the xcframeworks |
| 33 | + |
| 34 | +The Dynamsoft Code Parser (DCP) iOS SDK comes with four libraries: |
| 35 | + |
| 36 | +| File | Description | |
| 37 | +|---------|-------------| |
| 38 | +| `DynamsoftCodeParser.xcframework` | The Dynamsoft Code Parser library, which includes code parsing logic and related APIs. | |
| 39 | +| `DynamsoftCore.xcframework` | The core library, which includes common basic structures and intermediate result related APIs. | |
| 40 | +| `DynamsoftLicense.xcframework` | The license library, which includes license related APIs. | |
| 41 | +| `DynamsoftCodeParserDedicator.xcframework` | The code parser helper library, which includes some validation functions used by the SDK. | |
| 42 | + |
| 43 | +There are two ways to add the libraries into your project - **Manually** and **Via the CocaPods**. |
| 44 | + |
| 45 | +### Add the xcframeworks Manually |
| 46 | + |
| 47 | +1. Download the SDK package from the <a href="https://www.dynamsoft.com/survey/dlr/?utm_source=docs" target="_blank">Dynamsoft website</a>. After unzipping, four **xcframework** files can be found in the **Dynamsoft\Frameworks** directory: |
| 48 | + |
| 49 | + - **DynamsoftCodeParser.xcframework** |
| 50 | + - **DynamsoftCore.xcframework** |
| 51 | + - **DynamsoftLicense.xcframework** |
| 52 | + - **DynamsoftCodeParserDedicator.xcframework** |
| 53 | + |
| 54 | +2. Drag and drop the above **xcframeworks** into your Xcode project. Make sure to check Copy items if needed and create groups to copy the **xcframeworks** into your project's folder. |
| 55 | + |
| 56 | +3. Click on the project settings then go to **General –> Frameworks, Libraries, and Embedded Content**. Set the **Embed** field to **Embed & Sign** for all the **xcframeworks**. |
| 57 | + |
| 58 | +### Add the xcframeworks via CocoaPods |
| 59 | + |
| 60 | +1. Add the frameworks in your **Podfile**, replace `TargetName` with your real target name. |
| 61 | + |
| 62 | + ```pod |
| 63 | + target '{Your project name}' do |
| 64 | + use_frameworks! |
| 65 | + |
| 66 | + pod 'DynamsoftCodeParser','2.0.20' |
| 67 | + pod 'DynamsoftCodeParserDedicator','1.0.10' |
| 68 | + pod 'DynamsoftCore','3.0.20' |
| 69 | + pod 'DynamsoftLicense','3.0.30' |
| 70 | + |
| 71 | + end |
| 72 | + ``` |
| 73 | + |
| 74 | +2. Execute the pod command to install the frameworks and generate workspace(**{TargetName}.xcworkspace**): |
| 75 | + |
| 76 | + ```bash |
| 77 | + pod install |
| 78 | + ``` |
| 79 | + |
| 80 | +## Build Your First Application |
| 81 | + |
| 82 | +The following sample will demonstrate how to create a simple `HelloWorld` app for parsing text or bytes. |
| 83 | + |
| 84 | +>Note: |
| 85 | +> |
| 86 | +>- The following steps are completed in XCode 14.2 |
| 87 | +>- View the entire Swift source code from [HelloWorld(Swift) sample](https://github.com/Dynamsoft/code-parser-mobile-samples/blob/main/ios/HelloWorld/) |
| 88 | +
|
| 89 | +### Create a New Project |
| 90 | + |
| 91 | +1. Open XCode and select New Project… in the File > New > New Project… menu to create a new project. |
| 92 | + |
| 93 | +2. Select **iOS -> App** for your application. |
| 94 | + |
| 95 | +3. Input your product name (HelloWorld), interface (StoryBoard) and language (Objective-C/Swift) |
| 96 | + |
| 97 | +4. Click on the **Next** button and select the location to save the project. |
| 98 | + |
| 99 | +5. Click on the **Create** button to finish. |
| 100 | + |
| 101 | +### Include the Library |
| 102 | + |
| 103 | +Add the SDK to your new project. Please read [Add the xcframeworks](#add-the-xcframeworks) section for more details. |
| 104 | + |
| 105 | +### Initialize the License |
| 106 | + |
| 107 | +1. Use the `LicenseManager` class and initialize the license in the file **AppDelegate**. |
| 108 | + |
| 109 | + ```swift |
| 110 | + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { |
| 111 | + LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", verificationDelegate:self) |
| 112 | + return true |
| 113 | + } |
| 114 | + func onLicenseVerified(_ isSuccess: Bool, error: Error?) { |
| 115 | + if(error != nil) |
| 116 | + { |
| 117 | + if let msg = error?.localizedDescription { |
| 118 | + print("Server license verify failed, error:\(msg)") |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + ``` |
| 123 | + |
| 124 | + >Note: |
| 125 | + > |
| 126 | + >- Network connection is required for the license to work. |
| 127 | + >- The license string here will grant you a time-limited trial license. |
| 128 | + >- If the license has expired, you can go to the <a href="https://www.dynamsoft.com/customer/license/trialLicense?utm_source=docs&product=dcp&package=mobile" target="_blank">customer portal</a> to request for an extension. |
| 129 | + |
| 130 | +### Initialize Code Parser And Parse the Code Bytes |
| 131 | + |
| 132 | +1. Import and initialize the `CodeParser`. |
| 133 | + |
| 134 | + ```swift |
| 135 | + import UIKit |
| 136 | + import CodeParser |
| 137 | + class ViewController: UIViewController { |
| 138 | + private let codeParser = CodeParser() |
| 139 | + |
| 140 | + } |
| 141 | + ``` |
| 142 | + |
| 143 | +2. Add a "Parse" button on the UI. |
| 144 | + |
| 145 | + ```swift |
| 146 | + class ViewController: UIViewController { |
| 147 | + ... |
| 148 | + lazy var parseButton: UIButton = { |
| 149 | + let left = 120.0 |
| 150 | + let width = UIScreen.main.bounds.size.width - 2 * left |
| 151 | + let height = 50.0 |
| 152 | + let top = UIScreen.main.bounds.size.height - 50.0 - height |
| 153 | + let button = UIButton(frame: CGRectMake(left, top, width, height)) |
| 154 | + button.backgroundColor = .darkGray |
| 155 | + button.setTitle("Parse", for: .normal) |
| 156 | + button.setTitleColor(.white, for: .normal) |
| 157 | + button.layer.cornerRadius = 6.0 |
| 158 | + button.titleLabel?.font = UIFont.systemFont(ofSize: 20.0) |
| 159 | + button.addTarget(self, action: #selector(parseCode(_:)), for: .touchUpInside) |
| 160 | + return button |
| 161 | + }() |
| 162 | + |
| 163 | + override func viewDidLoad() { |
| 164 | + super.viewDidLoad() |
| 165 | + self.view.addSubview(parseButton) |
| 166 | + } |
| 167 | + } |
| 168 | + ``` |
| 169 | + |
| 170 | +3. Parse the code bytes when the user clicks the "Parse" button. Here we use the passport MRZ string as an example. |
| 171 | + |
| 172 | + ```swift |
| 173 | + class ViewController: UIViewController { |
| 174 | + ... |
| 175 | + @objc private func parseCode(_ button: UIButton) -> Void { |
| 176 | + let passportMRZStr = "P<D<<ADENAUER<<KONRAD<HERMANN<JOSEPH<<<<<<<<1234567897D<<7601059M6704115<<<<<<<<<<<<<<<2" |
| 177 | + |
| 178 | + let data = passportMRZStr.data(using: String.Encoding(rawValue: NSUTF8StringEncoding)) |
| 179 | + |
| 180 | + guard data != nil else {return} |
| 181 | + |
| 182 | + let dcpResultItem = try? self.codeParser.parse(data!, taskSettingName: "") |
| 183 | + |
| 184 | + showResults(dcpResultItem) |
| 185 | + } |
| 186 | + } |
| 187 | + ``` |
| 188 | + |
| 189 | + >Note: |
| 190 | + >- Check out the complete <a href="https://www.dynamsoft.com/code-parser/docs/core/code-types/" target="_blank">supported code types and fields </a>for details. |
| 191 | + |
| 192 | +### Display Parsed Results |
| 193 | + |
| 194 | +1. Display the parsed result(s) in an alert dialog box. |
| 195 | + |
| 196 | + ```swift |
| 197 | + class ViewController: UIViewController { |
| 198 | + ... |
| 199 | + private func showResults(_ item: ParsedResultItem?) -> Void { |
| 200 | + if item != nil { |
| 201 | + guard let parsedFields = item?.parsedFields else { return } |
| 202 | + |
| 203 | + var result = "" |
| 204 | + for (key,value) in parsedFields { |
| 205 | + result += "\(key):\(value)\n" |
| 206 | + } |
| 207 | + |
| 208 | + let alterController = UIAlertController(title:"Result", message: result, preferredStyle: .alert) |
| 209 | + let okAction = UIAlertAction(title:"OK", style: .default, handler: nil) |
| 210 | + alterController.addAction(okAction) |
| 211 | + |
| 212 | + present(alterController, animated: true, completion: nil) |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + ``` |
| 217 | + |
| 218 | +### Build and Run the Project |
| 219 | + |
| 220 | +1. Select the device that you want to run your app on. |
| 221 | +2. Run the project, then your app will be installed on your device. |
| 222 | + |
| 223 | +>Note: |
| 224 | +> |
| 225 | +>- You can get the source code of the HelloWord app from the following link |
| 226 | +>- View the entire Swift source code from [HelloWorld sample](https://github.com/Dynamsoft/code-parser-mobile-samples/blob/main/ios/HelloWorld/) |
0 commit comments