开发者

custom buttons on uiwebview(iphone)

开发者 https://www.devze.com 2023-03-03 19:39 出处:网络
I want to add custom button on webView. they should also should be there when i try anything in url. how is it possible??

I want to add custom button on webView. they should also should be there when i try anything in url.

how is it possible??

basically i want to put buttons on uiwebView and they are custom buttons

//edited code...

I am doing this...here link is appearing but method is not getting called...and there wasnot any error in your code ..:)

NSString *imagePath = [[NSBundle mainBundle] resourcePath];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

NSString *HTMLData = @"<html<a href=\"button://dosomething\" class=\"buttonStyle\">Click me!</a>--></style><br><br>";

[webView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: [NSString stringWithFormat:@"file:/%@//",imagePath]]];

and then

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request   navigationType:(UIWebViewNa开发者_C百科vigationType)navigationType 
{
    // only do something if a link has been clicked...
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {     

        // check if the url requests starts with our custom protocol:
        if ([[[request URL] absoluteString] hasPrefix:@"button://"]) {
            // Do custom code
            return NO;
        } 
    }
    return YES;
}


You'll just have to use links and style them.

Something like:

<a href="#" class="buttonStyle">Click me!</a>

Have a look at http://www.cssbuttongenerator.com/, very easy to create your own button and let it generate the css code for you. You'll actualy have to click on the button create itself to generate the code.

Execute custom code by clicking on a link(button) in html

First of all you've got to conform to the UIWebViewDelegate protocol, and set the delegate accordingly.

Then implement shouldStartLoadWithRequest.

You button links should look like this:

<a href="button://dosomething" class="buttonStyle">Click me!</a>

We are using a custom protocol we make up: button://.

Now implement shouldStartLoadWithRequest like this:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{
    // only do something if a link has been clicked...
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {     

            // check if the url requests starts with our custom protocol:
        if ([[[request URL] absoluteString] hasPrefix:@"button://"]) {
            // Do custom code
            return NO;
        } 
    }

    return YES;
}

That's it.

0

精彩评论

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