Dmitry Zinoviev
Dmitry Zinoviev

Reputation: 588

Custom vibration in Xamarin iOS

I need create a custom vibration(vibration must work 1 sec), but the is only method I know to start vibration SystemSound.Vibrate.PlaySystemSound()

How can I implement it?

Upvotes: 2

Views: 1508

Answers (2)

Dmitry Zinoviev
Dmitry Zinoviev

Reputation: 588

I found solution, but problem is that app can be rejected during verify.

I used this link. Are there APIs for custom vibrations in iOS?

And here is implementation for Xamarin.ios

    public enum VibrationPower
    {
        Normal,
        Low,
        Hight
    }

    [DllImport("/System/Library/Frameworks/AudioToolbox.framework/AudioToolbox")]
    private static extern void AudioServicesPlaySystemSoundWithVibration(uint inSystemSoundID, NSObject arg,
        IntPtr pattert);

    private void hightVibration()
    {
        var dictionary = new NSMutableDictionary();
        var vibePattern = new NSMutableArray();
        vibePattern.Add(NSNumber.FromBoolean(true));
        vibePattern.Add(NSNumber.FromInt32(1000));
        //vibePattern.Add(NSNumber.FromBoolean(false));
        //vibePattern.Add(NSNumber.FromInt32(500));
        //vibePattern.Add(NSNumber.FromBoolean(true));
        //vibePattern.Add(NSNumber.FromInt32(1000));
        dictionary.Add(NSObject.FromObject("VibePattern"), vibePattern);
        dictionary.Add(NSObject.FromObject("Intensity"), NSNumber.FromInt32(1));
        AudioServicesPlaySystemSoundWithVibration(4095U, null, dictionary.Handle);
    }

    public void Vibration(VibrationPower power = VibrationPower.Normal)
    {
        switch (power)
        {
                case VibrationPower.Normal:
                    SystemSound.Vibrate.PlaySystemSound();
                break;
                case VibrationPower.Hight:
                    hightVibration();
                break;
        }
    }

But remember your app can be rejected during verify!!!

Upvotes: 0

SushiHangover
SushiHangover

Reputation: 74174

You can not control the exact length of the haptic feedback (like you could on Android) as it would violate the iOS user-interface guidelines.

Besides the older Vibrate.PlaySystemSound, in iOS 10(+) the UIFeedbackGenerator was added.

There are three UIFeedbackGenerator variations depending upon what you are trying to signal to the user:

  • UIImpactFeedbackGenerator
    • creates haptics to simulate physical impacts.
  • UISelectionFeedbackGenerator
    • creates haptics to indicate a change in selection.
  • UINotificationFeedbackGenerator
    • creates haptics to communicate successes, failures, and warnings.

Re: https://developer.apple.com/documentation/uikit/uifeedbackgenerator

Example:

// cache the instance
var haptic = new UINotificationFeedbackGenerator();

// Do this in advance so it is ready to be called on-demand without delay...
haptic.Prepare();

// produce the feedback as many times as needed
haptic.NotificationOccurred(UINotificationFeedbackType.Success);

// when done all done, clean up
haptic.Dispose();

Upvotes: 3

Related Questions