2

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?

Christian
  • 25,249
  • 40
  • 134
  • 225
Dmitry Zinoviev
  • 588
  • 1
  • 6
  • 20

2 Answers2

3

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();
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
0

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!!!

Dmitry Zinoviev
  • 588
  • 1
  • 6
  • 20
  • Those are private framework functions... Personally, it is not worth getting your app rejected now or in the future if your goal is get it into the App Store and to keep it there. – SushiHangover Jun 21 '18 at 10:42