2011-02-18 4 views

답변

0

자바 스크립트를 실행하려면 현재보기에 uiwebview를 추가 할 필요가 없습니다. uiwebview를 화면에 표시하지 않고 원하는 와치를 실행할 수 있습니다.

uiwebview에 javascript를 사용하여 닫으라는 알림을 보내려면 첫째로 당신은 당신의 UIWebView의 대리인으로 클래스를 설정해야합니다 :

var iframe = document.createElement("IFRAME"); 
    iframe.setAttribute("src", "my-special-frame:uzObjectiveCFunction"); 
    document.documentElement.appendChild(iframe); 
:
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 
} 

는 그런 다음 자바 스크립트 당신은 우리가 목표 - C 코드를 기대하고 특별한 접두어가 HTTP 요청을해야

이 예에서는 특수 접두어가 포함 된 URL로 프레임을 엽니 다. 당신은 또한 간단하게 만들 수 있습니다 :

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

희망이 당신의 의문을 돕는다! 행운을 빕니다!

관련 문제