简体   繁体   中英

Call JavaScript from Objective-C in AppDelegate

Is there any way to call device token from Objective-C?

I just created a cordova ios project since I only have basic knowledge in Objective-C. It will automatically generate AppDelegate.m class

@implementation AppDelegate
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    self.viewController = [[MainViewController alloc] init];

    NSString * jsCallBack = [NSString stringWithFormat:@"myFunction()"];
    [webView stringByEvaluatingJavaScriptFromString:jsCallBack];  

    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
@end

How can a javascript function be called from index.html file, that loaded in inappbrowser

<!DOCTYPE html>
<html>
  <head>
    <title>Device Ready Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for device API libraries to load
    //
           myFunction()
               {
              alert('called');
           }
        function onLoad() {
            document.addEventListener("deviceready", onDeviceReady, false);
        }
        // device APIs are available
        //
        function onDeviceReady() {
            // Now safe to use device APIs
        }
        </script>
      </head>
      <body onload="onLoad()">
      </body>
    </html>

First of all:

make sure that your webview has already loaded the HTML page (in case you also need to modify certain UI elements in the DOM, it should maybe also be rendered already. The loading itself is performed asynchronously. That means you cannot implement it in one single process, but have to wait until it is ready for manipulation.

As you call the function through injection in the application's start method, I think the webview is not yet prepared.

Instead use the Delegate's approach for iOS applications: https://developer.apple.com/reference/uikit/uiwebviewdelegate

The UIWebViewDelegate informs you about certain state(-changes). eg:

So I would recommend extending your AppDelegate (or create a new delegate) with the following delegate property:

@property (nonatomic) UIWebViewDelegate *webDelegate;

Set the delegate of the webView accordingly:

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    self.viewController = [[MainViewController alloc] init];
    // I just used your very own code for this, I think you will have a separate property for this webView ?
    webView.delegate = self;
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

And also implement one of the webview delegate methods (in this case, when a webview finished loading:

@implementation AppDelegate
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    ...
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString * jsCallBack = [NSString stringWithFormat:@"myFunction()"];
    [webView stringByEvaluatingJavaScriptFromString:jsCallBack];
}

@end

您可以在目标C的任何位置调用javascript函数:

NSString *str=@"window['function_name'].apply(window,['param1','param2'])";

[webview evaluateJavaScript:str completionHandler:nil];

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