1
我想在webView的一個網站上按下特定的按鈕時將會調用一個事件(並且會優先於iPhone上的objective-c代碼)。 我認爲簡單的方法是監聽webView的HTTP請求。 我可以這樣做嗎?如何偵聽由WebView發送的HTTP請求(目標c)?
我想在webView的一個網站上按下特定的按鈕時將會調用一個事件(並且會優先於iPhone上的objective-c代碼)。 我認爲簡單的方法是監聽webView的HTTP請求。 我可以這樣做嗎?如何偵聽由WebView發送的HTTP請求(目標c)?
在你的HTML中,給URL一個特殊的方案。在這個例子中,該計劃是perform
:
<!-- ontouchstart tells WebKit to send us mouse events on a touch platform so we can use :active -->
<button class="button" ontouchstart="" onclick="window.open('perform:MAX')">MAX</button>
(你可以在這裏使用<a href
或其他技術這個例子使用onclick
很有用來自碼。)
設置你的控制器的委託UIWebView
。然後執行這個委託方法:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
if ([[url scheme] isEqualToString:@"perform"])
{
// url.resourceSpecifier will be @"MAX" in this example
// Do something with it.
return NO;
}
return YES;
}
COOL!非常感謝! – uriel 2012-02-02 16:23:14