Quick Start

更新时间:
复制 MD 格式

This guide shows how to add the Code Scanner SDK to an iOS project that already uses CocoaPods and mPaaS. The SDK supports QR codes, barcodes, and multi-code detection.

Two integration paths are available — choose the one that matches your mPaaS baseline:

  • Baseline 10.1.68.17 or later — uses the MPScanCodeAdapterInterface adapter API

  • Earlier than 10.1.68.17 — uses TBScanViewController directly

    Prerequisites

    Before you begin, ensure that you have:

    Add the SDK

    1. In your Podfile, add the Code Scanner dependency:

      mPaaS_pod "mPaaS_ScanCode"

    2. Run pod install to install the SDK.

Use the SDK (10.1.68.17 or later)

The following steps use the adapter API introduced in baseline 10.1.68.17. Full working examples are in the official Code Scanner demo.

Note

Multi-code detection is supported only in the standard interface.

  1. Call the standard scan page and handle the result in the callback.

      @interface MPScanDemoVC()<TBScanViewControllerDelegate>
      @property(nonatomic, strong) TBScanViewController *scanVC;
      @end
     - (void)defaultScan {
        TBScanViewController *vc = [[MPScanCodeAdapterInterface sharedInstance] createDefaultScanPageWithallback:^(id  _Nonnull result, BOOL keepAlive) {
    0000000000000000        // Process the scan result
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:result[@"resp_result"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
             alert.tag = 1999;
             [alert show];
         }];
         [self.navigationController pushViewController:vc animated:YES];
         self.scanVC =  vc;
     }
  2. To let users scan again after viewing a result, call resumeScan when they dismiss the alert.

     - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
         // Scan continuously
         [self.scanVC resumeScan];
     }

Use the SDK (earlier than 10.1.68.17)

The following steps use TBScanViewController directly. Full working examples are in the official Code Scanner demo.

  1. Initialize and push the scan view controller.

     @interface MPScanDemoVC()<TBScanViewControllerDelegate>
     @property(nonatomic, strong) TBScanViewController *scanVC;
     @end
     - (void)startDefauleScanViewController
     {
         TBScanViewController *vc = [[TBScanViewController alloc] init];
         vc.scanType = ScanType_All_Code;
         vc.delegate = self;
         [self.navigationController pushViewController:vc animated:YES];
         self.scanVC = vc;
     }
  2. Implement the delegate method to receive results. The result is delivered on a background thread — switch to the main thread before updating the UI.

     #pragma mark Process the scan result
     -(void)didFind:(NSArray<TBScanResult*>*)resultArray
     {
         if([resultArray count] > 0) {
             TBScanResult *result = resultArray.firstObject;
             NSString* content = result.data;
    
             dispatch_async(dispatch_get_main_queue(), ^{
                 // Note: The scan result is returned in a subthread. Switch to the main thread for UI-related operations.
                 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:content delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                 [alert show];
             });
         }
     }
  3. To let users scan again after viewing a result, call resumeScan when they dismiss the alert.

     #pragma mark alert
     - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
         [self.scanVC resumeScan];
     }