Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to initialise in iOS #6

Open
ltemimi opened this issue Aug 12, 2024 · 27 comments
Open

How to initialise in iOS #6

ltemimi opened this issue Aug 12, 2024 · 27 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@ltemimi
Copy link

ltemimi commented Aug 12, 2024

Hi

I am not clear on how to initialise in the app delegate for ios Please help

@exendahal
Copy link
Owner

Please refer to the Demo project. iOS initialization is not required.

@exendahal exendahal added the documentation Improvements or additions to documentation label Aug 12, 2024
@ltemimi
Copy link
Author

ltemimi commented Aug 12, 2024

I did thanks and I realise that but it works on android but not ios I get an exception

I get an exception

Object reference not set to an instance of an object.

In the line

var networkInfo = await CrossWifiManager.Current.GetNetworkInfo();

@exendahal
Copy link
Owner

Can you please share the error screenshot?

@ltemimi
Copy link
Author

ltemimi commented Aug 12, 2024

I have added

var status2 = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
if (status2 != PermissionStatus.Granted)
{
_ = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
}

    to request permissions if needed and now the exception

is nodename nor servname provided, or not known

image

My device is connected to th Internet.

Thanks

@exendahal exendahal self-assigned this Aug 13, 2024
@ltemimi
Copy link
Author

ltemimi commented Aug 13, 2024

Hi I hope we will find an answer to the ios issue it is expecting DNS and some other parameters.

I also have found another issue in the windows platform the SSD is always null when there is an internet wired connection in addition to the wireless connections. Please help

@exendahal
Copy link
Owner

exendahal commented Aug 13, 2024

Can you please share some more details regarding your test environment, with me so that I can reproduce the issue? Also, are you all getting this issues on the demo app ?

@ltemimi
Copy link
Author

ltemimi commented Aug 14, 2024

Hi

I have a dot net maui app I am using android,ios and windows platform and I am using a WifStaus View and the view model behind it uses code from the github repo. My wifi used for internet and local network is part of an internal IT infrastructure of the a business centre.

As to the windows platform if I disconnect the wired internet netwrok all works fine. So there is a conflict

And most important I am using dot net 8.

I am unable ro run the demo despite the fact I installed dot net 5 as you use netstandard 2

Severity Code Description Project File Line Suppression State
Error (active) MSB3644 The reference assemblies for .NETCore,Version=v5.0 were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks MauiWifiManager (uap10.0.19041) C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets 1259

My View model

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Core.ExtensionsLibrary.Services;
using License.Client.Interfaces;
using License.Maui.Interfaces;
using Plugin.MauiWifiManager;

namespace License.Maui.MVVM.ViewModels;

public partial class WifiStatusViewModel(
    IInternetConnectionHelper internetConnectionHelper,
    IAlertService alertService) : BaseViewModel
{
    [ObservableProperty]
    // ReSharper disable once InconsistentNaming
    // that is how the mvvm community works
    private string? currentSsid;


    [ObservableProperty]
    // ReSharper disable once InconsistentNaming
    // that is how the mvvm community works
    private bool hasInternet;

    [ObservableProperty]
    // ReSharper disable once InconsistentNaming
    // that is how the mvvm community works
    private string ipAddress = "0.0.0.0";

    [ObservableProperty]
    // ReSharper disable once InconsistentNaming
    // that is how the mvvm community works
    private string wifiStatus = "Not Checked Yet";


    [ObservableProperty]
    // ReSharper disable once InconsistentNaming
    // that is how the mvvm community works
    private Color wifiStatusColour = Colors.Cyan;

    public event EventHandler? CheckWifiStatus;


    [RelayCommand]
    public async Task CheckWifiStatusAsync()
    {
        try
        {
            await EnsurePermissionsAsync();

            // Step 1: Get current Wi-Fi status and SSID
            var networkInfo = await CrossWifiManager.Current.GetNetworkInfo();

            // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
            //it could be null
            if (networkInfo is null)
            {
                WifiStatus = "No Wi-Fi available";
                return;
            }

            CurrentSsid = networkInfo.Ssid ?? "SSD?";
            IpAddress = "Network Ip: " + networkInfo.IpAddress.ConvertIntToIpAddress();

            // Step 2: Check if the Wi-Fi has internet access
            HasInternet = await internetConnectionHelper.IsInternetAvailableAsync();

            WifiStatus = (HasInternet, networkInfo.Ssid != null) switch
            {
                (true, true) => $"Wi-Fi: '{CurrentSsid}' internet access ",
                (true, false) => $"Wi-Fi: '{CurrentSsid}' internet access wired?",
                (false, true) => $"Wi-Fi: '{CurrentSsid}' no internet access",
                (false, false) => $"Wi-Fi: '{CurrentSsid}' no internet access"
            };

            // fire event
            CheckWifiStatus?.Invoke(this, EventArgs.Empty);
        }
        catch (Exception e)
        {
            var err = e.Message;
            WifiStatus = "Unable to check wifi status";
            await alertService.ShowAlert("Unable to check wifi", "Checking Wifi is not allowed!", "OK");
        }
    }

    [RelayCommand]
    public async Task OpenWifiSettings()
    {
        _ = await CrossWifiManager.Current.OpenWifiSetting();
    }

    private async Task EnsurePermissionsAsync()
    {
        var status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
        if (status != PermissionStatus.Granted)
        {
            _ = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
        }
    }
}

