需求:
需求是想通过交换方法的方法hook住类似于获取地理位置和访问照片一类的弹框UIAlertController的,随机选一个点击。
操作步骤:
自己试过在app里面通过category+交换方法,hook住自己弹出的UIAlertController的ViewDidApear方法,延时随机点击是可以的。
但是系统的获取地理位置和访问照片一类的弹框还是hook不住,想请教下是app的权限问题导致hook不到系统弹出来的UIAlertController,还是说系统的UIAlertView压根不是UIAlertController?
Code:
#import "CocoaHook.h"
@interface UIAlertController()
{
NSMutableArray * _actions;
}
- (void)_dismissAnimated:(BOOL)arg1 triggeringAction:(UIAlertAction* )action;
@end
@implementation UIAlertController (Hook)
+ (void)hook
{
OBJC_EXCHANGE_METHOD_OC([self class], @selector(viewDidAppear), @selector(hooViewDidAppear));
}
- (void)hooViewDidAppear
{
NSLog(@"--------ALERT controller HOOK");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"%@",self.actions);
NSInteger buttonIndex = arc4random_uniform([self.actions count]);
NSLog(@"dismiss");
[self _dismissAnimated:YES triggeringAction:[self.actions objectAtIndex:buttonIndex]];
});
});
}
@end
method swizzling:
bool OBJC_EXCHANGE_METHOD_OC(Class orgClass,
SEL orgMethod,
SEL newMethod)
{
Method newMethodIns = class_getInstanceMethod(orgClass, newMethod);
Method orgMethodIns = class_getInstanceMethod(orgClass, orgMethod);
if(!orgMethodIns || !newMethodIns)
{
return false;
}
// method_exchangeImplementations(orgMethodIns, newMethodIns);
const char* typeEncoding = method_getTypeEncoding(newMethodIns);
IMP oldIMP = class_replaceMethod(orgClass, orgMethod, method_getImplementation(newMethodIns), typeEncoding);
assert(oldIMP);
class_replaceMethod(orgClass, newMethod, oldIMP, typeEncoding);
return true;
}