How to disable iOS 11 dragging within the whole app?

前端 未结 3 1176
名媛妹妹
名媛妹妹 2021-01-03 07:22

For security reasons I want to disable the new iOS 11 drag & drop feature within my whole app. More specifically the drag part.

In iOS 11 it\'s happening by defa

3条回答
  •  庸人自扰
    2021-01-03 07:37

    Found a solution. It is to method swizzle the isEnabled method of UIDragInteraction to return NO in the situations your app may need. Note that normally it's not a good idea to method swizzle.

    @implementation UIDragInteraction (TextLimitations)
    + (void)load
    {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
    
            Class class = [self class];
    
            SEL originalSelector = @selector(isEnabled);
            SEL swizzledSelector = @selector(restrictIsEnabled);
    
            Method originalMethod = class_getInstanceMethod(class, originalSelector);
            Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    
            BOOL didAddMethod =
            class_addMethod(class,
                            originalSelector,
                            method_getImplementation(swizzledMethod),
                            method_getTypeEncoding(swizzledMethod));
    
            if (didAddMethod) {
                class_replaceMethod(class,
                                    swizzledSelector,
                                    method_getImplementation(originalMethod),
                                    method_getTypeEncoding(originalMethod));
            } else {
                method_exchangeImplementations(originalMethod, swizzledMethod);
            }
        });
    }
    
    -(BOOL)restrictIsEnabled
    {
        if (restrictedCondition)
        {
            return NO;
        }
        return [self restrictIsEnabled];
    }
    

提交回复
热议问题