2011-02-18 1 views

答えて

0

javascriptを実行するために、現在のビューにuiwebviewを追加する必要はありません。 uiwebviewを画面に表示せずに、あなたが望むワイバーを実行することができます。

uiwebviewにjavascriptを使用するように通知するには、まず、あなたはあなたのUIWebViewの代理人としてあなたのクラスを設定する必要があります。

NSURL *url = [NSURL URLWithString:@"https://myWebWithJavascript.html"]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
//if your are not to display webview, frame dimensions does not mind 
UIWebView uiwebview = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320, 480)]; 
[uiwebview setDelegate:self]; //remember that your .h has to implement <UIWebViewDelegate> 
[uiwebview loadRequest:request]; 

//then you implement notifications: 
//this is executed when uiwebview has been loaded 
- (void)webViewDidFinishLoad:(UIWebView *)webView 
{ 
    //put here code if you wanna do something with uiwebview once has finished loading 
} 
//this one is executed if your request returns any error on loading page 
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error 
{ 
//put here code if you wanna manage html errors 
} 
//this one it the ONE you will use to receive messages from javascript code: 
//this function is executed every time an http request is made 
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest: (NSURLRequest*)req navigationType:(UIWebViewNavigationType)navigationType { 
     //we check every time there is an http request if the request contains 
     //an special prefix that indicates it is not a real http request, but 
     //a comunication from javascript code 
     if ([[[req URL] absoluteString] hasPrefix:@"my-special-frame"]) { 
      //so thats it- javascript code is indicating me to do something 
      //for example: closing uiwebview: 
      [webview release]; //probably it would be cleverer not to kill this way your uiwebview...but it is just an example 
      return NO; //that is important because avoid uiwebview to load this fake http request 
     } 
return YES; //that means that it will load http request that skips the if clause 
} 

は、その後、あなたのJavaScriptであなただけの私たちはObjective-Cのコードに期待しているという特殊な接頭辞でHTTPリクエストを行う必要があります。

var iframe = document.createElement("IFRAME"); 
    iframe.setAttribute("src", "my-special-frame:uzObjectiveCFunction"); 
    document.documentElement.appendChild(iframe); 

この例では、特別な接頭辞を含むURLのフレームを開きます。

document.location.href = my-special-frame:uzObjectiveCFunction; 

これはあなたの疑問を助けることを願っています!がんばろう!

関連する問題