1

我如何在 UIApplication 中加载钩子?

#import <CaptainHook/CaptainHook.h>
#import <SpringBoard/SpringBoard.h>

CHDeclareClass(SBAlertWindow);
CHOptimizedMethod(1, self, void, SBAlertWindow, displayAlert, id, alert) {
    NSLog(@"load displayAlert!");
    CHSuper(1, SBAlertWindow, displayAlert, alert);
}

CHDeclareClass(UIApplication)
CHOptimizedMethod(1, self, void, UIApplication, openURL, NSURL *, url) {
    NSString *linkToOpen = [[NSURL alloc] initWithString:[url absoluteString]];
    NSLog(@"dont load link: %@", linkToOpen);
    CHSuper(1, UIApplication, openURL, url);        
}

CHConstructor {
    CHLoadLateClass(SBAlertWindow);
    CHHook(1, SBAlertWindow, displayAlert);

    CHLoadLateClass(UIApplication);
    CHHook(1, UIApplication, openURL);
}

在我使用 SBAlertWindow 进行的测试中。工作完美。但 UIApplication 中的 openURL 不挂钩。

需要在makefile中进行一些配置吗?

4

2 回答 2

0

KHookObjectWrapper.h

#import <Foundation/Foundation.h>

@interface KHookObjectWrapper : NSObject

+ (void)initialize;
- (BOOL)fake__openURL:(NSURL *)url;

@end

KHookObjectWrapper.m

#import "KHookObjectWrapper.h"
#import <objc/objc.h>
#import <objc/runtime.h>

@implementation KHookObjectWrapper

+ (void)initialize
{
Method openURL = class_getInstanceMethod([UIApplication class], @selector(openURL:));
Method openURLMy = class_getInstanceMethod([self class], @selector(openURLHooked:));
IMP openURLImp = method_getImplementation(openURL);
class_addMethod([UIApplication class], @selector(fake__openURL:), openURLImp, method_getTypeEncoding(openURL));
IMP openURLSelfImp = method_getImplementation(openURLMy);
class_replaceMethod([UIApplication class], @selector(openURL:), openURLSelfImp, method_getTypeEncoding(openURL));
}

//fake method, never run.
- (BOOL)fake__openURL:(NSURL *)url
{
abort();
return YES;
}

- (BOOL)openURLHooked:(NSURL *)url
{
NSLog(@"openURL param:url=%@", [url absoluteString]);
return [self fake__openURL:url];
}

@end

最后,在你的主 appDelegate.m 中添加代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [KHookObjectWrapper initialize];
    ...
}
于 2013-12-13T15:45:10.340 回答
0

从这些转储https://github.com/Fr0stDev/SpringBoard-iOS5-Headers导入具有该实现的头文件。

我不知道使用的 excatct 方法,但它看起来像

-(void)openURl:(NSUrl*)url;

在您的调整中添加该方法并做您想做的事

-(void)openURl:(NSUrl*)url{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Open Url?" message:@"Tweak 
over rides the method and called an alertview. What do you want to do?" delegate:self 
cancelButtonTitle:@"No Thanks" otherButtonTitles:@"Copy Link", @"Save",@"ViewSaved", 
nil];
[alert show];
[alert release];

return Url;


}
于 2012-04-17T10:46:57.727 回答