How to make haptic in dynamic island?

202 Views Asked by At

In iOS17 dynamic island have capable to interactive with Button and Toggle .I wish to make a haptic effect when tap the button in dynamic island.

The normal solution NOT working in dynamic island likeUIImpactFeedbackGenerator.init(style: UIImpactFeedbackGenerator.FeedbackStyle.light).impactOccurred().

After I make a lot of trying,I find the oldest way to generate a haptic——AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))is still working.

But the problem is does AudioServicesPlayAlertSound can play a short haptic?

The answer is Yes!

1

There are 1 best solutions below

2
闪电狮 On

Disclaimer: below solution requires calling private Apple API's, which aren't allowed in AppStore submissions. Even though at this point in time Apple's automated checks aren't catching this, if that changes in the future (or a manual review catches the behaviour), your app might be blocked. Use this at your own risk.

To successfully make a short haptic by using AudioServicesPlaySystemSoundWithVibration,you have to call a private method in AudioToolbox.But it's safe for today's App Store, cause my app using this technology has already passing the review.I'm believe that App Store reviewer does not concert about using this technology in your app.

To make it, you need a objective-c class:

#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioServices.h>
#import <AudioToolbox/AudioToolbox.h>

#import "vibrate.h"
void AudioServicesPlaySystemSoundWithVibration(int, id, id);

@implementation Vibrate

+ (void) vibrateMutate
{
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
NSMutableArray* arr = [NSMutableArray array ];

[arr addObject:[NSNumber numberWithBool:YES]];
[arr addObject:[NSNumber numberWithInt:50]];

[arr addObject:[NSNumber numberWithBool:NO]];
[arr addObject:[NSNumber numberWithInt:10]];


[dict setObject:arr forKey:@"VibePattern"];
[dict setObject:[NSNumber numberWithDouble:.25] forKey:@"Intensity"];

AudioServicesPlaySystemSoundWithVibration(4095,nil,dict); //ERROR
}

+ (void) vibrateSectionChange
{
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
NSMutableArray* arr = [NSMutableArray array ];

[arr addObject:[NSNumber numberWithBool:YES]];
[arr addObject:[NSNumber numberWithInt:150]];

[arr addObject:[NSNumber numberWithBool:NO]];
[arr addObject:[NSNumber numberWithInt:10]];


[dict setObject:arr forKey:@"VibePattern"];
[dict setObject:[NSNumber numberWithDouble:.75] forKey:@"Intensity"];

AudioServicesPlaySystemSoundWithVibration(4095,nil,dict);
}

@end

(raw objective-c version)

and a .m file

#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>

@interface Vibrate : NSObject

+ (void)vibrateMutate;
+ (void)vibrateSectionChange;

@end

Last, adding this line to the "YOU_PROJECT_NAME-Bridging-Header.h" file that Xcode about create for you!

#import "Vibrate.h"

Finally , you can call this method in you Swift code(if not working, relaunch your Xcode app might work):

Vibrate.vibrateMutate()

You'll get a short、Morden-style haptic in you dynamic island!