Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective-C到JavaScript调用未发生_Javascript_Objective C_Ios_Cordova - Fatal编程技术网

Objective-C到JavaScript调用未发生

Objective-C到JavaScript调用未发生,javascript,objective-c,ios,cordova,Javascript,Objective C,Ios,Cordova,我正在使用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.

我正在使用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;
代码正在成功生成。但是,未调用index.html的脚本标记中定义的JavaScript函数“myJavascriptFunction()”。我该怎么做

我还附加了web视图加载代码,它存在于MainViewController.m文件中。由于我使用的是Cordova 2.1.0,因此ViewController和AppDelegate存在单独的文件
上课

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>    

我认为问题在于最后一行。

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

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

        function myJavascriptFunction()
        {
            window.location = "http://www.google.com/";
        }
警报代码未显示任何警报,特别是未显示
UIAlertView


我在这里纠正了关于警报的错误,UIAlertView显示在Javascript警报事件上。

显示加载WebView的代码Hi im try thing相同的事情,您最终是如何让它工作的?它在index.html的脚本标记中。我不确定它是否会在这里查找javascript函数。我还附上index.html代码。这个问题对我来说很重要。它已经成为一个巨大的瓶颈,如果不解决它,我就无法取得更大的进步。谢谢。就像我说的,错误在Javascript代码中。您不能通过在Javascript中调用
alert
来调用
UIAlertView
。要调用它,您需要在delegate.so中实现它,您的意思是说alert()将不起作用。但除此之外,在js函数中编写的普通javascript代码也可以工作,对吗?如果你能详细说明一下,我将不胜感激。无论如何,我的代码中不需要alert()。这只是为了测试。然后它必须与你的phonegap/cordova有关。在此选中同时使用UIAlertView和重定向的示例项目。检查这里的示例:@AmarKulo hi感谢您的回复,我使用了不同的方法解决了问题。
/**
 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;
    
}
<!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>    
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()"];
        function myJavascriptFunction()
        {
            window.location = "http://www.google.com/";
        }