使用潜意识禁用自动更正

时间:2014-05-21 16:31:33

标签: ios testing autocorrect subliminal

我在潜意识测试中遇到问题,由于设备默认设置,文本会自动更正。

我是否可以使用Subliminal在整个设备上禁用自动更正功能? 我可以以某种方式导航到设备设置吗?

1 个答案:

答案 0 :(得分:0)

Subliminal无法导航到设备设置,但您的测试可以覆盖默认文本字段自动更正类型method swizzling

// In the test that exercises the text fields,
// or a base test class of multiple tests that exercise text fields
#import <objc/runtime.h>

- (void)setUpTest {
    // `dispatch_once` in case this is a base test class,
    // where this method would be called multiple times
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        struct objc_method_description autocorrectionTypeMethodDescription = protocol_getMethodDescription(@protocol(UITextInputTraits), @selector(autocorrectionType), NO, YES);
        // (Re)implement `-[UITextField autocorrectionType]` to return `UITextAutocorrectionTypeNO`.
        IMP noAutocorrectionTypeIMP = imp_implementationWithBlock(^(UITextField *_self){ return UITextAutocorrectionTypeNo; });
        class_replaceMethod([UITextField class], @selector(autocorrectionType), noAutocorrectionTypeIMP, autocorrectionTypeMethodDescription.types);
    });
}