HttpListener used on Xamarin.IOS app. "Safari cant open the page because it couldn't connect to the server" error only on iOS 17 devices.

Anonymous
2023-09-20T06:21:25.29+00:00

Our project uses the latest Xamarin and our app has a function that creates a HTTP server to store a html and config file, and we use SfSafariViewController to view the contents on the html file. However, after the release of iOS17, this function does not work as intended. Safari was opened but could not connect to the localhost.

Question: is the creation of a localhost server using HttpListener blocked by Safari?

We want to view the contents on an in-app browser, and we are looking at using WKWebView as an alternative but we are open to more suggestions.

== Starting the server ==

using System.Net;

static HttpListener _httpListener = new HttpListener();

_httpListener.Prefixes.Add("http://localhost:8000/");

_httpListener.Start();

== Viewing the contents on Safari ==

var sfViewController = new SFSafariViewController(new NSUrl($"http://localhost:8000/test.html")

// vc has a class type of UIViewController

vc.PresentViewController(sfViewController, true, null);

== Outcome ==

Safari opened and display: “Safari can’t open the page because it couldn’t connect to the server”

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,377 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,161 Reputation points Microsoft External Staff
    2023-09-21T06:03:20.01+00:00

    Hello,

    iOS 17 will not be supported with Xamarin. Xcode 14 SDKs (iOS and iPadOS 16, macOS 13) will be the final versions Xamarin targets. Please see Xamarin Support Policy.

    I addition, you should specify a server address instead of localhost. And the "Http://localhost" request is considered as a non-secure site, you could try adding NSAllowsLocalNetworking to the Info.plist file.

    <key>NSAppTransportSecurity</key>
    <dict>
        <key> NSAllowsLocalNetworking</key>
        <true/>
    </dict>
    

    Or disable ATS by adding NSAllowsArbitraryLoads key.

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

    Or specific domain to bypass ATS. See App Transport Security in Xamarin.iOS - Xamarin | Microsoft Learn

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.