Skip to content

Commit f92886b

Browse files
add dcp ios guide
1 parent e1fe9f6 commit f92886b

File tree

1 file changed

+213
-1
lines changed

1 file changed

+213
-1
lines changed

programming/ios/user-guide/getting-started.md

Lines changed: 213 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,216 @@ needGenerateH3Content: true
88
noTitleIndex: true
99
---
1010

11-
# Getting Started with iOS Edition
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+
68+
end
69+
```
70+
71+
2. Execute the pod command to install the frameworks and generate workspace(**{TargetName}.xcworkspace**):
72+
73+
```bash
74+
pod install
75+
```
76+
77+
## Build Your First Application
78+
79+
The following sample will demonstrate how to create a simple `HelloWorld` app for parsing text or bytes.
80+
81+
>Note:
82+
>
83+
>- The following steps are completed in XCode 14.2
84+
>- View the entire Swift source code from [HelloWorld(Swift) sample](https://github.com/Dynamsoft/code-parser-mobile-samples/blob/main/ios/HelloWorld/)
85+
86+
### Create a New Project
87+
88+
1. Open XCode and select New Project… in the File > New > New Project… menu to create a new project.
89+
90+
2. Select **iOS -> App** for your application.
91+
92+
3. Input your product name (HelloWorld), interface (StoryBoard) and language (Objective-C/Swift)
93+
94+
4. Click on the **Next** button and select the location to save the project.
95+
96+
5. Click on the **Create** button to finish.
97+
98+
### Include the Library
99+
100+
Add the SDK to your new project. Please read [Add the xcframeworks](#add-the-xcframeworks) section for more details.
101+
102+
### Initialize the License
103+
104+
1. Use the `LicenseManager` class and initialize the license in the file **AppDelegate**.
105+
106+
```swift
107+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
108+
LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", verificationDelegate:self)
109+
return true
110+
}
111+
func onLicenseVerified(_ isSuccess: Bool, error: Error?) {
112+
if(error != nil)
113+
{
114+
if let msg = error?.localizedDescription {
115+
print("Server license verify failed, error:\(msg)")
116+
}
117+
}
118+
}
119+
```
120+
121+
>Note:
122+
>
123+
>- Network connection is required for the license to work.
124+
>- The license string here will grant you a time-limited trial license.
125+
>- 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.
126+
127+
### Initialize Code Parser And Parse the Code Bytes
128+
129+
1. Import and initialize the `CodeParser`.
130+
131+
```swift
132+
import UIKit
133+
import CodeParser
134+
class ViewController: UIViewController {
135+
private let codeParser = CodeParser()
136+
137+
}
138+
```
139+
140+
2. Add a "Parse" button on the UI.
141+
142+
```swift
143+
class ViewController: UIViewController {
144+
...
145+
lazy var parseButton: UIButton = {
146+
let left = 120.0
147+
let width = UIScreen.main.bounds.size.width - 2 * left
148+
let height = 50.0
149+
let top = UIScreen.main.bounds.size.height - 50.0 - height
150+
let button = UIButton(frame: CGRectMake(left, top, width, height))
151+
button.backgroundColor = .darkGray
152+
button.setTitle("Parse", for: .normal)
153+
button.setTitleColor(.white, for: .normal)
154+
button.layer.cornerRadius = 6.0
155+
button.titleLabel?.font = UIFont.systemFont(ofSize: 20.0)
156+
button.addTarget(self, action: #selector(parseCode(_:)), for: .touchUpInside)
157+
return button
158+
}()
159+
160+
override func viewDidLoad() {
161+
super.viewDidLoad()
162+
self.view.addSubview(parseButton)
163+
}
164+
}
165+
```
166+
167+
3. Parse the code bytes when the user clicks the "Parse" button. Here we use the passport MRZ string as an example.
168+
169+
```swift
170+
class ViewController: UIViewController {
171+
...
172+
@objc private func parseCode(_ button: UIButton) -> Void {
173+
let passportMRZStr = "P<D<<ADENAUER<<KONRAD<HERMANN<JOSEPH<<<<<<<<1234567897D<<7601059M6704115<<<<<<<<<<<<<<<2"
174+
175+
let data = passportMRZStr.data(using: String.Encoding(rawValue: NSUTF8StringEncoding))
176+
177+
guard data != nil else {return}
178+
179+
let dcpResultItem = try? self.codeParser.parse(data!, taskSettingName: "")
180+
181+
showResults(dcpResultItem)
182+
}
183+
}
184+
```
185+
186+
>Note:
187+
>- 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.
188+
189+
### Display Parsed Results
190+
191+
1. Display the parsed result(s) in an alert dialog box.
192+
193+
```swift
194+
class ViewController: UIViewController {
195+
...
196+
private func showResults(_ item: ParsedResultItem?) -> Void {
197+
if item != nil {
198+
guard let parsedFields = item?.parsedFields else { return }
199+
200+
var result = ""
201+
for (key,value) in parsedFields {
202+
result += "\(key):\(value)\n"
203+
}
204+
205+
let alterController = UIAlertController(title:"Result", message: result, preferredStyle: .alert)
206+
let okAction = UIAlertAction(title:"OK", style: .default, handler: nil)
207+
alterController.addAction(okAction)
208+
209+
present(alterController, animated: true, completion: nil)
210+
}
211+
}
212+
}
213+
```
214+
215+
### Build and Run the Project
216+
217+
1. Select the device that you want to run your app on.
218+
2. Run the project, then your app will be installed on your device.
219+
220+
>Note:
221+
>
222+
>- You can get the source code of the HelloWord app from the following link
223+
>- View the entire Swift source code from [HelloWorld sample](https://github.com/Dynamsoft/code-parser-mobile-samples/blob/main/ios/HelloWorld/)

0 commit comments

Comments
 (0)