Forum Archive

How to add a camera or microphone to wkwebview by permisions

roku_one

hellow, how i can give permission to wkwebview. In safari they ask for it. I test this script :https://addpipe.com/simple-recorderjs-demo/
Also i know the info.plist admit the permision.

cvp

@roku_one I don't know if it is possible but you could try with SFSafariViewController


import ui
from objc_util import *

from flask import Flask
import threading
import os

app = Flask(__name__)

@app.route('/')
def entry_point():
    global local_html
    fil = os.path.expanduser('~/Documents/'+local_html)
    #print(fil)
    data = open(fil).read()
    return data 

def safariViewControllerDidFinish_(_self, _cmd, _controller):
    #print('SafariViewControllerDidFinish_')
    SFSafariViewController = ObjCInstance(_controller)
    #print(SFSafariViewController.uiview)
    try:
        SFSafariViewController.uiview.close()
    except Exception as e:
        SFSafariViewController.uiview().close()

methods = [safariViewControllerDidFinish_,]
protocols = ['SFSafariViewControllerDelegate']
try:
        MySFSafariViewControllerDelegate = ObjCClass('MySFSafariViewControllerDelegate')
except:
    MySFSafariViewControllerDelegate = create_objc_class('MySFSafariViewControllerDelegate', methods=methods, protocols=protocols)

@on_main_thread 
def MySFSafariViewController(url, w, h, mode='sheet', popover_location=None):
    uiview = ui.View()
    uiview.frame = (0,0,w,h)
    uiview.background_color = 'white'
    if mode == 'sheet':
        uiview.present('sheet',hide_title_bar=True)
    elif mode == 'popover':
        if popover_location:
            uiview.present('popover', hide_title_bar=True, popover_location=popover_location)
        else:
            return
    else:
        return

    #SFSafariViewController = ObjCClass('SFSafariViewController').alloc().initWithURL_entersReaderIfAvailable_(url,True)
    SFSafariViewControllerConfiguration = ObjCClass('SFSafariViewControllerConfiguration').alloc().init()
    SFSafariViewControllerConfiguration.setEntersReaderIfAvailable_(True)
    SFSafariViewController = ObjCClass('SFSafariViewController').alloc().initWithURL_configuration_(url,SFSafariViewControllerConfiguration)
    #print(dir(SFSafariViewController))

    # Use new delegate class:
    delegate = MySFSafariViewControllerDelegate.alloc().init()
    SFSafariViewController.delegate = delegate      
    SFSafariViewController.setModalPresentationStyle_(3)
    SFSafariViewController.uiview   = uiview        # used by delegate

    objc_uiview = ObjCInstance(uiview)
    SUIViewController = ObjCClass('SUIViewController')
    vc = SUIViewController.viewControllerForView_(objc_uiview)  

    vc.presentViewController_animated_completion_(SFSafariViewController, True, None)
    #print(dir(SFSafariViewController))
    #print(dir(SFSafariViewController.view()))

def main(url,local=None):
    global local_html
    # demo code
    #mv = ui.View()
    #mv.background_color = 'white'
    #mv.name = 'Test SFSafariViewController'
    #mv.present()
    ns_url = nsurl(url)
    if 'http://127.0.0.1:5000/' in url:
        # local html
        local_html = local
        threads = threading.enumerate()
        already = False
        for thread in threads:
            if thread.name  == 'flask':
                already = True
                break
        if not already:
            # run Flask in separate thread
            threading.Thread(target=app.run,name='flask').start()
    MySFSafariViewController(ns_url,600,500)
    #MySFSafariViewController(url,600,500, mode ='popover', popover_location=(mv.width-40,60))

if __name__ == '__main__':  
    #main('https://www.macstories.net/stories/my-must-have-ios-apps-2018-edition/2/')
    main('https://addpipe.com/simple-recorderjs-demo/')
    #main('http://www.google.be')
    #main('http://127.0.0.1:5000/'  ,local='MesTests/test.html')
    #main('http://127.0.0.1:5000/'  ,local='MesTests/test.jpg')
stephen

@roku_one

cvp's script should do it