exendahal / maui_wifi_manager

.NET MAUI Wi-Fi Manager is solution for Wi-Fi management features. With these APIs, effortlessly connect, add, and retrieve Wi-Fi network information in cross-platform MAUI applications.
https://github.com/exendahal/WifiManagerMAUI
MIT License
15 stars 5 forks source link

How to initialise in iOS #6

Open ltemimi opened 2 months ago

ltemimi commented 2 months ago

Hi

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

exendahal commented 2 months ago

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

ltemimi commented 2 months ago

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 commented 2 months ago

Can you please share the error screenshot?

ltemimi commented 2 months ago

I have added

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

    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

ltemimi commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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

ltemimi commented 2 months ago

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 commented 2 months ago

Can you test this example attached at the end here? https://github.com/exendahal/maui_wifi_manager/issues/5

ltemimi commented 2 months ago

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 commented 2 months ago

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

ltemimi commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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

ltemimi commented 2 months ago

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 commented 1 month ago

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

Please help

exendahal commented 1 month ago

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

ltemimi commented 1 month ago

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

exendahal commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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