1

My visual studio Xamarin.Form App works fine in Android Phone but fails on iPhone or iPhone Simulator at the following statement:

var content = await _client.GetStringAsync(RI_Url);   // RI_Ur1 is "https://xxxxxxx" 

It can display the result when I put the RI_Url in Safari, Chrome and Edge.

It works if RI_Uri = "http://xxxxxxx"

I tried to put the following in info.plist but it does not work.

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

Would someone help to solve the problem?

Thanks.

The error message is:

{System.Net.Http.HttpRequestException: An SSL error has occurred and a secure connection to the server cannot be made. ---> 
Foundation.NSErrorException: Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." 
UserInfo={NSErrorFailingURLStringKey=https://xxxxxxxxxxxxxxxxxx, 
NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, 
_NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <60343732-0A70-45A6-A4BB-263867134C53>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <60343732-0A70-45A6-A4BB-263867134C53>.<1>"
), NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., 
NSErrorFailingURLKey=https://xxxxxxxxxxxxxxxxxxx, 
NSUnderlyingError=0x6000024cafd0 {Error Domain=kCFErrorDomainCFNetwork Code=-1200 "(null)" 
UserInfo={_kCFStreamPropertySSLClientCertificateState=0, _kCFNetworkCFStreamSSLErrorOriginalValue=-9836, _kCFStreamErrorDomainKey=3, 
_kCFStreamErrorCodeKey=-9836}}, _kCFStreamErrorCodeKey=-9836}
   --- End of inner exception stack trace ---
  at System.Net.Http.NSUrlSessionHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) 
[0x001d4] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.2.0.42/src/Xamarin.iOS/Foundation/NSUrlSessionHandler.cs:437 
  at System.Net.Http.HttpClient.SendAsyncWorker (System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, 
System.Threading.CancellationToken cancellationToken) [0x0009e] 
in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:281 
  at System.Net.Http.HttpClient.GetStringAsync (System.String requestUri) [0x00033] 
in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:328 
  at xxxxxxxxx.OnGetList () [0x00039] in C:\xxxxxxx\xxxxxxxxPage.xaml.cs:41 }
deczaloth
  • 7,094
  • 4
  • 24
  • 59
  • Since iOS 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. Check https://stackoverflow.com/questions/53032699/uiwebview-in-xamarin-ios-seems-to-ignore-external-css-styles – Lucas Zhang Oct 28 '19 at 07:45

1 Answers1

1

Try this:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>yoururl</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>
Patrick Goode
  • 1,412
  • 5
  • 20
  • 35
  • I added your code at the end of info.plist but it still doesn't work. The code was added just above the last line . I wonder if I added the code in incorrectly. – Kenneth Wong Oct 25 '19 at 17:59
  • It may load in a browser, but does the site actually have a certificate error? Can you load another known good https? – Patrick Goode Oct 27 '19 at 15:45