Xamarin.Forms WebView on iOS does not execute window.open() function

dushanigdushanig Member ✭✭

Hi,

I have a xamarin.forms app which uses a webview to display the home screen(user details screen) when the user is successfully logged in to his/her account. This web page(user details screen) has a javascript function window.open() for a click event on a link, and when the link is clicked nothing happens. How can I enable javascript on the webview for iOS? This is working fine on android.

Thanks

Answers

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai
    edited June 5

    Firstly, If you set a link in your html file .You don't need to set the click action any more .

    <a href="your url" >this is a link</a>
    

    In Addition ,If your url has prefix https:// ,add the following code in info.plist

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
          <key>your domain</key>
          <dict>       
           <key>NSExceptionRequiresForwardSecrecy</key>
           <false/>
           <key>NSExceptionAllowsInsecureHTTPLoads</key>
           <true/>
           <key>NSIncludesSubdomains</key>
           <true/>
         </dict>
    </dict>
    </dict>
    

    If it is still doesn't work .You can provide the url and the code in html so that I can test it on my side. :)

  • dushanigdushanig Member ✭✭

    Hi @LucasZhang , thanks for your help, but the webpage is handled by a third party so I cannot edit or share the html, and also the link doesn't have an href property and it's using an onClick event to call a JS function. In android setting JavaScriptEnabled, DomStorageEnabled and JavaScriptCanOpenWindowsAutomatically to true using a custom webview renderer solved the issue. I wan't to know the similar approach for iOS. I have already created a custom renderer.

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai

    WebView in iOS allows load js in default. Did you add the code in info.plist? And can you provide the error code in output?

  • dushanigdushanig Member ✭✭
    edited June 5

    Yes I added the code in info.plist. Unfortunately I'm not getting any error codes in output, when the link is clicked nothing happens. The click event has an ajax request, and on success it opens a url on another tab using "var myWindow = window.open("_blank");". This works fine on a browser and opens the link on a new tab but won't work on the webview.

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai
    edited June 5

    window.open() will been blocked on iOS because of apple safety policy(window.open() will open a new page in safari ).There's nothing wrong with your code and project settings. I suggest you can contact the third part and use
    window.location.href = linkUrl

  • dushanigdushanig Member ✭✭

    I can try it out but if it doesn't work, are you sure that there is no workaround to open the safari browser from the webview?

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai
    edited June 6

    You can try the following code .

    using Foundation;
    using UIKit;
    
    using xxx;
    using xxx.iOS;
    
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.iOS;
    using WebKit;
    
    [assembly:ExportRenderer(typeof(WebView),typeof(MyWebViewRenderer))]
    namespace xxx.iOS
    {
        public class MyWebViewRenderer:WkWebViewRenderer ,IWKUIDelegate
        {
            protected override void OnElementChanged(VisualElementChangedEventArgs e)
            {
                base.OnElementChanged(e);
    
                if(NativeView!=null)
                {
                    WeakUIDelegate = this;
                }
    
            }
    
            [Export("webView:createWebViewWithConfiguration:forNavigationAction:windowFeatures:")]
            public WKWebView CreateWebView(WKWebView webView,WKWebViewConfiguration configuration,WKNavigationAction action, WKWindowFeatures  features)
            {
    
                if(action.Request.Url!=null)
                {
    
                    NSUrl url = action.Request.Url;
    
                    UIApplication.SharedApplication.OpenUrl(url);
    
                }
    
                return null;
            }
    
        }
    }
    
  • dushanigdushanig Member ✭✭

    This is also not working LucasZhang, nothing happens when the link is clicked.

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai

    As I said window.open() will been blocked on iOS .So the best way is using window.location.href = linkUrl

  • dushanigdushanig Member ✭✭

    Yes, I will try that out. Thanks for your help.

Sign In or Register to comment.