【问题标题】:How to use javascript code in objective C in iOS?如何在 iOS 的目标 C 中使用 javascript 代码?
【发布时间】:2017-07-21 07:59:03
【问题描述】:

我有一个 web 视图,加载时我必须在其中打开在线聊天。我有一个用于在线聊天的 javascript 代码。我很困惑我该如何使用它。我试图打开苹果网站,它工作正常,但我如何在 Objective c 中使用 javascript 代码。我对语言很陌生,这是我第一次使用它。我在一个文件夹中添加了 javascript 代码并将其添加到我的应用程序的资源文件夹中。

【问题讨论】:

    标签: javascript ios objective-c


    【解决方案1】:

    试试这个。

    NSString *scriptURL = [[NSBundle mainBundle] pathForResource:@"JavaScript" ofType:@"js"];
    NSString *scriptContent = [NSString stringWithContentsOfFile:scriptURL encoding:NSUTF8StringEncoding error:nil];
    
    [wkWebView evaluateJavaScript: scriptContent completionHandler:^(id _Nullable content, NSError * _Nullable error)
     {
         if (error) {
             NSLog(@"Error: %@",error);
             return;
         }
    
         NSLog(@"Content: %@",content);
     }];
    

    【讨论】:

    【解决方案2】:

    结帐the apple doc。它会帮助你更多。

    这里是 the best example 在 webView 中使用脚本。

    这是您可以在代码中使用的东西:

    NSString *path;
    NSBundle *thisBundle = [NSBundle mainBundle];
    path = [thisBundle pathForResource:@"first" ofType:@"html"];
    NSURL *instructionsURL = [NSURL fileURLWithPath:path];
    [webView loadRequest:[NSURLRequest requestWithURL:instructionsURL]];
    
     NSString * jsCallBack = [NSString stringWithFormat:@"myFunction()"];
     [webView stringByEvaluatingJavaScriptFromString:jsCallBack];
    
    [webView stringByEvaluatingJavaScriptFromString:@"myFunction()"];
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多