views:

129

answers:

1

Hi,

I was wondering how I can override the UIActionSheet that appears when tapping and holding your finger on a link in an UIWebView (it shows the link and an open and copy button). I need to add a button to the alert, but I don't know how to customize it.

Does anyone have an idea on how to solve this?

+2  A: 

Given that there exists no such API, you still could customize the sheet in a non-standard-api-like-way without using private api. The easiest will probably be to observe the subviews of that webview and when one appears (like a popup) check it's class and if it's such a popup, to customize it. Here's how I'd try that.

Still: this is hacky and might easily break in the next update.

Add observation:

[myWebView addObserver:self forKeyPath:@"subviews" options:0 context:@"popup"];

Then observe:

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
    if (context == @"popup") {
        for (UIView *view in [object subviews]) {
            if ([view isKindOfClass: [UIAlertView class]])
                 [self customizeAlert: (UIAlertView*)view];
        }
    }
    [super observeValueForKeyPath:keyPath
                         ofObject:object
                           change:change
                          context:context];
}

Then do your customization in such a method:

- (void)customizeAlert:(UIAlertView*)alert { ... }
Max Seelemann
out of some reason, `- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context` never gets called although I added the observer :/
David Schiefer
Did you set a breakpoint to check that the observation actually gets called? One possible solution is to simply subclass UIWebView and to override addSubview: etc. In there just trigger the kvo-notification on your own and it hopefully should work.
Max Seelemann