调用原生代码(objective)从UIWebView HTML (JavaScript)



我读过

使用UIWebView从HTML代码中调用objective - c代码中的方法

他们做

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

这里,我的代码的一个问题是,我没有

UIWebView的委托的shouldStartLoadWithRequest方法,因为我简单地实现UIWebView直接在AppDelegate如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{       
    UIWebView *view = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    NSString* path = [[NSBundle mainBundle] pathForResource:@"main" ofType:@"html" inDirectory:@"www"];
    [view loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
    view.scalesPageToFit = NO;
    UIViewController *controller = [[UIViewController alloc] init];
    controller.view = view;
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = controller;
    [self.window makeKeyAndVisible];
    return YES;    
}

我喜欢这种方式,因为代码和文件的结构更简单。

所以我的问题是,是否有可能实现call-native-from-webView在这种风格?

或者,我需要有一个独立的UIWebView控制器/委托吗?

如果可能的话,你能给我示范一下怎么做吗?

我没有做太多的objective - c代码和MVC模型,所以感谢如果你告诉我的方式。谢谢。

webView:shouldStartLoadWithRequest:navigationType:方法将在任何被设置为webview委托的对象上被调用。

最新更新