开发者

How to determine if the user had clicked on a UIWebView

I need to get the url that a UIWebView is displaying after the user clicks on it once.

I've tried putting a button which calls a method which determines the url, under the UIWebView, but this way the button doesn't work. I've tried putting a button over a UIWebView but this way it gives not the url after the click, but the starting url.

Can you help me, please?

Tha开发者_开发技巧nks in advance!


The best way to know if somebody has clicked over your view is implementing touchesEnded method: First declare a rect with the size of your webView in your viewDidLoad for example:

touchRect=CGRectMake(YourWebView.startPositionX, YourWebView.StartPositionY,YourWebView.width, YourWebView.height);

and then implement the touchesEnded:

(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if(touch){

        CGPoint location = [touch locationInView: [touch view]];

        if (CGRectContainsPoint(touchRect,  location)){

            //do whatever

        }
    }

}

This way you will know if you have make a touch inside your webView.


To display the current location of UIWebView (location of main page):

NSURLRequest* webViewRequest = [myWebView request];
if (webViewRequest) {
    NSURL* webViewURL = [webViewRequest URL];
    NSString* webViewLocation = [webViewURL absoluteString];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