Skip to content

Commit 7ce3b89

Browse files
Merge pull request #392 from dynamsoft-docs/preview
Preview
2 parents 5fea074 + d624169 commit 7ce3b89

3 files changed

Lines changed: 216 additions & 558 deletions

File tree

programming/objectivec-swift/foundational-guide.md

Lines changed: 79 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ keywords: user guide, objective-c, oc, swift
66
needAutoGenerateSidebar: true
77
needGenerateH3Content: true
88
noTitleIndex: true
9-
multiProgrammingLanguage: true
10-
enableLanguageSelection: true
119
---
1210

1311
# Build Your App with Foundational APIs
@@ -18,11 +16,33 @@ enableLanguageSelection: true
1816
- Supported ABI: **arm64** and **x86_64**.
1917
- Development Environment: Xcode 13 and above (Xcode 14.1+ recommended).
2018

21-
## Add the SDK
19+
## Build Your First Application
20+
21+
In this section, let's create a **HelloWorld** app for reading barcodes from camera video input.
22+
23+
<div class="blockquote-note"></div>
24+
>
25+
>- XCode 14.2 is used here in this guide.
26+
>- You can download the complete Swift source code from [DecodeWithCameraEnhancer Sample](https://github.com/Dynamsoft/barcode-reader-mobile-samples/tree/main/ios/FoundationalAPISamples/DecodeWithCameraEnhancer){:target="_blank"}
27+
>- DCE is used for camera capture in this guide below. If you use the iOS AVFoundation framework for camera capture, check [DecodeWithAVCaptureSession sample](https://github.com/Dynamsoft/barcode-reader-mobile-samples/tree/main/ios/FoundationalAPISamples/DecodeWithAVCaptureSession){:target="_blank"} on how to add barcode scanning to your app.
28+
29+
### Step 1: Create a New Project
30+
31+
1. Open Xcode and select create a new project.
32+
33+
2. Select **iOS > App** for your application.
34+
35+
3. Input your product name (DBRHelloworld), interface (StoryBoard) and select the language (Objective-C/Swift).
36+
37+
4. Click on the **Next** button and select the location to save the project.
38+
39+
5. Click on the **Create** button to finish.
40+
41+
### Step 2: Add the SDK
2242

2343
There are three ways to add the SDK into your project - **Manually**, via **CocoaPods**, or via **Swift Package Manager**.
2444

25-
### Option 1: Add the xcframeworks via Swift Package Manager
45+
#### Option 1: Add the xcframeworks via Swift Package Manager
2646

2747
1. In your Xcode project, go to **File --> AddPackages**.
2848

@@ -32,7 +52,7 @@ There are three ways to add the SDK into your project - **Manually**, via **Coco
3252

3353
4. Check all the **xcframeworks** and add.
3454

35-
### Option 2: Add the Frameworks via CocoaPods
55+
#### Option 2: Add the Frameworks via CocoaPods
3656

3757
1. Add the frameworks in your **Podfile**, replace `TargetName` with your real target name.
3858

@@ -53,7 +73,7 @@ There are three ways to add the SDK into your project - **Manually**, via **Coco
5373
pod install
5474
```
5575

56-
### Option 3: Add Local xcframeworks files
76+
#### Option 3: Add Local xcframeworks files
5777

5878
1. Download the SDK package from the <a href="https://www.dynamsoft.com/barcode-reader/downloads/?utm_source=docs#mobile" target="_blank">Dynamsoft Website</a>. After unzipping, you can find the following **xcframeworks** under the **Dynamsoft\Frameworks** directory:
5979

@@ -64,33 +84,7 @@ There are three ways to add the SDK into your project - **Manually**, via **Coco
6484

6585
3. Click on the project settings then go to **General –> Frameworks, Libraries, and Embedded Content**. Set the **Embed** field to **Embed & Sign** for all above **xcframeworks**.
6686

67-
## Build Your First Application
68-
69-
In this section, let's create a **HelloWorld** app for reading barcodes from camera video input.
70-
71-
<div class="blockquote-note"></div>
72-
>
73-
>- XCode 14.2 is used here in this guide.
74-
>- You can download the complete Swift source code from [DecodeWithCameraEnhancer Sample](https://github.com/Dynamsoft/barcode-reader-mobile-samples/tree/main/ios/FoundationalAPISamples/DecodeWithCameraEnhancer){:target="_blank"}
75-
>- DCE is used for camera capture in this guide below. If you use the iOS AVFoundation framework for camera capture, check [DecodeWithAVCaptureSession sample](https://github.com/Dynamsoft/barcode-reader-mobile-samples/tree/main/ios/FoundationalAPISamples/DecodeWithAVCaptureSession){:target="_blank"} on how to add barcode scanning to your app.
76-
77-
### Create a New Project
78-
79-
1. Open Xcode and select create a new project.
80-
81-
2. Select **iOS > App** for your application.
82-
83-
3. Input your product name (DBRHelloworld), interface (StoryBoard) and select the language (Objective-C/Swift).
84-
85-
4. Click on the **Next** button and select the location to save the project.
86-
87-
5. Click on the **Create** button to finish.
88-
89-
### Include the Frameworks
90-
91-
Add the SDK to your new project. There are several ways to do this, all of which are explained in [this section](#add-the-sdk) for more details.
92-
93-
### Initialize the License
87+
### Step 3: Initialize the License
9488

9589
Dynamsoft Barcode Reader needs a valid license to work.
9690

@@ -102,12 +96,14 @@ Dynamsoft Barcode Reader needs a valid license to work.
10296
>
10397
>1.
10498
```objc
105-
#import <DynamsoftLicense/DynamsoftLicense.h>
106-
@interface ViewController () <DSLicenseVerificationListener>
99+
#import "ViewController.h"
100+
#import <DynamsoftCaptureVisionBundle/DynamsoftCaptureVisionBundle.h>
101+
@interface ViewController ()<DSLicenseVerificationListener>
102+
@end
107103
```
108104
2.
109105
```swift
110-
import DynamsoftLicense
106+
import DynamsoftCaptureVisionBundle
111107
class ViewController: UIViewController, LicenseVerificationListener
112108
```
113109

@@ -119,14 +115,16 @@ Dynamsoft Barcode Reader needs a valid license to work.
119115
>
120116
>1.
121117
```objc
118+
@implementation ViewController
122119
- (void)setLicense {
123120
[DSLicenseManager initLicense:@"DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9" verificationDelegate:self];
124121
}
125122
- (void)onLicenseVerified:(BOOL)isSuccess error:(nullable NSError *)error {
126123
if (!isSuccess && error != nil) {
127-
NSLog(@"error: %@", error);
124+
NSLog(@"error: %@", error);
128125
}
129126
}
127+
@end
130128
```
131129
2.
132130
```swift
@@ -149,62 +147,45 @@ Dynamsoft Barcode Reader needs a valid license to work.
149147
>- You can request a 30-day trial license via the [Request a Trial License](https://www.dynamsoft.com/customer/license/trialLicense?product=dbr&utm_source=guide&package=ios){:target="_blank"} link.
150148
>- If you download the <a href="https://www.dynamsoft.com/barcode-reader/downloads/?utm_source=docs#mobile" target="_blank">Installation Package</a>, it comes with a 30-day trial license by default.
151149
152-
### Configure the Camera Enhancer
150+
### Step 4: Configure the Camera Enhancer
153151

154-
1. Import all the headers that you will need in the `ViewController` file.
152+
Initialize `CameraEnhancer` and `CameraView` and add configurations for the `CameraEnhancer`.
155153

156-
<div class="sample-code-prefix"></div>
157-
>- Objective-C
158-
>- Swift
159-
>
160-
>1.
161-
```objc
162-
#import <DynamsoftCaptureVisionBundle/DynamsoftCaptureVisionBundle.h>
163-
#import <DynamsoftBarcodeReaderBundle/DynamsoftBarcodeReaderBundle.h>
164-
```
165-
2.
166-
```swift
167-
import DynamsoftCaptureVisionBundle
168-
import DynamsoftBarcodeReaderBundle
169-
```
170-
171-
2. Initialize `CameraEnhancer` and `CameraView` and add configurations for the `CameraEnhancer`.
172-
173-
<div class="sample-code-prefix"></div>
174-
>- Objective-C
175-
>- Swift
176-
>
177-
>1.
178-
```objc
179-
@interface ViewController () <DSLicenseVerificationListener>
180-
@property (nonatomic, strong) DSCameraView *cameraView;
181-
@property (nonatomic, strong) DSCameraEnhancer *dce;
182-
@end
183-
...
184-
@implementation ViewController
185-
- (void)setUpCamera {
186-
self.cameraView = [[DSCameraView alloc] initWithFrame:self.view.bounds];
187-
self.cameraView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
188-
[self.view insertSubview:self.cameraView atIndex:0];
189-
self.dce = [[DSCameraEnhancer alloc] init];
190-
self.dce.cameraView = self.cameraView;
191-
}
192-
...
193-
```
194-
2.
195-
```swift
196-
var cameraView:CameraView!
197-
let dce = CameraEnhancer()
198-
...
199-
func setUpCamera() {
200-
cameraView = .init(frame: view.bounds)
201-
cameraView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
202-
view.insertSubview(cameraView, at: 0)
203-
dce.cameraView = cameraView
204-
}
205-
```
154+
<div class="sample-code-prefix"></div>
155+
>- Objective-C
156+
>- Swift
157+
>
158+
>1.
159+
```objc
160+
@interface ViewController () <DSLicenseVerificationListener>
161+
@property (nonatomic, strong) DSCameraView *cameraView;
162+
@property (nonatomic, strong) DSCameraEnhancer *dce;
163+
@end
164+
...
165+
@implementation ViewController
166+
- (void)setUpCamera {
167+
self.cameraView = [[DSCameraView alloc] initWithFrame:self.view.bounds];
168+
self.cameraView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
169+
[self.view insertSubview:self.cameraView atIndex:0];
170+
self.dce = [[DSCameraEnhancer alloc] init];
171+
self.dce.cameraView = self.cameraView;
172+
}
173+
...
174+
```
175+
2.
176+
```swift
177+
var cameraView:CameraView!
178+
let dce = CameraEnhancer()
179+
...
180+
func setUpCamera() {
181+
cameraView = .init(frame: view.bounds)
182+
cameraView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
183+
view.insertSubview(cameraView, at: 0)
184+
dce.cameraView = cameraView
185+
}
186+
```
206187

207-
### Configure the Barcode Decoding Task via CaptureVisionRouter
188+
### Step 5: Configure the Barcode Decoding Task via CaptureVisionRouter
208189

209190
1. Initialize the `CaptureVisionRouter` and bind with the `CameraEnhancer` instance.
210191

@@ -302,17 +283,17 @@ Dynamsoft Barcode Reader needs a valid license to work.
302283
}
303284
}
304285
}
305-
private func showResult(_ title: String, _ message: String, completion: @escaping () -> Void) {
286+
private func showResult(_ title: String, _ message: String?, completion: (() -> Void)? = nil) {
306287
DispatchQueue.main.async {
307288
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
308-
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in completion() }))
289+
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in completion?() }))
309290
self.present(alert, animated: true, completion: nil)
310291
}
311292
}
312293
}
313294
```
314295

315-
### Configure viewDidLoad, viewWillAppear, viewWillDisappear
296+
### Step 6: Configure viewDidLoad, viewWillAppear, viewWillDisappear
316297

317298
Now that all of the main functions are defined and configured, let's finish things off on the code side of things by completing the `viewDidLoad`, `viewWillAppear`, and `viewWillDisappear` functions.
318299

@@ -374,11 +355,11 @@ override func viewWillDisappear(_ animated: Bool) {
374355
...
375356
```
376357

