Skip to content

Commit 5e968d5

Browse files
committed
update ios webview sample; update readme
1 parent 9991f7a commit 5e968d5

File tree

25 files changed

+375
-611
lines changed

25 files changed

+375
-611
lines changed

1.hello-world/14.read-video-webview/android/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Hello-world for WebView - Dynamsoft Barcode Reader Sample
1+
# Hello-world for Android WebView - Dynamsoft Barcode Reader Sample
22

33
This sample demonstrates how to use the [Dynamsoft Barcode Reader](https://www.dynamsoft.com/barcode-reader/overview/) JS Edition in Android.
44

@@ -26,7 +26,7 @@ Don't forget to load this page using webview in your Android project.
2626

2727
Copy the file 'DBRWebViewHelper.java' in the sample to your Android project.
2828

29-
This file provides a class: `DBRWebViewHelper`, which will make it very convenient to let the javascript code in your WebView use DBR Android by providing some methods.
29+
This file provides a class: `DBRWebViewHelper`, which will make it very convenient to let the java code use DBR JS by providing some methods.
3030

3131
```java
3232
DBRWebViewHelper dbrWebViewHelper = new DBRWebViewHelper();
@@ -88,8 +88,8 @@ public class WebAppInterface {
8888
`DBRWebViewHelper` has an `evaluateJavascript()` method, which executes javascript code through `WebView.evaluateJavascript()`, you can use it to complete the interaction like the following code.
8989

9090
```java
91-
public void stopScanner() {
92-
evaluateJavascript("stopScanner()");
91+
public void startScanner() {
92+
evaluateJavascript("startScanner()");
9393
}
9494
```
9595

1.hello-world/14.read-video-webview/android/app/src/main/assets/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<meta name="viewport" content="width=device-width,initial-scale=1.0">
77
<meta name="description" content="Quickly read barcodes with Dynamsoft Barcode Reader from a live camera stream.">
88
<meta name="keywords" content="camera based barcode reading">
9-
<link rel="canonical" href="https://demo.dynamsoft.com/Samples/DBR/JS/1.hello-world/14.read-video-webview.html">
9+
<link rel="canonical" href="https://demo.dynamsoft.com/Samples/DBR/JS/1.hello-world/14.read-video-webview">
1010
<title>Dynamsoft Barcode Reader Sample - Read Video WebView(Android)</title>
1111
</head>
1212

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Hello-world for iOS WKWebView - Dynamsoft Barcode Reader Sample
2+
3+
This sample demonstrates how to use the [Dynamsoft Barcode Reader](https://www.dynamsoft.com/barcode-reader/overview/) JS Edition in iOS(Swift).
4+
5+
If you want to learn how to use the Android Edition SDK in javascript, you can check [iOS WebView Barcode Scanning](https://github.com/Dynamsoft/barcode-reader-mobile-samples/tree/main/ios/JavaScript/WebViewBarcodeScanning).
6+
7+
## Get Started
8+
9+
### 1. Get your web page ready
10+
11+
Follow this [Guide](https://www.dynamsoft.com/barcode-reader/docs/web/programming/javascript/user-guide/?ver=latest#building-your-own-page) and build your page, including importing the SDK and initializing the certificate, etc.
12+
13+
Then you can define some functions according to your needs, like the code below, we will use it later:
14+
15+
```javascript
16+
async function startScanner() {
17+
let scanner = await Dynamsoft.DBR.BarcodeScanner.createInstance();
18+
await scanner.setUIElement(document.getElementById('div-ui-container'));
19+
await scanner.show();
20+
}
21+
```
22+
23+
Don't forget to load this page using wkWebView in your iOS project.
24+
25+
### 2. Add DBRWKWebViewHelper
26+
27+
Creat the file 'DBRWKWebViewHelper.swift' in the sample to your iOS project, and copy the DBRWKWebViewHelper's code in the sample to this file.
28+
29+
This file provides a class: `DBRWKWebViewHelper`, which will make it very convenient to let the swift code use DBR JS by providing some methods.
30+
31+
### 3. Pollute your WKWebView
32+
33+
Class `DBRWKWebViewHelper` provides a method `pollute`, which will inject a global variable `webkit` into the js code in your WKWebView.
34+
35+
```swift
36+
DBRWKWebViewHelper().pollute(wkWebView);
37+
```
38+
39+
The variable is an object that has some methods to communicate with native code, you can call them directly like this:
40+
41+
```javascript
42+
// e.g. callback after decoding, this will execute our corresponding java code in DBRWKWebViewHelper
43+
scanner.onUniqueRead = (txt, result) => {
44+
const format = result.barcodeFormatString;
45+
window.webkit.messageHandlers.onUniqueRead.postMessage(format + " " + txt);
46+
};
47+
```
48+
49+
For more detailes about webkit: [Apple Developer Documentation](https://developer.apple.com/documentation/webkit/wkscriptmessagehandler).
50+
51+
## Customize DBRWKWebViewHelper
52+
53+
**Notice: when you change variables or methods, you should modify both native code and JS code to avoid errors.**
54+
55+
### 1. Change the methods to be injected
56+
57+
All methods are injected in the method `pollute`, and defined in the method `userContentController` . You have to modify both of them.
58+
59+
```swift
60+
// e.g.
61+
// func pollute(wkWebView: WKWebView)
62+
let userContentController = configuration.userContentController
63+
// The parameter name is the name of the injected message handler.
64+
// https://developer.apple.com/documentation/webkit/wkusercontentcontroller/1537172-add
65+
userContentController.add(self, name: "onUniqueRead")
66+
userContentController.add(self, name: "onWasmLoaded")
67+
wkWebView.configuration.userContentController = userContentController
68+
69+
// extension DBRWKWebViewHelper: WKScriptMessageHandler
70+
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
71+
// message.name is the name of the function called in JS.
72+
// message.body is the parameter passed when calling.
73+
// Both of them are of type string.
74+
switch message.name {
75+
case "onUniqueRead":
76+
// your code here
77+
case "onWasmLoaded":
78+
// your code here
79+
default:
80+
// your code here
81+
}
82+
}
83+
```
84+
85+
### 2. Execute JS code
86+
87+
WKWebView natively has a method `WKWebView.evaluateJavascript()`, you can use it to complete the interaction like the following code.
88+
89+
```swift
90+
func startScanner() {
91+
wkWebView.evaluateJavaScript("startScanner()")
92+
}
93+
```
94+
95+
Then, the following code will execute the function `startScanner()` we just defined in javascript code.
96+
97+
```java
98+
dbrWKWebViewHelper.startScanner();
99+
```

1.hello-world/14.read-video-webview/ios/WebViewBarcodeScanning.xcodeproj/project.pbxproj renamed to 1.hello-world/14.read-video-webview/ios/ReadVideoWKWebview.xcodeproj/project.pbxproj

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
/* Begin PBXBuildFile section */
1010
68306D50294073E500020E5B /* html in Resources */ = {isa = PBXBuildFile; fileRef = 68306D4F294073E500020E5B /* html */; };
11+
683A144029C19A4300C80931 /* DynamsoftBarcodeReader.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 683A143F29C19A4300C80931 /* DynamsoftBarcodeReader.framework */; };
12+
683A144129C19A4300C80931 /* DynamsoftBarcodeReader.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 683A143F29C19A4300C80931 /* DynamsoftBarcodeReader.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
13+
683A144329C19A4B00C80931 /* DynamsoftCameraEnhancer.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 683A144229C19A4A00C80931 /* DynamsoftCameraEnhancer.framework */; };
14+
683A144429C19A4B00C80931 /* DynamsoftCameraEnhancer.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 683A144229C19A4A00C80931 /* DynamsoftCameraEnhancer.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
1115
68F2F6762938471F00318579 /* DBRWKWebViewHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68F2F6752938471F00318579 /* DBRWKWebViewHelper.swift */; };
1216
C42699C7262579AF00DA60E9 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C42699C6262579AF00DA60E9 /* AppDelegate.swift */; };
1317
C42699CB262579AF00DA60E9 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C42699CA262579AF00DA60E9 /* ViewController.swift */; };
@@ -23,6 +27,8 @@
2327
dstPath = "";
2428
dstSubfolderSpec = 10;
2529
files = (
30+
683A144129C19A4300C80931 /* DynamsoftBarcodeReader.framework in Embed Frameworks */,
31+
683A144429C19A4B00C80931 /* DynamsoftCameraEnhancer.framework in Embed Frameworks */,
2632
);
2733
name = "Embed Frameworks";
2834
runOnlyForDeploymentPostprocessing = 0;
@@ -31,50 +37,61 @@
3137

3238
/* Begin PBXFileReference section */
3339
68306D4F294073E500020E5B /* html */ = {isa = PBXFileReference; lastKnownFileType = folder; path = html; sourceTree = "<group>"; };
40+
683A143F29C19A4300C80931 /* DynamsoftBarcodeReader.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = DynamsoftBarcodeReader.framework; sourceTree = "<group>"; };
41+
683A144229C19A4A00C80931 /* DynamsoftCameraEnhancer.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = DynamsoftCameraEnhancer.framework; sourceTree = "<group>"; };
3442
68F2F6752938471F00318579 /* DBRWKWebViewHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DBRWKWebViewHelper.swift; sourceTree = "<group>"; };
35-
C42699C3262579AF00DA60E9 /* WebViewBarcodeScanning.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = WebViewBarcodeScanning.app; sourceTree = BUILT_PRODUCTS_DIR; };
43+
C42699C3262579AF00DA60E9 /* ReadVideoWKWebview.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ReadVideoWKWebview.app; sourceTree = BUILT_PRODUCTS_DIR; };
3644
C42699C6262579AF00DA60E9 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
3745
C42699CA262579AF00DA60E9 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
3846
C42699CD262579AF00DA60E9 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
3947
C42699CF262579B300DA60E9 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
4048
C42699D2262579B300DA60E9 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
4149
C42699D4262579B300DA60E9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
42-
C45B6DF2267CA8D100D6FCC3 /* DBRSample_Bridging_Header.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DBRSample_Bridging_Header.h; sourceTree = "<group>"; };
4350
/* End PBXFileReference section */
4451

4552
/* Begin PBXFrameworksBuildPhase section */
4653
C42699C0262579AF00DA60E9 /* Frameworks */ = {
4754
isa = PBXFrameworksBuildPhase;
4855
buildActionMask = 2147483647;
4956
files = (
57+
683A144029C19A4300C80931 /* DynamsoftBarcodeReader.framework in Frameworks */,
58+
683A144329C19A4B00C80931 /* DynamsoftCameraEnhancer.framework in Frameworks */,
5059
);
5160
runOnlyForDeploymentPostprocessing = 0;
5261
};
5362
/* End PBXFrameworksBuildPhase section */
5463

5564
/* Begin PBXGroup section */
65+
683A143E29C19A4200C80931 /* Frameworks */ = {
66+
isa = PBXGroup;
67+
children = (
68+
683A144229C19A4A00C80931 /* DynamsoftCameraEnhancer.framework */,
69+
683A143F29C19A4300C80931 /* DynamsoftBarcodeReader.framework */,
70+
);
71+
name = Frameworks;
72+
sourceTree = "<group>";
73+
};
5674
C42699BA262579AF00DA60E9 = {
5775
isa = PBXGroup;
5876
children = (
59-
C42699C5262579AF00DA60E9 /* WebViewBarcodeScanning */,
77+
C42699C5262579AF00DA60E9 /* ReadVideoWKWebview */,
6078
C42699C4262579AF00DA60E9 /* Products */,
61-
C42699DC26257A2C00DA60E9 /* Frameworks */,
79+
683A143E29C19A4200C80931 /* Frameworks */,
6280
);
6381
sourceTree = "<group>";
6482
};
6583
C42699C4262579AF00DA60E9 /* Products */ = {
6684
isa = PBXGroup;
6785
children = (
68-
C42699C3262579AF00DA60E9 /* WebViewBarcodeScanning.app */,
86+
C42699C3262579AF00DA60E9 /* ReadVideoWKWebview.app */,
6987
);
7088
name = Products;
7189
sourceTree = "<group>";
7290
};
73-
C42699C5262579AF00DA60E9 /* WebViewBarcodeScanning */ = {
91+
C42699C5262579AF00DA60E9 /* ReadVideoWKWebview */ = {
7492
isa = PBXGroup;
7593
children = (
7694
68306D4F294073E500020E5B /* html */,
77-
C45B6DF2267CA8D100D6FCC3 /* DBRSample_Bridging_Header.h */,
7895
C42699C6262579AF00DA60E9 /* AppDelegate.swift */,
7996
C42699CA262579AF00DA60E9 /* ViewController.swift */,
8097
68F2F6752938471F00318579 /* DBRWKWebViewHelper.swift */,
@@ -83,22 +100,15 @@
83100
C42699D1262579B300DA60E9 /* LaunchScreen.storyboard */,
84101
C42699D4262579B300DA60E9 /* Info.plist */,
85102
);
86-
path = WebViewBarcodeScanning;
87-
sourceTree = "<group>";
88-
};
89-
C42699DC26257A2C00DA60E9 /* Frameworks */ = {
90-
isa = PBXGroup;
91-
children = (
92-
);
93-
name = Frameworks;
103+
path = ReadVideoWKWebview;
94104
sourceTree = "<group>";
95105
};
96106
/* End PBXGroup section */
97107

98108
/* Begin PBXNativeTarget section */
99-
C42699C2262579AF00DA60E9 /* WebViewBarcodeScanning */ = {
109+
C42699C2262579AF00DA60E9 /* ReadVideoWKWebview */ = {
100110
isa = PBXNativeTarget;
101-
buildConfigurationList = C42699D7262579B300DA60E9 /* Build configuration list for PBXNativeTarget "WebViewBarcodeScanning" */;
111+
buildConfigurationList = C42699D7262579B300DA60E9 /* Build configuration list for PBXNativeTarget "ReadVideoWKWebview" */;
102112
buildPhases = (
103113
C42699BF262579AF00DA60E9 /* Sources */,
104114
C42699C0262579AF00DA60E9 /* Frameworks */,
@@ -109,9 +119,9 @@
109119
);
110120
dependencies = (
111121
);
112-
name = WebViewBarcodeScanning;
122+
name = ReadVideoWKWebview;
113123
productName = WebViewBarcodeScanningSwift;
114-
productReference = C42699C3262579AF00DA60E9 /* WebViewBarcodeScanning.app */;
124+
productReference = C42699C3262579AF00DA60E9 /* ReadVideoWKWebview.app */;
115125
productType = "com.apple.product-type.application";
116126
};
117127
/* End PBXNativeTarget section */
@@ -128,7 +138,7 @@
128138
};
129139
};
130140
};
131-
buildConfigurationList = C42699BE262579AF00DA60E9 /* Build configuration list for PBXProject "WebViewBarcodeScanning" */;
141+
buildConfigurationList = C42699BE262579AF00DA60E9 /* Build configuration list for PBXProject "ReadVideoWKWebview" */;
132142
compatibilityVersion = "Xcode 9.3";
133143
developmentRegion = en;
134144
hasScannedForEncodings = 0;
@@ -141,7 +151,7 @@
141151
projectDirPath = "";
142152
projectRoot = "";
143153
targets = (
144-
C42699C2262579AF00DA60E9 /* WebViewBarcodeScanning */,
154+
C42699C2262579AF00DA60E9 /* ReadVideoWKWebview */,
145155
);
146156
};
147157
/* End PBXProject section */
@@ -319,18 +329,18 @@
319329
FRAMEWORK_SEARCH_PATHS = (
320330
"$(inherited)",
321331
../../../Frameworks/,
322-
"$(PROJECT_DIR)/WebViewBarcodeScanning",
332+
"$(PROJECT_DIR)/ReadVideoWKWebview",
323333
"$(PROJECT_DIR)",
324334
);
325-
INFOPLIST_FILE = WebViewBarcodeScanning/Info.plist;
335+
INFOPLIST_FILE = ReadVideoWKWebview/Info.plist;
326336
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
327337
LD_RUNPATH_SEARCH_PATHS = (
328338
"$(inherited)",
329339
"@executable_path/Frameworks",
330340
);
331-
PRODUCT_BUNDLE_IDENTIFIER = com.Dynamsoft.WebViewBarcodeScanning;
332-
PRODUCT_NAME = WebViewBarcodeScanning;
333-
SWIFT_OBJC_BRIDGING_HEADER = WebViewBarcodeScanning/DBRSample_Bridging_Header.h;
341+
PRODUCT_BUNDLE_IDENTIFIER = com.Dynamsoft.ReadVideoWKWebview;
342+
PRODUCT_NAME = ReadVideoWKWebview;
343+
SWIFT_OBJC_BRIDGING_HEADER = "";
334344
SWIFT_VERSION = 4.2;
335345
TARGETED_DEVICE_FAMILY = 1;
336346
VALIDATE_WORKSPACE = YES;
@@ -347,18 +357,18 @@
347357
FRAMEWORK_SEARCH_PATHS = (
348358
"$(inherited)",
349359
../../../Frameworks/,
350-
"$(PROJECT_DIR)/WebViewBarcodeScanning",
360+
"$(PROJECT_DIR)/ReadVideoWKWebview",
351361
"$(PROJECT_DIR)",
352362
);
353-
INFOPLIST_FILE = WebViewBarcodeScanning/Info.plist;
363+
INFOPLIST_FILE = ReadVideoWKWebview/Info.plist;
354364
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
355365
LD_RUNPATH_SEARCH_PATHS = (
356366
"$(inherited)",
357367
"@executable_path/Frameworks",
358368
);
359-
PRODUCT_BUNDLE_IDENTIFIER = com.Dynamsoft.WebViewBarcodeScanning;
360-
PRODUCT_NAME = WebViewBarcodeScanning;
361-
SWIFT_OBJC_BRIDGING_HEADER = WebViewBarcodeScanning/DBRSample_Bridging_Header.h;
369+
PRODUCT_BUNDLE_IDENTIFIER = com.Dynamsoft.ReadVideoWKWebview;
370+
PRODUCT_NAME = ReadVideoWKWebview;
371+
SWIFT_OBJC_BRIDGING_HEADER = "";
362372
SWIFT_VERSION = 4.2;
363373
TARGETED_DEVICE_FAMILY = 1;
364374
VALIDATE_WORKSPACE = YES;
@@ -368,7 +378,7 @@
368378
/* End XCBuildConfiguration section */
369379

370380
/* Begin XCConfigurationList section */
371-
C42699BE262579AF00DA60E9 /* Build configuration list for PBXProject "WebViewBarcodeScanning" */ = {
381+
C42699BE262579AF00DA60E9 /* Build configuration list for PBXProject "ReadVideoWKWebview" */ = {
372382
isa = XCConfigurationList;
373383
buildConfigurations = (
374384
C42699D5262579B300DA60E9 /* Debug */,
@@ -377,7 +387,7 @@
377387
defaultConfigurationIsVisible = 0;
378388
defaultConfigurationName = Release;
379389
};
380-
C42699D7262579B300DA60E9 /* Build configuration list for PBXNativeTarget "WebViewBarcodeScanning" */ = {
390+
C42699D7262579B300DA60E9 /* Build configuration list for PBXNativeTarget "ReadVideoWKWebview" */ = {
381391
isa = XCConfigurationList;
382392
buildConfigurations = (
383393
C42699D8262579B300DA60E9 /* Debug */,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import UIKit
2+
3+
@UIApplicationMain
4+
class AppDelegate: UIResponder, UIApplicationDelegate {
5+
6+
var window: UIWindow?
7+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
8+
9+
if #available(iOS 15.0, *) {
10+
let app = UINavigationBarAppearance()
11+
app.configureWithOpaqueBackground()
12+
app.titleTextAttributes = [
13+
NSAttributedString.Key.foregroundColor: UIColor.black
14+
]
15+
app.backgroundColor = UIColor.white
16+
17+
UINavigationBar.appearance().scrollEdgeAppearance = app
18+
UINavigationBar.appearance().standardAppearance = app
19+
20+
}
21+
22+
// Override point for customization after application launch.
23+
return true
24+
}
25+
26+
}
27+
28+
extension UIResponder {
29+
var parentViewController: UIViewController? {
30+
return next as? UIViewController ?? next?.parentViewController
31+
}
32+
}

1.hello-world/14.read-video-webview/ios/WebViewBarcodeScanning/Assets.xcassets/AppIcon.appiconset/Contents.json renamed to 1.hello-world/14.read-video-webview/ios/ReadVideoWKWebview/Assets.xcassets/AppIcon.appiconset/Contents.json

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)