Flutter Barcode SDK for iOS and macOS

The Flutter barcode SDK series is coming to the end. This week, we switch the coding environment to macOS to finish the rest of the plugin platforms — iOS and MacOS — using Swift.

The Series of Flutter Barcode SDK

Download

30-day FREE Trial

https://www.dynamsoft.com/customer/license/trialLicense/?product=dbr

Flutter Barcode Plugin for iOS

As always, we add the iOS template to the existing plugin project first:

The command generates a flutter_barcode_sdk.podspec file for build configuration and a Classes folder containing code implementation.

You can either set vendored_frameworks or dependency for linking in flutter_barcode_sdk.podspec. The difference is vendored_frameworks are the paths of the framework bundles shipped with the plugin, whereas setting dependency triggers pod install if the framework is not locally cached. If you have trouble in building the project with dependency, vendored_frameworks could be the alternative. The only problem of using vendored_frameworks is you cannot publish the package to pub.dev if the package size is over 100 MB.

Next, we open Classes/SwiftFlutterBarcodeSdkPlugin.swift file to start coding.

Here is the code snippet for importing and initializing Dynamsoft Barcode Reader:

The organization ID 200001 authorizes developers to use the SDK for 7 days.

The public func handle() function is used to handle Flutter method call:

We can implement the corresponding methods as follows:

Before testing the plugin, we have to open example/ios/Runner.xcworkspace in Xcode to configure the signing certificate. After that, we can run flutter run in terminal to launch the barcode scanner demo app. No extra Flutter code needed, because the Dart code for UI is shared between Android and iOS:

Flutter Barcode Plugin for macOS

Once the Flutter barcode plugin is done for iOS, developing the plugin for macOS is much easier. Probably you have noticed that there is no framework for macOS but only dylib. To utilize the barcode SDK for macOS, we need to create a bridging header.

Here are the steps:

  1. Add the Flutter macOS template to the current plugin project:

2. Create a bridging header macos/Runner/Runner-Bridging-Header.h:

3. Set the path of the bridging header in Xcode:

4. Copy the Swift code from ios/Classes/SwiftFlutterBarcodeSdkPlugin.swift to macos/Classes/FlutterBarcodeSdkPlugin.swift.

Although the macOS application can now run successfully, it will fail to access files due to the sandbox restriction. To enable loadings file via file path string, we disable the entitlement com.apple.security.app-sandbox in example/macos/Runner/DebugProfile.entitlements:

Finally, the Flutter macOS barcode reader can work perfectly.

Ending

So far, the Flutter barcode SDK has covered Windows, Linux, macOS, iOS, Android, and web. It will also support Fuchsia OS in the near future.

Source Code

https://github.com/yushulx/flutter_barcode_sdk

Originally published at https://www.dynamsoft.com on May 26, 2021.

Manager of Dynamsoft Open Source Projects | Tech Lover