377-
### Configure Camera Permissions
358+
### Step 7: Configure Camera Permissions
378359

379360
Add **Privacy - Camera Usage Description** to the `info.plist` of your project to request camera permission. An easy way to do this is to access your project settings, go to *Info* and then add this Privacy property to the iOS target properties list.
380361

381-
### Additional Steps for iOS 12 or Lower Versions
362+
### Step 8: Additional Steps for iOS 12 or Lower Versions
382363

383364
If your iOS version is less than 13, please add the following additional steps:
384365

@@ -406,7 +387,7 @@ If your iOS version is less than 13, please add the following additional steps:
406387
}
407388
```
408389

409-
### (Optional) Manually Releasing Deep Learning Models
390+
### Step 9: (Optional) Manually Releasing Deep Learning Models
410391

411392
Starting from v11.2.1000, Dynamsoft Barcode Reader integrates deep learning models to enhance decoding ability. Once initialized, these models remain cached in memory until they are explicitly released. If the decoding task has finished, call `clearDLModelBuffers` to free the associated memory.
412393

@@ -417,7 +398,7 @@ Starting from v11.2.1000, Dynamsoft Barcode Reader integrates deep learning mode
417398
>1.
418399
```objc
419400
- (void)dealloc {
420-
[CaptureVisionRouter clearDLModelBuffers];
401+
[DSCaptureVisionRouter clearDLModelBuffers];
421402
}
422403
```
423404
2.
@@ -428,7 +409,7 @@ deinit
428409
}
429410
```
430411

431-
### Run the Project
412+
### Step 10: Run the Project
432413

433414
1. Select the device that you want to run your app on. **Please note that you will require a physical device to run the application on.** If you run the app on a simulator, you will experience some errors at runtime as a physical camera component is required. *If you have an M1 Macbook (or a later model) you can run the app on your Macbook directly as it has the same architecture as your iPhone/iPad.*
434415
2. Run the project, then your app will be installed on your device.

0 commit comments

Comments
 (0)