@exendahal
Copy link
Owner

Do you mean you were not able to build the Demo project due to reference assemblies?

@ltemimi
Copy link
Author

ltemimi commented Aug 14, 2024

Correct it asked me to download dot net 5 which I did to no avail. Thanks I am mainly concerned that it does not work with ios due to missing parameters linke DNS etc..

@exendahal
Copy link
Owner

Can you test this example attached at the end here? #5

@ltemimi
Copy link
Author

ltemimi commented Aug 14, 2024

Hi
still could not build the pro0ject but I ported the code and the info.plist etc. and

var response = await CrossWifiManager.Current.ScanWifiNetworks(); returns a count of zero it has not detected any wifi

@exendahal
Copy link
Owner

Can you share your implementation into a separate project? I will try to replicate on my machine.

@ltemimi
Copy link
Author

ltemimi commented Aug 15, 2024

Hi many thanks for the help. I have spent this morning doing a new dot net Maui project and I tested it with windows, android and ios. The wifiStaus is a control (View) so that future app can use it>

Please use your own apple provisioning

1 - Windows platform works if not wired internet is connected . When there is a wired internet connection it overrides all.
2 - Android works well
3 - IOS can't scan wifi network

NOTE: in IOS if the wifi scan returns zero count and one attempt to get a network info an exception is thrown as it cannot find parameters it needs the exception is a socket exception

Please download from here: https://1drv.ms/f/s!Ai1jsiYR5baCnJEqYOPc0-EJHPILCg?e=GNddYx

@exendahal
Copy link
Owner

You can access Wi-Fi networks using the ScanWifiNetworks method only on Android & Windows. Unfortunately, iOS doesn't provide such APIs to scan nearby Wi-Fi.

@ltemimi
Copy link
Author

ltemimi commented Aug 15, 2024

OK if you use the get info you will get a socket exception as explained before with a screen shot (the if statement that checks for the count > 0 was put only recently to avoid the socket exception. So please feel free to take it out.

So please help with the socket exception on iOS

thanks

@exendahal
Copy link
Owner

Could you please share the iOS version and device model you're currently using?

@ltemimi
Copy link
Author

ltemimi commented Aug 16, 2024

yes, I am using iPhone 8 iOS 16.7.10

the exception is thrown at var networkInfo = await CrossWifiManager.Current.GetNetworkInfo();

Thanks for the help

@ltemimi
Copy link
Author

ltemimi commented Sep 5, 2024

Hi I bought an iPhone 11 with latest os still the same issue cannot get the SSD data

Please help

@exendahal
Copy link
Owner

Please share me your demo project. I can't replicate your issue in my side

@ltemimi
Copy link
Author

ltemimi commented Sep 5, 2024

I did send you my demo project, I think it is better if you send yours. Thanks

@exendahal
Copy link
Owner

exendahal commented Sep 5, 2024

I am running the demo project that comes with the project source code. Also, I was not able to get the source code from the link above. Can you please confirm the link attached?

@ltemimi
Copy link
Author

ltemimi commented Sep 5, 2024

Hi

I used the demo project and it does have the same problem with getting SSD data so it must be the business centre network (though it works on Android) . I will try another location will get back soon Thank.

Yes I removed the demo I sent you I will re-instate it if needed

@ltemimi
Copy link
Author

ltemimi commented Sep 8, 2024

Hi

I tried on a different network (at home) and got the same results i.e could not get network data. I changed disabled the Wi fi private address and I got the Ip address but not the SSD.

I even tried using native code to no avail. So I have come to the conclusion it is an apple restriction. (some sources quote since version 14)

Your demo gives the same results as my code.

Have you tested your demo on a later version than 14? if it worked were there any changes made to the Wi Fi settings?

Thanks

@exendahal
Copy link
Owner

I have a working demo project on iOS 18 that provides information about the connected Wi-Fi. If you can share any replication steps, I'd be happy to include them in the test cases, as it could be beneficial for others as well.

@ltemimi
Copy link
Author

ltemimi commented Sep 14, 2024

Hi Sorry for the delay (technology probs)

Her is the link I have not tested with ios 18

https://1drv.ms/f/s!Ai1jsiYR5baCnftCUUC-JK280yE3Ug?e=vjfnAo

@exendahal
Copy link
Owner

Hello,
Can you please get 1.0.5 version for the package.
Also, please enable access to Wi-Fi information capabilities for your iOS app in App Store Connect.
image

@ltemimi
Copy link
Author

ltemimi commented Sep 22, 2024

Ok Thanks it requires Apple approval, I will submit a form and see what happens.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants