dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.28k stars 1.76k forks source link

iOS simulator Accelerometer Microsoft.Maui.ApplicationModel.FeatureNotSupportedException #13889

Closed myers-gh1328 closed 1 year ago

myers-gh1328 commented 1 year ago

Description

The iOS simulator is throwing a Microsoft.Maui.ApplicationModel.FeatureNotSupportedException when I try to use the accelerometer to detect the shake event. I followed the below article which does not mention anything about this not being available for the iOS simulators. And according to Apple's documentation this is a supported feature. https://help.apple.com/simulator/mac/current/#/devb0244142d

https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/device/sensors?view=net-maui-7.0&tabs=ios#shake

Steps to Reproduce

  1. Create a new .net maui sample app
  2. Add the shake detection per this MS article https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/device/sensors?view=net-maui-7.0&tabs=ios#shake
  3. Try to run the app in an iOS simulator

Link to public reproduction project repository

https://github.com/mmyers299/maui-sensor-bug

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

All

Did you find any workaround?

Comment out the code only when debugging on an iOS simulator

Relevant log output

No response

drasticactions commented 1 year ago
スクリーンショット 2023-03-14 17 42 07

Are you sure it's supported?

ghost commented 1 year ago

Hi @mmyers299. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

myers-gh1328 commented 1 year ago

@drasticactions I am 100% sure, it is specifically mentioned in Apple's documentation (see last item in screenshot). There is also a button in the simulator device menu (and a shortcut) to "shake" the device.

Screenshot 2023-03-14 at 7 52 54 AM
drasticactions commented 1 year ago

The simulator supports the "Shake" gesture, which is not the same thing as the accelerometer.

https://github.com/dotnet/maui/blob/917f4497a7df1416a57814e5945b8bb284211c3b/src/Essentials/src/Accelerometer/Accelerometer.ios.watchos.cs

The implementation in Essentials uses CMMotionManager which, correctly, says that the accelerometer is not available, because it's not. So the way MAUI detects shakes will not work with the simulator.

https://github.com/xamarin/xamarin-macios/blob/29633a62311bdf00e5f29b961560a84e3935d2a4/src/UIKit/UIEnums.cs#L180-L182

That said, there are other ways to detect it within .NET and iOS, but getting to it from MAUI UI is tricky.


[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
    public AppDelegate()
    {
    }

    protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();

    public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
    {
        application.ApplicationSupportsShakeToEdit = true;
        return base.FinishedLaunching(application, launchOptions);
    }
}

The shake gesture requires setting "ApplicationSupportsShakeToEdit" on the UIApplication, which you can get to from the AppDelegate.

However, the subscribe to the events, you need to override the underlying UIApplication or UIViewControllers for the Motion events. They are not event handlers, but methods that are called that you would override... which you can only, AFAIK, do in MAUI if you create your own UIViewController and somehow wire it into the existing stack... which seems not viable.

@rolfbjarne Is what I'm saying correct? Is there anything this person can do?

rolfbjarne commented 1 year ago

However, the subscribe to the events, you need to override the underlying UIApplication or UIViewControllers for the Motion events. They are not event handlers, but methods that are called that you would override... which you can only, AFAIK, do in MAUI if you create your own UIViewController and somehow wire it into the existing stack... which seems not viable.

@rolfbjarne Is what I'm saying correct? Is there anything this person can do?

I'm not sure what you can and can't do in MAUI, but your explanation of how it has to be implemented seems to be correct as far as I can tell.

mattleibow commented 1 year ago

Closing this issue as it is not supported using the MAUI shake detection. However, if this is a feature that is important to you, please open a new issue for a new feature request so that we can track that and discuss the implementation.