dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.63k stars 4.56k forks source link

[iOS] Error The Internet connection appears to be offline. #102163

Open czuck opened 3 months ago

czuck commented 3 months ago

Description

This error has been reported but those reports have been closed and this is an issue in 8.0.20 but I can also recreate it with 8.0.14. When my app is not previously installed on my phone, I consistently get this error when calling HttpClient.SendAsync(requestMessage). Just before this call Connectivity.NetworkAccess is checked and is set to NetworkAccess.Internet. I will do a full rebuild and debug and repeat that process until the request is successfully returned. Once it works, it continues to work until I delete the app from the phone, at which point the issue reappears. It happens with both Debug and Release builds. I have tested doing a rebuild, if works (for the first time), I delete the app from the phone, re-deploy the same build and test and it does not work.

{System.Net.Http.HttpRequestException: The Internet connection appears to be offline.
 ---> Foundation.NSErrorException: Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={_kCFStreamErrorCodeKey=50, NSUnderlyingError=0x3026e07e0 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_NSURLErrorNWPathKey=unsatisfied (Local network prohibited), interface: en0[802.11], ipv4, uses wifi, _kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <4B22EF41-B5E1-4278-8A9B-DE62658BEFB6>.<7>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <4B22EF41-B5E1-4278-8A9B-DE62658BEFB6>.<7>"
), NSLocalizedDescription=The Internet connection appears to be offline., NSErrorFailingURLStringKey=http://192.168.x.xx/m/d/ApiSettings/GetApiVersionInfo, NSErrorFailingURLKey=http://192.168.x.xx/m/d/ApiSettings/GetApiVersionInfo, _kCFStreamErrorDomainKey=1}
   --- End of inner exception stack trace ---
   at System.Net.Http.NSUrlSessionHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:line 544
   at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
   at JMoApp.Utilities.HttpUtilities.<GetResponseAsync>d__6`1[[JMoApp.Models.ApiVersionInfo, JMoApp, Version=2024.1.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext() in C:\Code\My App \Utilities\HttpUtilities.cs:line 180}

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

8.0.20 SR4

Is this a regression from previous behavior?

Yes, this used to work in Xamarin.Forms

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

17.4.1

Did you find any workaround?

keep rebuilding and debugging.

Relevant log output

No response

jfversluis commented 2 months ago

Could you please add any relevant code that you are using here? Preferably a reproduction project?

czuck commented 2 months ago

Trying to narrow down the issue, it only occurs if the url I'm sending the http request to is my local machine or local hyperV instance. I'm using ip addresses for both connections, but I can use an ip to a non-secure server on the network and do not get the issue. I do have NSAllowsArbitraryLoads set to true. I do have a 'try again' button that executes the exact same http request, and the second time it will often return successfully. This is also happening during the startup of my app.

mjanzetic commented 2 months ago

Hi, I'm not the OP but basically facing the same issue. I've written a little sample project which can be found here. It's a REST API client which also uses HttpClient.SendAsync(requestMessage) to send a request to a URL of your choice.

I've tested this both on Android and iOS with physical devices. On Android (10) my request is successful and answered with 204 Not Content (as expected): Android

On iOS (17.4.1) the same request is failing with the same error as OP's: iOS

mjanzetic commented 2 months ago

I forgot to mention that my REST server runs on an internal network without any access to the Internet. Maybe this helps.

QianaJiao commented 2 months ago

Verified this issue with Visual Studio 17.10 Preview 7.0 (8.0.21). Can repro it on Maui iOS platform. And in Xamarin iOS and Android. It worked fine.

mattleibow commented 2 months ago

/similarissues

github-actions[bot] commented 2 months ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

dotnet-policy-service[bot] commented 2 months ago

Tagging subscribers to this area: @dotnet/ncl See info in area-owners.md if you want to be subscribed.

Ni2Be commented 1 month ago

Getting the same error with post

            var response = await client.PostAsync(url,
                new StringContent(
                    JsonConvert.SerializeObject(item),
                    Encoding.UTF8, MediaType), cancellationToken);
Versions: ```yml dotnet --info .NET SDK: Version: 8.0.301 Commit: 1a0e9c0300 Workload version: 8.0.300-manifests.011fccd5 MSBuild version: 17.10.4+10fbfbf2e Runtime Environment: OS Name: Windows OS Version: 10.0.22631 OS Platform: Windows RID: win-x64 Base Path: C:\Program Files\dotnet\sdk\8.0.301\ .NET workloads installed: [android] Installation Source: SDK 8.0.300, VS 17.10.34928.147 Manifest Version: 34.0.95/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.android\34.0.95\WorkloadManifest.json Install Type: FileBased [aspire] Installation Source: SDK 8.0.300, VS 17.10.34928.147 Manifest Version: 8.0.1/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.0.1\WorkloadManifest.json Install Type: FileBased [ios] Installation Source: SDK 8.0.300, VS 17.10.34928.147 Manifest Version: 17.2.8053/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.ios\17.2.8053\WorkloadManifest.json Install Type: FileBased [maccatalyst] Installation Source: SDK 8.0.300, VS 17.10.34928.147 Manifest Version: 17.2.8053/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maccatalyst\17.2.8053\WorkloadManifest.json Install Type: FileBased [maui-windows] Installation Source: SDK 8.0.300, VS 17.10.34928.147 Manifest Version: 8.0.21/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maui\8.0.21\WorkloadManifest.json Install Type: FileBased Host: Version: 8.0.6 Architecture: x64 Commit: 3b8b000a0e .NET SDKs installed: 8.0.205 [C:\Program Files\dotnet\sdk] 8.0.300 [C:\Program Files\dotnet\sdk] 8.0.301 [C:\Program Files\dotnet\sdk] .NET runtimes installed: Microsoft.AspNetCore.App 8.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 8.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 8.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 8.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 8.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 8.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Other architectures found: x86 [C:\Program Files (x86)\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation] Environment variables: Not set global.json file: Not found Learn more: https://aka.ms/dotnet/info Download .NET: https://aka.ms/dotnet/download ```
nickdavidbhs commented 1 month ago

Does anyone got to solve the issue? I am facing the same issue.

In debug mode the api calls work fine. But in release after first install is not working, the user has to close the app and reopen it, then it keeps working fine.

Version 8.0.20

deejcoder commented 1 month ago

I have managed to reproduce this reliably. I am not sure if this is the same as the original problem.

We have multiple solutions that reference a core nuget package which is a MAUI class library and has all of our components and any shared resources (images / styles).

When there are multiple solutions installed on a single iOS device, with different versions of this nuget package, then I get the error.

If I uninstall all but one of the solutions, it starts working again.

MrZander commented 3 weeks ago

I'm running into this and cannot get past it. It is only happening once deployed, testing it through TestFlight.

I can build the app in release mode with a developer provisioning profile, and it connects fine, but once I deploy it through the app store, it gives me "The Internet connection appears to be offline."

I am trying to connect to a local resource on the same network, which is using a self-signed TLS certificate (using ServerCertificateCustomValidationCallback to validate it manually).
I can connect to our service fine through Safari, but not my app when deployed.

I've tried adding:

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

to Info.plist and using NSUrlSessionHandler.TrustOverrideForUrl to manually approve the URL, but neither have helped.

dotnet --version 8.0.302

If anyone has any insight, it would be greatly appreciated. This is blocking my ability to publish the app and I am out of debugging ideas.

deejcoder commented 3 weeks ago

I'm running into this and cannot get past it. It is only happening once deployed, testing it through TestFlight.

I can build the app in release mode with a developer provisioning profile, and it connects fine, but once I deploy it through the app store, it gives me "The Internet connection appears to be offline."

I am trying to connect to a local resource on the same network, which is using a self-signed TLS certificate (using ServerCertificateCustomValidationCallback to validate it manually).
I can connect to our service fine through Safari, but not my app when deployed.

I've tried adding:

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

to Info.plist and using NSUrlSessionHandler.TrustOverrideForUrl to manually approve the URL, but neither have helped.

dotnet --version 8.0.302

If anyone has any insight, it would be greatly appreciated. This is blocking my ability to publish the app and I am out of debugging ideas.

Hi MrZander,

Have you tried removing NSAllowsLocalNetworking and instead adding NSAllowsArbitraryLoads? Doing this, will disable the ATS security restrictions, which is not recommended, however is required if your network service does not meet them. You can read more here: https://developer.apple.com/documentation/bundleresources/information_property_list/nsapptransportsecurity/nsallowsarbitraryloads

Even with ATS turned off I believe you will still be prompted to grant permission to access local networks, if all is working as expected.

Please note, while this may be a solution, it is still not a solution when having multiple MAUI apps installed at once, as per my previous comment.

MrZander commented 3 weeks ago

@deejcoder I get the same error message with NSAllowsArbitraryLoads, but I only have one MAUI app installed.

MrZander commented 3 weeks ago

It started working after I rebooted my device. I have no idea why, one of the most frustrating problems I've ever debugged.

My info.plist has only NSAllowsLocalNetworking set to true