【问题标题】:tweak openURL in UIApplication (CaptainHook)在 UIApplication (CaptainHook) 中调整 openURL
【发布时间】:2012-04-07 08:51:32
【问题描述】:

如何在 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 中的一些配置吗?

【问题讨论】:

    标签: objective-c ios5 hook jailbreak tweak


    【解决方案1】:

    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];
        ...
    }
    

    【讨论】:

      【解决方案2】:

      从这些转储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;
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 2023-03-23
        • 1970-01-01
        • 1970-01-01
        • 2014-05-28
        • 1970-01-01
        • 1970-01-01
        • 2012-06-29
        • 1970-01-01
        • 2011-11-07
        相关资源
        最近更新 更多