简体   繁体   中英

WebView is not showing anything in Xamarin Forms for IOS

I'm newbie in this kind of development. I'm developing a Xamarin Forms for IOS in Visual Studio 2017. Company I work for needs that app shows the html content of a specific url into a WebView. I did that and I tested using different urls that shows content fine, however when using IP in http urls, it doesn't show anything. Something like this (obviously I replaced real Ip with X's):

http://XX.XX.XX.XX:9090/banking/initAppn1.start()

As far as I know, for security reasons, Apple restricts of using IP in urls (most of all if app will be deployed in the store) and when unsecured protocol like http is used, documentation indicate: "Since version 9, iOS will only allow your application to communicate with servers that implement best-practice security by default. Values must be set in Info.plist to enable communication with insecure servers". To enable a specific domain to bypass ATS requirements according documentation I did this into info.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>UIDeviceFamily</key>
    <array>
        <integer>1</integer>
        <integer>2</integer>
    </array>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>MinimumOSVersion</key>
    <string>6.0</string>
    <key>CFBundleDisplayName</key>
    <string>TestMobile</string>
    <key>CFBundleIdentifier</key>
    <string>com.yourcompany.TestMobile</string>
    <key>CFBundleVersion</key>
    <string>1.0</string>
    <key>CFBundleIconFiles</key>
    <array>
        <string>Icon-60@2x.png</string>
        <string>Icon-76.png</string>
        <string>Icon-76@2x.png</string>
        <string>Default.png</string>
        <string>Default@2x.png</string>
        <string>Default-568h@2x.png</string>
        <string>Default-Portrait.png</string>
        <string>Default-Portrait@2x.png</string>
        <string>Icon-Small-40.png</string>
        <string>Icon-Small-40@2x.png</string>
        <string>Icon-Small.png</string>
        <string>Icon-Small@2x.png</string>
    </array>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen.storyboard</string>
    <key>CFBundleShortVersionString</key>
    <string></string>
    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>XXX.XX.XX.XX:9090</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSTemporaryExceptionMinimumTLSVersion</key>
                <string>TLSv1.1</string>
            </dict>
        </dict>
    </dict>
</dict>
</plist>

I doesn't show anyhting!. After that I tried removing port in the domain, just using the IP address, and I doesn't work too.

After that I tried allowing arbitrary loads like this:

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

Anything is shown!

For now, domain name for the URL with IP is not provided by the client for infrastructure reasons, but IP is just for testing.

Somebody told me that he developed similar app using IP in url and app worked.

By the way, using the url in a browser works perfect!

Please need your help! Thanks

I've found solution. On one hand, if app will be published in the app store, IP must not be used because it will be rejected due to IPv6 compatibility, that is my case, otherwise, it could use IP just for internal distribution. On the Second hand, by using insecure URL (Http), you must configure NSAppTransportSecurity with NSExceptionDomains into info.plist with no ports in the URL according documentation: https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html

So, I've used domain name with no port and NSExceptionDomains and that's all, it worked. Thanks!

By using:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

You'll disable the ATS and it will reject your application, during the App Review process, without a valid justification for it.

As you said, the proper way is by using a NSExceptionDomains dictionary filled with the URLs that cannot be accessed through the HTTPS protocol. Another way to disable the ATS for web views is by using the key:

NSAllowsArbitraryLoadsInWebContent

That if enabled, disables all ATS restrictions for requests made from web views. This lets your app use an embedded browser that can display arbitrary content, without disabling ATS for the other URLs.

Reference

I searched high and low for the answer. It turned out to be the height and width requests

"Unlike most other Xamarin.Forms views, WebView requires that HeightRequest and WidthRequest are specified when contained in StackLayout or RelativeLayout. If you fail to specify those properties, the WebView will not render."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM