0

Using swift 4 and IOS 12 ,i am using a webViewKit to load a website. One of the features of the web app is to scan qrcode, i have successfully loaded the website but i am still struggling how to access the device camera to scan qr code, in android everything works fine including this qr code scanner using webView onPermissionRequest method. I have already tried this Key : Privacy - Camera Usage Description in info.plist and also adding these lines of codes here AVCaptureDevice.authorizationStatus(for: .video) on url load before pressing the web button to scan qr code. According to our web developer this button press executes a javascript undefined function, but i don't know how to make webViewKit respond to this button click event and the hardest part is that there are no logs or NSLogs to evaluate. I am new to IOS development for about almost a month. Thank you

var WebCodeCamJS = function(element) {
'use strict';
this.Version = {
    name: 'WebCodeCamJS',
    version: '2.7.0',
    author: 'Tóth András',
};
var mediaDevices = window.navigator.mediaDevices;
mediaDevices.getUserMedia = function(c) {
    return new Promise(function(y, n) {
        (window.navigator.getUserMedia || window.navigator.mozGetUserMedia || window.navigator.webkitGetUserMedia).call(navigator, c, y, n);
    });
}

HTMLVideoElement.prototype.streamSrc = ('srcObject' in HTMLVideoElement.prototype) ? function(stream) {
    this.srcObject = !!stream ? stream : null;
} : function(stream) {
    if (!!stream) {
        this.src = (window.URL || window.webkitURL).createObjectURL(stream);
    } else {
        this.removeAttribute('src');
    }
};

Above code is a js function named webcodecamjs.js that is being executed from a button click event on the web page. I found similar problem with solution here , but i'm not quite sure how to implement it, it says

  1. Now Add JS file (WebRTC.js) that defines various WebRTC classes, functions & passes the calls to the WKWebView.
  2. In the WKWebView inject the script at the document start:

where do i put this WebRTC.js file? what i did was to create a new file in my ios project and named it WebRTC.js , i also tried renaming it to webcodecamjs.js like what we have in web js files, how i did it in func viewDidAppear

let contentController = WKUserContentController()

    contentController.add(self, name: "callbackHandler")
    let script = try! String(contentsOf: Bundle.main.url(forResource: "webcodecamjs", withExtension: "js")!, encoding: String.Encoding.utf8)
    contentController.addUserScript(WKUserScript(source: script, injectionTime: WKUserScriptInjectionTime.atDocumentStart, forMainFrameOnly: true))
    let preferences = WKPreferences()
    preferences.javaScriptEnabled = true

    let configuration = WKWebViewConfiguration()
    configuration.preferences = preferences
    configuration.userContentController = contentController
    configuration.allowsPictureInPictureMediaPlayback = true


     webView.navigationDelegate = self
    webView.configuration.preferences = configuration.preferences
    WKWebView.load(webView)(NSURLRequest(url: NSURL(string: encodedLoginUrl)! as URL) as URLRequest)

webView is a

@IBOutlet weak var webView: WKWebView

not declared as below

var webView: WKWebView

that's why i can't do something like below, cause it gives me 'weak' warning

webView = WKWebView(frame: CGRect.zero, configuration: config)

  • Check this link: https://stackoverflow.com/questions/11896985/identify-submit-button-click-of-uiwebview – Mahak Mittal Jan 15 '19 at 05:56
  • @MahakMittal i appreciate your response , i have already visited that site before and tried to implement those accepted answers but unfortunately some of the answers are not written in swift , there is one there but it is not supported in ios 12 anymore. I also tried this [https://stackoverflow.com/questions/37509990/migrating-from-uiwebview-to-wkwebview] but still no luck.. – Peter Mora Jan 15 '19 at 06:45

1 Answers1

0

Could you add code snippet to check?

Mahak Mittal
  • 121
  • 1
  • 10
  • You don't need to add js file in your code. You just need to add following function : func webView(webView:UIWebView, shouldStartLoadWithRequest request:NSURLRequest, navigationType:UIWebViewNavigationType) -> Bool { let u = request.mainDocumentURL print("local or remote url was " ,u) return true // allow the form to in fact send the form } This will allow you to get the action of submit button – Mahak Mittal Jan 15 '19 at 10:23
  • In the url you can check for particular action and can do whatever you want. – Mahak Mittal Jan 15 '19 at 10:26
  • it gave me warning 'UIWebView' was deprecated in iOS 12.0: No longer supported; please adopt WKWebView.' , how ever there is a migration from shouldStartLoadWithRequest here [https://stackoverflow.com/questions/37509990/migrating-from-uiwebview-to-wkwebview] , but still no response in my logs, after i click the button the method is not executed – Peter Mora Jan 17 '19 at 05:47
  • Does the code working fine with UIWebview? If you do not convert it with WKWebView. – Mahak Mittal Jan 17 '19 at 07:11
  • No it doesn't work either .. i think what i really need to figure out is how am i going to make WKWebView listen for onclick events that involves javascript. If i catch that event or receive the onclick event thats the time i can figure out when to invoke user permission pop up to open the device camera and scan the qr code – Peter Mora Jan 17 '19 at 08:52
  • by the way i am now using WKWebView and set to viewController view like self.view = webView – Peter Mora Jan 17 '19 at 08:55