通过JS控制ViewController

JS与IOS原生应用程序看似没有联系,其实却暗藏玄机。

URL便是连接它们的桥梁,JS对应的是window.location,IOS对应的是NSURLRequest对象的mainDocumentURL.relativePath属性。

我做了一个Demo,看首页

底部是一个UITabBarController,它是window.rootViewController。UITabBarController的子层是一个UINavigationController,

中间是一个UIWebView,用来加载html文件。

文件系统:

关键代码:

AppDelegate.m代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    _viewController.title = @"Home";
    _viewController.tabBarItem.image = [UIImage imageNamed:@"house.png"];
    
    _nav = [[[UINavigationController alloc] initWithRootViewController:_viewController] autorelease];
    
    MoreViewController *moreViewCon = [[[MoreViewController alloc] init] autorelease];
    moreViewCon.title = @"More";
    moreViewCon.tabBarItem.image = [UIImage imageNamed:@"alarm.png"];
    
    _tabBarController = [[[UITabBarController alloc] init] autorelease];
    _tabBarController.viewControllers = [NSArray arrayWithObjects:_nav,moreViewCon,nil];
    self.window.rootViewController = _tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

ViewController里包含UIWebView,它实现UIWebViewDelegate的方法

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

ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]]; 
}

#pragma mark - webViewDelegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //获取URL并且做比较,判断是否触发了JS事件,注意有"/"
    if ([request.mainDocumentURL.relativePath isEqualToString:@"/clicked"]) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"you click me" message:@"clicked" delegate:nil cancelButtonTitle:@"sure" otherButtonTitles:@"cancel", nil];
        [alertView show];
        [alertView release];
        
        SecondViewController *secondViewCon = [[[SecondViewController alloc] init] autorelease];
        [self.navigationController pushViewController:secondViewCon animated:YES];
        
        return false;
    }
    return  true;
}

index.html
<html>
<head>
<title>Home</title>
<script>
    function jump()
    {
        var clicked=true; 
    	window.location="/clicked";     //改变URL  注意:要使用"/"分隔符
        alert("js alert :jump");
    }

</script>
</head>
<body>
<h1>Page One</h1>
<button οnclick="jump()">Click me</button>
</body>
</html>


点击按钮效果:

    



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值