I want to invoke IOS system delegate(jailbreaked) in tweak(dylib) for all app, for example
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
I try to do this in tweak.xm:
void* Run(void *arg) {
CLLocationManager *manager = (CLLocationManager *)arg;
MY_LOG(@"thread->%@", manager);
for (int i = 0; i < 1000; i++) {
if (manager.delegate != nil) {
MY_LOG(@"okay1");
if ([manager.delegate respondsToSelector:@selector(locationManager:didUpdateLocations:)]) {
//...
}
}
sleep(1);
}
return NULL;
}
%hook CLLocationManager
- (id)init {
CLLocationManager *manager = %orig;
MY_LOG(@"CLLocationManager->init: %@", manager);
pthread_t handle = 0;
pthread_create(&handle, NULL, Run, (void*)manager);
return manager;
}
%end
My thinking is: hook all CLLocationManager's init function, get its reference, and pass it to a new thread, and in these threads, I can invoke its delegate methods...However, this tweak crash at here:
[manager.delegate respondsToSelector:@selector(locationManager:didUpdateLocations:)]
If remove the line above, the tweak work well, and the log "okay1" printed.
So my question is:
1 Is this idea feasible? If yes, how to improve it so let it no crash and works well?
2 If this idea is bad, how to achieve the goal(invoke locationManager:didUpdateLocations:)?