2012-05-06 42 views
2

我想知道如何從javascript調用目標c方法。例如,我在我的myFile.js這樣定義的javascript功能:從javascript文件調用目標c方法

function() { 
alert('test'); 
....   // How i can an objective c function here ??? 
} 

我也有一個UIWebView:

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { 
.... 
// what to do here ?????? 
} 

謝謝您的回答

+0

有很多問題都像你問。請搜索它們。 – Sakares

回答

3

只需設置位置的東西您會發現並檢查request.URL是否與此匹配。

function nav(f){ 
location.href = "methodCall"; 
} 

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { 
    if([request.URL.relativeString isEqualToString:@"methodCall"]){ 
     //call method here 
     return false; //so it doesn't navigate anywhere 
    } 
    return true; //if you want every other link to work normally. 
} 

注:我沒有測試過這一點,但我想它會工作,雖然通過request.URL.relativeString返回的URL可能會有點不同,所以只設置一個斷點,看看它是什麼。