简体   繁体   中英

Calling AngularJS controller function from ios uiwebview

I have dashboard.html page and DashboardController.js is associated with dashboard.html. I am loading page in UIWebview and now I want to call a angularjs function from DashboardController.js from uiwebview. It does not get called.

Below is the code which I am trying in iOS

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSURL *url = [request URL];
    NSLog(@"URL:%@",url);
    if (![Utility isInternetAvailable]) {
        return FALSE;
    }
    NSString *jsCallBack = [NSString stringWithFormat:@"DashboardController.test()"];
    NSString *response =  [webView stringByEvaluatingJavaScriptFromString:jsCallBack];
    NSLog(@"%@",response);
    return YES; // Return YES to make sure regular navigation works as expected.
}

I also tried without Controller reference after making sure that dashboard.html has been loaded inside uiwebview. NSString *jsCallBack = [NSString stringWithFormat:@"test()"];

While test() is js function inside my DashboardController.js scope.

function test (){
  alert('sendCredentialsToNativeApp');
}

Thanks in advance.

This test() method should be called if you have defined without controller reference in the controller JS. However, this will only be available after page readystate because you cannot call any method from the controller until its loaded completely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM