How can I check programmatically if an iOS device supports vibration?

How can I check programmatically if an iOS device supports vibration? Is the only way to make the assumption that iPhones can vibrate but iPads, iPods etc cannot?

For UWP, I can use:

            bool deviceCanVibrate
                = ApiInformation.IsTypePresent("Windows.Phone.Devices.Notification.VibrationDevice") 
                  && (! (VibrationDevice.GetDefault() is null));

For Android, I can use:

            bool deviceCanVibrate = false;
            Vibrator vibrator  = (Vibrator) MainActivity.TheInstance.GetSystemService(Context.VibratorService);
            if (! (vibrator is null))
            {
                // According to https://developer.android.com/reference/android/os/Vibrator?hl=da
                // the HasVibrator method was added in API 11 (Hoeycomb). Before that, we assume 
                // that any device with the Context.VibratorService can vibrate. Of course, 
                // Xamarin.Forms does not support stuff that old, so it's a moot point whether 
                // it's worth doing the test for the version number, but I do it anyway in case I ever 
                // port this code away from Xamarin.Forms
                // Details of version numbers can be found at
                // https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/android-api-levels?tabs=vswin
                deviceCanVibrate
                    = (Build.VERSION.SdkInt < BuildVersionCodes.Honeycomb) 
                      || vibrator.HasVibrator;
            }

But other than checking if the device is a phone, I haven't so far found a suitable test for iOS.

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I think vibration is handled in the new-ish Xamarin Essentials package.
    https://docs.microsoft.com/en-us/xamarin/essentials/

    Looks like you can try to vibrate the device and if its not supported you'll get a "notSupportedException" returned.
    https://docs.microsoft.com/en-us/xamarin/essentials/vibrate?context=xamarin/xamarin-forms&amp;tabs=android

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    In the effect one of my coworkers wrote, he just checked if it was a phone on iOS 10.0 or higher.

    deviceCanVibrate = Device.Idiom == TargetIdiom.Phone && UIDevice.CurrentDevice.CheckSystemVersion(10, 0);

  • JohnHardmanJohnHardman GBUniversity mod

    @JoeManke said:
    In the effect one of my coworkers wrote, he just checked if it was a phone on iOS 10.0 or higher.

    deviceCanVibrate = Device.Idiom == TargetIdiom.Phone && UIDevice.CurrentDevice.CheckSystemVersion(10, 0);

    I'm curious about the check for iOS 10.0 or higher. Any idea why that was included?

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    CC @MatthewR Do you remember why you checked for iOS 10 in the TapFeedbackEffect?

  • JohnHardmanJohnHardman GBUniversity mod

    @ClintStLaurent said:
    I think vibration is handled in the new-ish Xamarin Essentials package.
    https://docs.microsoft.com/en-us/xamarin/essentials/

    Looks like you can try to vibrate the device and if its not supported you'll get a "notSupportedException" returned.
    https://docs.microsoft.com/en-us/xamarin/essentials/vibrate?context=xamarin/xamarin-forms&amp;tabs=android

    I want to check if vibration is supported, without actually causing a vibration (if it is).
    Xamarin.Essentials doesn't do a check for this on iOS.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Sounds like a good feature request for that pre-release package. Easier to add it in the early stages.. or point out the use case that people want to know so it can be baked-in early.

  • MatthewRMatthewR USMember ✭✭
    edited September 2018

    @JoeManke @JohnHardman

    If you attempted to use the UIImpactFeedbackGenerator on devices less than iOS 10 it would throw an exception.

    *Keep in mind vibrations were only supported on iPhone 7 or newer.

  • JohnHardmanJohnHardman GBUniversity mod
    edited September 2018

    @MatthewR said:
    *Keep in mind vibrations were only supported on iPhone 7 (maybe 6s?) or newer.

    Charging up my old iPhones - pretty sure my 6s, 5c and 4s all vibrated... Will update once they spring to life :-) Just curious about that bit, as I'm not targeting iPhones before 6.

  • MatthewRMatthewR USMember ✭✭

    @JohnHardman said:

    @MatthewR said:
    *Keep in mind vibrations were only supported on iPhone 7 (maybe 6s?) or newer.

    Charging up my old iPhones - pretty sure both my 6s and 4s vibrated... Will update once they spring to life :-) Just curious about that bit, as I'm not targeting iPhones that old.

    https://stackoverflow.com/a/41446364

  • JohnHardmanJohnHardman GBUniversity mod

    @JohnHardman said:

    @MatthewR said:
    *Keep in mind vibrations were only supported on iPhone 7 (maybe 6s?) or newer.

    Charging up my old iPhones - pretty sure my 6s, 5c and 4s all vibrated... Will update once they spring to life :-) Just curious about that bit, as I'm not targeting iPhones before 6.

    Both my 6s and 5c vibrate. Can't find the charger for the 4s right now, but pretty sure that did too. Whether the vibrate plug-in uses UIImpactFeedbackGenerator I don't know without investigating. Perhaps it uses a different mechanism.

  • MatthewRMatthewR USMember ✭✭

    @JohnHardman said:

    @JohnHardman said:

    @MatthewR said:
    *Keep in mind vibrations were only supported on iPhone 7 (maybe 6s?) or newer.

    Charging up my old iPhones - pretty sure my 6s, 5c and 4s all vibrated... Will update once they spring to life :-) Just curious about that bit, as I'm not targeting iPhones before 6.

    Both my 6s and 5c vibrate. Can't find the charger for the 4s right now, but pretty sure that did too. Whether the vibrate plug-in uses UIImpactFeedbackGenerator I don't know without investigating. Perhaps it uses a different mechanism.

    You will be able to use vibrations with the VibratePlugin but you won't be able to change the duration.
    https://github.com/jamesmontemagno/VibratePlugin/blob/master/src/Vibrate.Plugin/Vibrate.ios.cs

    I wanted a "haptic" vibrate so 500ms was a bit intense for that so I use the UIImpactFeedbackGenerator.

    private void HapticVibrate()
    {
        if (Device.Idiom != TargetIdiom.Phone || !UIDevice.CurrentDevice.CheckSystemVersion(10, 0)) return;
        var impact = new UIImpactFeedbackGenerator(UIImpactFeedbackStyle.Light);
        impact.Prepare();
        impact.ImpactOccurred();
    }
    

    Either way, you can ignore my greater than iphone 7 comment if you are using the vibrate plugin. ;)

  • JohnHardmanJohnHardman GBUniversity mod

    @MatthewR said:
    You will be able to use vibrations with the VibratePlugin but you won't be able to change the duration.
    I wanted a "haptic" vibrate so 500ms was a bit intense for that so I use the UIImpactFeedbackGenerator.

    Ah, that's interesting. I use different durations on Android and it makes a big difference to user experience. I'll take a look at UIImpactFeedbackGenerator and maybe use that when available to allow the duration to be changed on iOS, and fallback to the plugin if UIImpactFeedbackGenerator is not available. Many thanks.

Sign In or Register to comment.