1

是否可以通过内置触控板(如新款 MacBook)或蓝牙设备(如 Magic Trackpad 2)来确定 Mac 是否支持 Force Touch?

如果 Mac 支持 Force Touch,我想提供特定于 Force Touch 的首选项,但如果 Force Touch 不可用,则不显示(或禁用)这些首选项。

在分隔符之后的部分,您可以在此处链接的图片中看到我想到的选项。(抱歉,嵌入图片本身不起作用)。

喜好

因此,不显示首选项不会限制没有强制触摸的用户,它只会让拥有它的用户配置它应该如何工作,而这些设置对没有它的用​​户来说毫无用处。

有没有办法做到这一点?

谢谢你和亲切的问候,马特

编辑:它在Objective-C中。

4

1 回答 1

0

我想到了:

+ (BOOL)isForceTouchCapable
{
    if (![[self class] isAtLeastElCapitan])
        return NO;

    io_iterator_t iterator;

    //get default HIDDevice dictionary
    CFMutableDictionaryRef mDict = IOServiceMatching(kIOHIDDeviceKey);

    //add manufacturer "Apple Inc." to dict
    CFDictionaryAddValue(mDict, CFSTR(kIOHIDManufacturerKey), CFSTR("Apple Inc."));

    //get matching services, depending on dict
    IOReturn ioReturnValue = IOServiceGetMatchingServices(kIOMasterPortDefault, mDict, &iterator);

    BOOL result = YES;
    if (ioReturnValue != kIOReturnSuccess)
        NSLog(@"error getting matching services for force touch devices");
    else
    {
        //recursively go through each device found and its children and grandchildren, etc.
        result = [[self class] _containsForceTouchDevice:iterator];
        IOObjectRelease(iterator);
    }

    return result;
}

+ (BOOL)_containsForceTouchDevice:(io_iterator_t)iterator
{
    io_object_t object = 0;
    BOOL success = NO;
    while ((object = IOIteratorNext(iterator)))
    {
        CFMutableDictionaryRef result = NULL;
        kern_return_t state = IORegistryEntryCreateCFProperties(object, &result, kCFAllocatorDefault, 0);
        if (state == KERN_SUCCESS && result != NULL)
        {
            if (CFDictionaryContainsKey(result, CFSTR("DefaultMultitouchProperties")))
            {
                CFDictionaryRef dict = CFDictionaryGetValue(result, CFSTR("DefaultMultitouchProperties"));
                CFTypeRef val = NULL;
                if (CFDictionaryGetValueIfPresent(dict, CFSTR("ForceSupported"), &val))
                {
                    Boolean aBool = CFBooleanGetValue(val);
                    if (aBool) //supported
                        success = YES;
                }
            }
        }

        if (result != NULL)
            CFRelease(result);

        if (success)
        {
            IOObjectRelease(object);
            break;
        } else
        {
            io_iterator_t childIterator = 0;
            kern_return_t err = IORegistryEntryGetChildIterator(object, kIOServicePlane, &childIterator);
            if (!err)
            {
                success = [[self class] _containsForceTouchDevice:childIterator];
                IOObjectRelease(childIterator);
            } else
                success = NO;

            IOObjectRelease(object);
        }
    }

    return success;
}

只需调用 + (BOOL)isForceTouchCapable,如果 Force Touch 设备可用(Magic Trackpad 2 或内置的 force-touch-trackpad),它将返回 YES,如果没有,它将返回 NO。

对于那些对它是如何产生感兴趣的人,我在我的博客上写了一个示例项目。

于 2015-11-16T11:20:15.860 回答