question

AmrKamal-2391 avatar image
0 Votes"
AmrKamal-2391 asked WenyanZhang-MSFT commented

Xamarin WebRTC in IOS WebView not working

I'm trying to integrate webrtc into Xamarin Forms app using webview Android is working fine but in IOS streaming camera not working . I think the problem in IOS WebView render here's my render

public class HybridWebViewRenderer : WkWebViewRenderer, IWKScriptMessageHandler
{
public HybridWebViewRenderer() : this(new WKWebViewConfiguration())
{
}

      public HybridWebViewRenderer(WKWebViewConfiguration config) : base(config)
      {
          config.AllowsInlineMediaPlayback = true;
          userController = config.UserContentController;
          var script = new WKUserScript(new NSString(JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false);
          userController.AddUserScript(script);
          userController.AddScriptMessageHandler(this, "invokeAction");
      }
      protected override void OnElementChanged(VisualElementChangedEventArgs e)
      {
          base.OnElementChanged(e);
          if (e.OldElement != null)
          {
              userController.RemoveAllUserScripts();
              userController.RemoveScriptMessageHandler("invokeAction");
              GenericWebView hybridWebView = e.OldElement as GenericWebView;
              hybridWebView.Cleanup();
          }
          if (e.NewElement != null)
          {
              //{((GenericWebView)Element).Uri}
              string filename = Path.Combine(NSBundle.MainBundle.BundlePath, $"/test.html");
              LoadRequest(new NSUrlRequest(new NSUrl(filename, false)));
          }
      }
      public void DidReceiveScriptMessage(WKUserContentController userContentController, WKScriptMessage message)
      {
          ((GenericWebView)Element).InvokeAction(message.Body.ToString());
      }
  }

How to fix this issue ?


dotnet-xamarin
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You could check the camera and photo permission in your info.plist : Privacy - Camera Usage Description, Privacy - Photo Library Usage Description. But I'm not sure how it doesn't work, if the app crashed, it is mainly related to permission. If the screen is black, you can try to check your html file and check App Transport Security in Xamarin.iOS. Finally, I'm not sure if AllowsInlineMediaPlayback has been correctly set to your webview, try to replace WkWebViewRenderer by ViewRenderer and set AllowsInlineMediaPlayback.

 using WebKit;
 [assembly: ExportRenderer(typeof(WebView), typeof(MyWebViewRenderer))]
 namespace WebViewRendererDemo.iOS
 {
     public class MyWebViewRenderer : ViewRenderer<WebView, WKWebView>
     {
         WKWebView wkWebView;
         protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
         {
             base.OnElementChanged(e);
             if (Control == null)
             {
                 WKWebViewConfiguration configuration = new WKWebViewConfiguration();
                 configuration.AllowsInlineMediaPlayback = true;
                 wkWebView = new WKWebView(new CGRect(0,0,300,400), configuration);
                 wkWebView.LoadRequest(new NSUrlRequest(xxxx);
                 SetNativeControl(wkWebView);
             }
         }
     }
 }


0 Votes 0 ·

Permissions are placed correctly in the info.plist , now the screen is black

https://github.com/amrkamal1993/XamarinWebRTC

I uploaded the full sample here

0 Votes 0 ·

I check your sample and deploy it to my iOS15.4 real device for testing, after I allow using my camera and mic phone, the screen is not black. I try to test on iOS15.5 simulator, the screen still not black. I have to say, I can't reproduce your issue. Furthermore, I debug your call.html in Safari, there is only an error saying "Unhandled Promise Rejections" in call.js line 51.
You could try to uninstall the app from your device, clean the bin/obj folder, and test again.

0 Votes 0 ·

0 Answers