ShingoFukuyama
10/2/2016 - 5:03 AM

Taptic API for iPhone 7, 7 Plus (Public API). And also for iPhone 6s, 6s Plus (Private API).

Taptic API for iPhone 7, 7 Plus (Public API). And also for iPhone 6s, 6s Plus (Private API).


// StackOverflow: Taptic in iOS 9
// http://stackoverflow.com/a/36320245/3283039

#import <Foundation/Foundation.h>
#import <UIKit/UIDevice.h>
@import AudioToolbox;
#include <sys/sysctl.h>

#define VER_10_0_OR_LATER       ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0f)


NS_INLINE NSString * ext_deviceType()
{
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *machine = malloc(size);
    sysctlbyname("hw.machine", machine, &size, NULL, 0);
    NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
    free(machine);
    return platform;
}

#define IPHONE_TAPTIC_OLD       ([ext_deviceType() isEqualToString:@"iPhone8,1"] || [ext_deviceType() isEqualToString:@"iPhone8,2"])

NS_INLINE void tapticNotificationWithStyle(UINotificationFeedbackType type) {
    if (!VER_10_0_OR_LATER) return;
    if (![OHJGlobal shared].tapticEngineEnabled) return;
    UINotificationFeedbackGenerator *taptic = [[UINotificationFeedbackGenerator alloc] init];
    [taptic prepare];
    [taptic notificationOccurred:type];
}

NS_INLINE void tapticNotificationError() {
    if (![OHJGlobal shared].tapticEngineEnabled) return;
    if (IPHONE_TAPTIC_OLD) {
        AudioServicesPlaySystemSound(1521);
    }
    else {
        if (!VER_10_0_OR_LATER) return;
        UINotificationFeedbackGenerator *taptic = [[UINotificationFeedbackGenerator alloc] init];
        [taptic prepare];
        [taptic notificationOccurred:UINotificationFeedbackTypeError];
    }
}

NS_INLINE void tapticNotificationWarning() {
    if (!VER_10_0_OR_LATER) return;
    if (![OHJGlobal shared].tapticEngineEnabled) return;
    UINotificationFeedbackGenerator *taptic = [[UINotificationFeedbackGenerator alloc] init];
    [taptic prepare];
    [taptic notificationOccurred:UINotificationFeedbackTypeWarning];
}

NS_INLINE void tapticNotificationSuccess() {
    if (!VER_10_0_OR_LATER) return;
    if (![OHJGlobal shared].tapticEngineEnabled) return;
    UINotificationFeedbackGenerator *taptic = [[UINotificationFeedbackGenerator alloc] init];
    [taptic prepare];
    [taptic notificationOccurred:UINotificationFeedbackTypeSuccess];
}

NS_INLINE void tapticImpactWithStyle(UIImpactFeedbackStyle style) {
    if (!VER_10_0_OR_LATER) return;
    if (![OHJGlobal shared].tapticEngineEnabled) return;
    UIImpactFeedbackGenerator *taptic = [[UIImpactFeedbackGenerator alloc] initWithStyle:style];
    [taptic prepare];
    [taptic impactOccurred];
}

NS_INLINE void tapticImpactLight() {
    if (![OHJGlobal shared].tapticEngineEnabled) return;
    if (IPHONE_TAPTIC_OLD) {
        //AudioServicesPlaySystemSound(1519);
    }
    else {
        if (!VER_10_0_OR_LATER) return;
        tapticImpactWithStyle(UIImpactFeedbackStyleLight);
    }
}

NS_INLINE void tapticImpactMedium() {
    if (![OHJGlobal shared].tapticEngineEnabled) return;
    if (IPHONE_TAPTIC_OLD) {
        AudioServicesPlaySystemSound(1519);
    }
    else {
        if (!VER_10_0_OR_LATER) return;
        tapticImpactWithStyle(UIImpactFeedbackStyleMedium);
    }
}

NS_INLINE void tapticImpactHeavy() {
    if (![OHJGlobal shared].tapticEngineEnabled) return;
    if (IPHONE_TAPTIC_OLD) {
        AudioServicesPlaySystemSound(1520);
    }
    else {
        if (!VER_10_0_OR_LATER) return;
        tapticImpactWithStyle(UIImpactFeedbackStyleHeavy);
    }
}

NS_INLINE void tapticSelection() {
    if (![OHJGlobal shared].tapticEngineEnabled) return;
    if (IPHONE_TAPTIC_OLD) {
        //AudioServicesPlaySystemSound(1519);
    }
    else {
        if (!VER_10_0_OR_LATER) return;
        UISelectionFeedbackGenerator *taptic = [[UISelectionFeedbackGenerator alloc] init];
        [taptic prepare];
        [taptic selectionChanged];
    }
}
        [taptic notificationOccurred:UINotificationFeedbackTypeError];
    }
}

NS_INLINE void tapticNotificationWarning() {
    if (!VER_10_0_OR_LATER) return;
    UINotificationFeedbackGenerator *taptic = [[UINotificationFeedbackGenerator alloc] init];
    [taptic prepare];
    [taptic notificationOccurred:UINotificationFeedbackTypeWarning];
}

NS_INLINE void tapticNotificationSuccess() {
    if (!VER_10_0_OR_LATER) return;
    UINotificationFeedbackGenerator *taptic = [[UINotificationFeedbackGenerator alloc] init];
    [taptic prepare];
    [taptic notificationOccurred:UINotificationFeedbackTypeSuccess];
}

NS_INLINE void tapticImpactWithStyle(UIImpactFeedbackStyle style) {
    if (!VER_10_0_OR_LATER) return;
    UIImpactFeedbackGenerator *taptic = [[UIImpactFeedbackGenerator alloc] initWithStyle:style];
    [taptic prepare];
    [taptic impactOccurred];
}

NS_INLINE void tapticImpactLight() {
    if (IPHONE_TAPTIC_OLD) {
        //AudioServicesPlaySystemSound(1519);
    }
    else {
        if (!VER_10_0_OR_LATER) return;
        tapticImpactWithStyle(UIImpactFeedbackStyleLight);
    }
}

NS_INLINE void tapticImpactMedium() {
    if (IPHONE_TAPTIC_OLD) {
        AudioServicesPlaySystemSound(1519);
    }
    else {
        if (!VER_10_0_OR_LATER) return;
        tapticImpactWithStyle(UIImpactFeedbackStyleMedium);
    }
}

NS_INLINE void tapticImpactHeavy() {
    if (IPHONE_TAPTIC_OLD) {
        AudioServicesPlaySystemSound(1520);
    }
    else {
        if (!VER_10_0_OR_LATER) return;
        tapticImpactWithStyle(UIImpactFeedbackStyleHeavy);
    }
}

NS_INLINE void tapticSelection() {
    if (IPHONE_TAPTIC_OLD) {
        //AudioServicesPlaySystemSound(1519);
    }
    else {
        if (!VER_10_0_OR_LATER) return;
        UISelectionFeedbackGenerator *taptic = [[UISelectionFeedbackGenerator alloc] init];
        [taptic prepare];
        [taptic selectionChanged];
    }
}