[ad_1]
Earlier than iOS 16, we might detect the person is utilizing dictation to enter textual content by checking primaryLanguage variable of the textInputMode being dictation. Easy sufficient. However this methodology does not work no extra. In iOS 16, primaryLanguage is the language chosen for dictation.
In my Distant Mouse & Keyboard for Mac / PC app (https://remote4mac.app), I must detect whether or not the person is utilizing dictation to attend for the entire phrase earlier than sending it to the pc. So I began in search of different strategies and spent an excellent couple of hours to no avail. However then I remembered methodology swizzling. iOS utilizing UIDictationController to manage dictation interactions, however it’s a non-public class. So I had to determine find out how to swizzle its strategies, and luckily, you are able to do that by including a class and utilizing class strategies for it like this
@interface NSObject(PrivateSwizzleCategory)
- (void)swizzled_setupForDictationStart;
- (void)swizzled_stopDictation;
@finish
@implementation NSObject(PrivateSwizzleCategory)
- (void)swizzled_setupForDictationStart {
[(NSObject *)self swizzled_setupForDictationStart];
[[NSNotificationCenter defaultCenter] postNotificationName:@"UIDictationControllerWillStart" object:nil];
}
- (void)swizzled_stopDictation {
[(NSObject *)self swizzled_stopDictation];
[[NSNotificationCenter defaultCenter] postNotificationName:@"UIDictationControllerDidStop" object:nil];
}
@finish
Then you definately simply must run a code that may swizzle these strategies with the UIDictationController like this
Methodology unique, swizzled;
unique = class_getInstanceMethod(objc_getClass("UIDictationController"), NSSelectorFromString(@"setupForDictationStart"));
swizzled = class_getInstanceMethod(objc_getClass("NSObject"), @selector(swizzled_setupForDictationStart));
method_exchangeImplementations(unique, swizzled);
unique = class_getInstanceMethod(objc_getClass("UIDictationController"), NSSelectorFromString(@"stopDictation"));
swizzled = class_getInstanceMethod(objc_getClass("NSObject"), @selector(swizzled_stopDictation));
method_exchangeImplementations(unique, swizzled);
and you might be achieved. Simply take heed to the brand new notifications and act accordingly in your code.
So now its time for me to see if the app will go Apple evaluation.
[ad_2]
