【问题标题】:Call JavaScript from Objective-C in AppDelegate在 AppDelegate 中从 Objective-C 调用 JavaScript
【发布时间】:2017-10-15 00:23:40
【问题描述】:

有没有办法从 Objective-C 调用设备令牌?

我刚刚创建了一个 cordova ios 项目,因为我只有 Objective-C 的基本知识。会自动生成AppDelegate.m

@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

如何从 index.html 文件中调用在 inappbrowser 中加载的 javascript 函数

<!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>

【问题讨论】:

  • 我认为我们需要您提供更多信息:如果您想使用 Javascriot 将其注入 HTML:HTML/Javascript 的外观如何,是否有此功能?此外,Push 服务需要什么样的“设备令牌”,请详细说明您的需求并提供一些附加信息。
  • 鳞翅目我改进了详细的问题

标签: javascript ios objective-c cordova


【解决方案1】:

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

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

[webview evaluateJavaScript:str completionHandler:nil];

【讨论】:

    【解决方案2】:

    首先:

    确保您的 webview 已经加载了 HTML 页面(如果您还需要修改 DOM 中的某些 UI 元素,它可能也应该已经呈现。加载本身是异步执行的。这意味着您无法实现它在一个进程中,但必须等到它准备好进行操作。

    当你在应用程序的start方法中通过注入调用函数时,我认为webview还没有准备好。

    改为对 iOS 应用程序使用 Delegate 的方法: https://developer.apple.com/reference/uikit/uiwebviewdelegate

    UIWebViewDelegate 会通知您某些状态(-changes)。例如:

    因此,我建议使用以下委托属性扩展您的 AppDelegate(或创建新委托):

    @property (nonatomic) UIWebViewDelegate *webDelegate;
    

    相应地设置 webView 的委托:

    - (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];
    }
    

    并且还实现其中一种 webview 委托方法(在这种情况下,当 webview 完成加载时:

    @implementation AppDelegate
    - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
    {
        ...
    }
    
    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
        NSString * jsCallBack = [NSString stringWithFormat:@"myFunction()"];
        [webView stringByEvaluatingJavaScriptFromString:jsCallBack];
    }
    
    @end
    

    【讨论】:

      猜你喜欢
      • 2014-06-20
      • 2013-01-11
      • 2017-09-27
      • 1970-01-01
      • 2017-01-09
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      • 2010-12-12
      相关资源
      最近更新 更多