0

我正在使用 Phonegap/Cordova(2.1.0) 创建 IOS 应用程序。我想从 Objective-C 函数调用 Phonegap 的 index.html 文件中的 JavaScript 函数。所以,我正在创建一个“UIWebView”类的实例“theWebView”,如下所示:

AppDelegate.h 中的代码:

#import <UIKit/UIKit.h>

#import <Cordova/CDVViewController.h>
#import "sqlite3.h"

@interface AppDelegate : NSObject < UIApplicationDelegate > {
    NSString* invokeString;
    
}

@property (nonatomic, strong) IBOutlet UIWebView* theWebView;

AppDelegate.m 中的代码:

#import "AppDelegate.h"
#import "MainViewController.h"
#import <Cordova/CDVPlugin.h>


@implementation AppDelegate

@synthesize theWebView;

调用JavaScript函数的代码如下:

// to call javascript function 
    [theWebView stringByEvaluatingJavaScriptFromString:@"myJavascriptFunction()"];

代码已成功构建。但是,在 index.html 的脚本标签中定义的 JavaScript 函数“myJavascriptFunction()”没有被调用。我怎样才能做到这一点?

我还附加了 Web 视图加载代码,它存在于 MainViewController.m 文件中。因为,我使用的是 Cordova 2.1.0,所以 ViewController 和 AppDelegate
类存在单独的文件。

/**
 Called when the webview finishes loading.  This stops the activity view and closes the imageview
 */
- (void)webViewDidFinishLoad:(UIWebView *)theWebView 
{
    NSLog(@"webViewDidFinishLoad function");
    
    // Black base color for background matches the native apps
    theWebView.backgroundColor = [UIColor blackColor];
    
    return [ super webViewDidFinishLoad:theWebView ];
}

- (void)webViewDidStartLoad:(UIWebView *)theWebView 
{
    NSLog(@"webViewDidStartLoad function");    
    return [ super webViewDidStartLoad:theWebView ];
}

/**
 * Fail Loading With Error
 * Error - If the webpage failed to load display an error with the reason.
 */
- (void)webView:(UIWebView *)theWebView didFailLoadWithError:(NSError *)error 
{
    NSLog(@"didFailLoadWithError function");
    return [ super webView:theWebView didFailLoadWithError:error ];
}

/**
 * Start Loading Request
 * This is where most of the magic happens... We take the request(s) and process the response.
 * From here we can redirect links and other protocols to different internal methods.
 */
- (BOOL)webView:(UIWebView *)webView2 
shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    NSLog(@"shouldStartLoadWithRequest function");
    
    // Intercept custom location change, URL begins with "js-call:"
    if ([[[request URL] absoluteString] hasPrefix:@"js-call:"]) {
        
        // Call the given selector
        [[[UIApplication sharedApplication] delegate] performSelector:NSSelectorFromString(@"resetBadgeCount")];
        
        // Cancel the location change
        return NO;
    }
    
    // Accept this location change
    return YES;
    
}

index.html 中围绕 javascript 函数“myJavascriptFunction”的代码:

<!DOCTYPE html>
<html>
    <head>
        <title>Index Page</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <link rel="stylesheet" href="iui/iui.css" type="text/css" media="all"/>
        <link rel="stylesheet" title="Default" href="iui/t/default/default-theme.css"  type="text/css" media="all"/>
        <link rel="stylesheet" href="iui/t/default/iui-custom.css" type="text/css" media="all"/>
        <script type="text/javascript" src="cordova-2.1.0.js"></script>
        <script type="text/javascript">

            function myJavascriptFunction()
            {
                alert('myJavascriptFunction');
                
                return;
            }
            
            </script>
        
    </head>
    <body id="bd">
    </body>
</html>    

我附加了 Objective-C 代码来加载 index.html 文件,然后调用 JavaScript 函数:

NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"www"];
    NSURL *baseURL = [NSURL fileURLWithPath:path];
    NSString *myFile = [path stringByAppendingPathComponent:@"index.html"];
    
    NSLog(@"base url= %@",myFile);
    
    NSData *myFileData = [[NSData alloc] initWithContentsOfFile:myFile];
    NSString* myFileHtml = [[NSString alloc] initWithData:myFileData encoding:NSASCIIStringEncoding];
    
    [theWebView loadHTMLString:myFileHtml baseURL:baseURL];
    
    // to call javascript function 
    [theWebView stringByEvaluatingJavaScriptFromString:@"onDeviceReady()"];

我在想问题出在最后一行。

4

1 回答 1

1

我不认为你对这段代码有问题。在这里一切看起来都很好,但是如果工作正常,您应该检查myJavascriptFunction()您是否正在调用。您可以从带有 JavaScript 控制台的浏览器尝试,看看它在手动调用时是否可以工作。

例如,适用于我的 myJavascriptFunction 代码是:

        function myJavascriptFunction()
        {
            window.location = "http://www.google.com/";
        }

虽然警报代码没有显示任何警报,特别是没有UIAlertView

关于警报,我在这里更正了,UIAlertView 显示在 Javascript 警报事件上。

于 2013-01-03T08:14:12.480 回答