天天看點

通過JS控制ViewController

JS與IOS原生應用程式看似沒有聯系,其實卻暗藏玄機。

URL便是連接配接它們的橋梁,JS對應的是window.location,IOS對應的是NSURLRequest對象的mainDocumentURL.relativePath屬性。

我做了一個Demo,看首頁

通過JS控制ViewController

底部是一個UITabBarController,它是window.rootViewController。UITabBarController的子層是一個UINavigationController,

中間是一個UIWebView,用來加載html檔案。

檔案系統:

通過JS控制ViewController

關鍵代碼:

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>
           

點選按鈕效果:

通過JS控制ViewController
通過JS控制ViewController
通過JS控制ViewController

繼續閱讀