0

Phonegap/Cordova(2.1.0) を使用して IOS アプリを作成しています。Phonegap の index.html ファイル内の JavaScript 関数を Objective-C 関数から呼び出したい。そのため、以下のように「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 の script タグに定義されている JavaScript 関数「myJavascriptFunction()」が呼び出されません。これどうやってするの?

MainViewController.m ファイルに存在する Web ビューのロード コードも添付します。私は 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;
    
}

javascript 関数「myJavascriptFunction」の周りの index.html のコード:

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

index.html ファイルをロードし、JavaScript 関数を呼び出す目的の C コードを添付しています。

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 に答える