Dynamsoft Code Parser - iOS User Guide
Requirements
- Supported OS: iOS 11 or higher (iOS 13 and higher recommended).
- Supported ABI: arm64 and x86_64.
- Development Environment: Xcode 13 and above (Xcode 14.1+ recommended).
Add the xcframeworks
The Dynamsoft Code Parser (DCP) iOS SDK comes with four libraries:
File | Description |
---|---|
DynamsoftCodeParser.xcframework |
The Dynamsoft Code Parser library, which includes code parsing logic and related APIs. |
DynamsoftCore.xcframework |
The core library, which includes common basic structures and intermediate result related APIs. |
DynamsoftLicense.xcframework |
The license library, which includes license related APIs. |
DynamsoftCodeParserDedicator.xcframework |
The code parser helper library, which includes some validation functions used by the SDK. |
-
Add the frameworks in your Podfile, replace
TargetName
with your real target name.target '{Your project name}' do use_frameworks! pod 'DynamsoftCodeParser','2.2.10' pod 'DynamsoftCodeParserDedicator','1.2.20' pod 'DynamsoftCore','3.2.30' pod 'DynamsoftLicense','3.2.20' end
-
Execute the pod command to install the frameworks and generate workspace({TargetName}.xcworkspace):
pod install
Build Your First Application
The following sample will demonstrate how to create a simple HelloWorld
app for parsing text or bytes.
Note:
- The following steps are completed in XCode 14.2
Create a New Project
-
Open XCode and select New Project… in the File > New > New Project… menu to create a new project.
-
Select iOS -> App for your application.
-
Input your product name (HelloWorld), interface (StoryBoard) and language (Objective-C/Swift)
-
Click on the Next button and select the location to save the project.
-
Click on the Create button to finish.
Include the Library
Add the SDK to your new project. Please read Add the xcframeworks section for more details.
Initialize the License
-
Use the
LicenseManager
class and initialize the license in the file AppDelegate.func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", verificationDelegate:self) return true } func onLicenseVerified(_ isSuccess: Bool, error: Error?) { if(error != nil) { if let msg = error?.localizedDescription { print("Server license verify failed, error:\(msg)") } } }
Note:
- Network connection is required for the license to work.
- The license string here will grant you a time-limited trial license.
- You can request a 30-day trial license via the Request a Trial License link. Offline trial license is also available by contacting us.
Initialize Code Parser And Parse the Code Bytes
-
Import and initialize the
CodeParser
.import UIKit import CodeParser class ViewController: UIViewController { private let codeParser = CodeParser() }
-
Add a “Parse” button on the UI.
class ViewController: UIViewController { ... lazy var parseButton: UIButton = { let left = 120.0 let width = UIScreen.main.bounds.size.width - 2 * left let height = 50.0 let top = UIScreen.main.bounds.size.height - 50.0 - height let button = UIButton(frame: CGRectMake(left, top, width, height)) button.backgroundColor = .darkGray button.setTitle("Parse", for: .normal) button.setTitleColor(.white, for: .normal) button.layer.cornerRadius = 6.0 button.titleLabel?.font = UIFont.systemFont(ofSize: 20.0) button.addTarget(self, action: #selector(parseCode(_:)), for: .touchUpInside) return button }() override func viewDidLoad() { super.viewDidLoad() self.view.addSubview(parseButton) } }
-
Parse the code bytes when the user clicks the “Parse” button. Here we use the passport MRZ string as an example.
class ViewController: UIViewController { ... @objc private func parseCode(_ button: UIButton) -> Void { let passportMRZStr = "P<D<<ADENAUER<<KONRAD<HERMANN<JOSEPH<<<<<<<<1234567897D<<7601059M6704115<<<<<<<<<<<<<<<2" let data = passportMRZStr.data(using: String.Encoding(rawValue: NSUTF8StringEncoding)) guard data != nil else {return} let dcpResultItem = try? self.codeParser.parse(data!, taskSettingName: "") showResults(dcpResultItem) } }
Note:
- Check out the complete supported code types and fields for details.
Display Parsed Results
-
Display the parsed result(s) in an alert dialog box.
class ViewController: UIViewController { ... private func showResults(_ item: ParsedResultItem?) -> Void { if item != nil { guard let parsedFields = item?.parsedFields else { return } var result = "" for (key,value) in parsedFields { result += "\(key):\(value)\n" } let alterController = UIAlertController(title:"Result", message: result, preferredStyle: .alert) let okAction = UIAlertAction(title:"OK", style: .default, handler: nil) alterController.addAction(okAction) present(alterController, animated: true, completion: nil) } } }
Build and Run the Project
- Select the device that you want to run your app on.
- Run the project, then your app will be installed on your device.