@@ -172,8 +172,6 @@ Add the SDK to your new project. Please read [Add the SDK](#add-the-sdk) section
172172 config.license = "DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9"
173173 vc.config = config
174174 }
175- class ViewController: UIViewController {
176- /* CONTINUATION OF CODE FROM STEP 3 AND 4 */
177175 func setup() {
178176 button.backgroundColor = .black
179177 button.setTitle("Scan a Barcode", for: .normal)
@@ -202,7 +200,6 @@ Add the SDK to your new project. Please read [Add the SDK](#add-the-sdk) section
202200 ])
203201 }
204202 }
205- }
206203 ```
207204
208205 > Note:
@@ -221,100 +218,100 @@ Now that the license is configured and the license has been set, it is time to i
221218
222219Each result comes with a ` DSResultStatus ` that can be one of * finished* , * canceled* , or * exception* . The first, * finished* , indicates that the result has been decoded and is available - while * canceled* indicates that the operation has been halted. If * exception* is the result status, then that means that an error has occurred during the barcode detection process. So let us now continue the code of the ` buttonTapped ` method from step 3:
223220
224- <div class =" sample-code-prefix " ></div >
225- > - Objective-C
226- > - Swift
227- >
228- > 1 .
229- ``` objc
230- /* CONTINUATION OF CODE FROM STEP 3 */
231- - (void )buttonTapped {
232- DSBarcodeScannerViewController *vc = [[DSBarcodeScannerViewController alloc] init];
233- DSBarcodeScannerConfig *config = [[DSBarcodeScannerConfig alloc] init];
234- config.license = @"DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9";
235- // some other settings
236- vc.config = config;
237- __weak typeof(self) weakSelf = self;
238- vc.onScannedResult = ^(DSBarcodeScanResult *result) {
239- switch (result.resultStatus) {
240- case DSResultStatusFinished: {
241- dispatch_async (dispatch_get_main_queue (), ^{
242- NSString * format = result.barcodes.firstObject.formatString ?: @"";
243- NSString * text = result.barcodes.firstObject.text ?: @"";
244- weakSelf.label.text = [ NSString stringWithFormat:@"Result:\nFormat: %@\nText: %@", format, text] ;
245- });
246- break;
247- }
248- case DSResultStatusCanceled: {
249- dispatch_async(dispatch_get_main_queue(), ^{
250- weakSelf.label.text = @"Scan canceled";
251- });
252- break;
253- }
254- case DSResultStatusException: {
255- dispatch_async(dispatch_get_main_queue(), ^{
256- weakSelf.label.text = result.errorString;
257- });
258- break;
259- }
260- default:
261- break;
221+ <div class="sample-code-prefix"></div>
222+ > - Objective-C
223+ > - Swift
224+ >
225+ > 1 .
226+ ``` objc
227+ /* CONTINUATION OF CODE FROM STEP 3 */
228+ - (void )buttonTapped {
229+ DSBarcodeScannerViewController * vc = [[ DSBarcodeScannerViewController alloc] init] ;
230+ DSBarcodeScannerConfig * config = [[ DSBarcodeScannerConfig alloc] init] ;
231+ config.license = @"DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9";
232+ // some other settings
233+ vc.config = config;
234+ __ weak typeof(self) weakSelf = self;
235+ vc.onScannedResult = ^(DSBarcodeScanResult * result) {
236+ switch (result.resultStatus) {
237+ case DSResultStatusFinished: {
238+ dispatch_async(dispatch_get_main_queue(), ^{
239+ NSString * format = result.barcodes.firstObject.formatString ?: @"";
240+ NSString * text = result.barcodes.firstObject.text ?: @"";
241+ weakSelf.label.text = [ NSString stringWithFormat:@"Result:\nFormat: %@\nText: %@", format, text] ;
242+ });
243+ break;
262244 }
263- dispatch_async(dispatch_get_main_queue(), ^{
264- [ weakSelf.navigationController popViewControllerAnimated: YES ] ;
265- });
266- };
267- dispatch_async(dispatch_get_main_queue(), ^{
268- weakSelf.navigationController.navigationBar.hidden = YES;
269- [ weakSelf.navigationController pushViewController: vc animated: YES ] ;
270- });
271- }
272- ```
273- 2.
274- ```swift
275- class ViewController: UIViewController {
276- /* CONTINUATION OF CODE FROM STEP 3 */
277- @objc func buttonTapped() {
278- let vc = BarcodeScannerViewController()
279- let config = BarcodeScannerConfig()
280- config.license = "DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9"
281- // some other settings
282- vc.config = config
283- vc.onScannedResult = { [weak self] result in
284- guard let self = self else { return }
285- switch result.resultStatus {
286- /* if the result is valid, display it in the label */
287- case .finished:
288- DispatchQueue.main.async {
289- let format = result.barcodes?.first?.formatString ?? ""
290- self.label.text = "Result:\nFormat: " + (format) + "\n" + "Text: " + (result.barcodes?.first?.text ?? "")
291- }
292- /* if the scan operation is canceled by the user */
293- case .canceled:
294- DispatchQueue.main.async {
295- self.label.text = "Scan canceled"
296- }
297- /* if an error occurs during capture, display the error string in the label */
298- case .exception:
299- DispatchQueue.main.async {
300- self.label.text = result.errorString
301- }
302- @unknown default:
303- break
304- }
305- /* return back to the home page to display the result/cancel message/error string */
306- DispatchQueue.main.async {
307- self.navigationController?.popViewController(animated: true)
308- }
245+ case DSResultStatusCanceled: {
246+ dispatch_async(dispatch_get_main_queue(), ^{
247+ weakSelf.label.text = @"Scan canceled";
248+ });
249+ break;
250+ }
251+ case DSResultStatusException: {
252+ dispatch_async(dispatch_get_main_queue(), ^{
253+ weakSelf.label.text = result.errorString;
254+ });
255+ break;
256+ }
257+ default:
258+ break;
259+ }
260+ dispatch_async(dispatch_get_main_queue(), ^{
261+ [ weakSelf.navigationController popViewControllerAnimated: YES ] ;
262+ });
263+ };
264+ dispatch_async (dispatch_get_main_queue (), ^{
265+ weakSelf.navigationController.navigationBar.hidden = YES;
266+ [ weakSelf.navigationController pushViewController: vc animated: YES ] ;
267+ });
268+ }
269+ ```
270+ 2 .
271+ ``` swift
272+ class ViewController : UIViewController {
273+ /* CONTINUATION OF CODE FROM STEP 3 */
274+ @objc func buttonTapped () {
275+ let vc = BarcodeScannerViewController ()
276+ let config = BarcodeScannerConfig ()
277+ config.license = " DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9"
278+ // some other settings
279+ vc.config = config
280+ vc.onScannedResult = { [weak self ] result in
281+ guard let self = self else { return }
282+ switch result.resultStatus {
283+ /* if the result is valid, display it in the label */
284+ case .finished :
285+ DispatchQueue.main .async {
286+ let format = result.barcodes ? .first ? .formatString ?? " "
287+ self .label .text = " Result:\n Format: " + (format) + " \n " + " Text: " + (result.barcodes ? .first ? .text ?? " " )
288+ }
289+ /* if the scan operation is canceled by the user */
290+ case .canceled :
291+ DispatchQueue.main .async {
292+ self .label .text = " Scan canceled"
293+ }
294+ /* if an error occurs during capture, display the error string in the label */
295+ case .exception :
296+ DispatchQueue.main .async {
297+ self .label .text = result.errorString
298+ }
299+ @unknown default :
300+ break
309301 }
310- /* when the button is clicked, hide the navigation bar and push the newly created BarcodeScannerViewController to the main view */
302+ /* return back to the home page to display the result/cancel message/error string */
311303 DispatchQueue.main .async {
312- self.navigationController?.navigationBar.isHidden = true
313- self.navigationController?.pushViewController(vc, animated: true)
304+ self .navigationController ? .popViewController (animated : true )
314305 }
315- }
306+ }
307+ /* when the button is clicked, hide the navigation bar and push the newly created BarcodeScannerViewController to the main view */
308+ DispatchQueue.main .async {
309+ self .navigationController ? .navigationBar .isHidden = true
310+ self .navigationController ? .pushViewController (vc, animated : true )
311+ }
316312 }
317- ```
313+ }
314+ ```
318315
319316## Step 5: Configure the Barcode Scanner (optional)
320317
0 commit comments