UIWebView + JavaScript2

In the past, I explained how to hook Javascript.
This is Javascript hook Objective-C.
Create JavaScript and detect Javascript event from native code and call Objective-C.
In this case, Web page is created by ourselves. How about others?
If there is no hook point, we cannot hook.

Add JavaScript Event

Use UIWebViewDelegate and stringByEvaluatingJavaScriptFromString

#pragma mark - UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
    self.webViewLoadingCount++;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    self.webViewLoadingCount--;
    
    if (self.webViewLoadingCount == 0) {
        // Finished loading
        [webView stringByEvaluatingJavaScriptFromString:@"JavaScript"];
    }
}

JavaScript and Detect is already written in Javascript hook Objective-C.

Example

This example shows how to add JavaScript in existing page.
Add additional hook and detect and call Objective-C.

JavaScriptHackViewController.h

@interface JavaScriptHackViewController : UIViewController<UIWebViewDelegate>
@end

JavaScriptHackViewController.m

#import "JavaScriptHackViewController.h"

@interface JavaScriptHackViewController ()

@property (nonatomic) UIWebView *webView;
@property (nonatomic) int webViewLoadingCount;

@end

static NSString *javaScript = @"var box=document.getElementById('sb_form_q'); box.onfocus=function(){ open(\"boxfocus:\"); this.style.backgroundColor='blue'; return false;};";

@implementation JavaScriptHackViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.webView = [[UIWebView alloc] init];
    self.webView.delegate = self;
    self.webView.scalesPageToFit = YES;
    self.webViewLoadingCount = 0;
    [self.view addSubview:self.webView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    
    NSURL *url = [NSURL URLWithString:@"http://www.bing.com"];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:req];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillLayoutSubviews {
    self.webView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}


#pragma mark - UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
    self.webViewLoadingCount++;
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

    if ( [request.URL.scheme isEqualToString:@"boxfocus"] ) {
        NSLog(@"Objective-C");
        //[webView stringByEvaluatingJavaScriptFromString:@"var url=document.URL; alert(url);"];
    }
    return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    self.webViewLoadingCount--;
    
    if (self.webViewLoadingCount == 0) {
        // Load JavaScript
        [webView stringByEvaluatingJavaScriptFromString:javaScript];
        [webView stringByEvaluatingJavaScriptFromString:@"var url=document.URL; alert(url);"];
    }
}
@end

Result

Bing Text color changed and NSLog was called.
iOS JavaScript Hack