How to implement multiple tabs like in safari?

Viewed 2101

I am currently creating a google-cast app for iPhone. Its all working like I wanted, I can cast a video on GoogleCast. My only problem is that if the website user visiting has popup ads, the webview automatically redirects to that ad site (sometimes multiple redirects at once).

Since UIWebView doesnt have adBlock function, I thought maybe I should implement multiple tabs feature, so that the ad can be opened in a new tab and the user can close the ad tab manually.

Thanks in advance.

Has any of you have experience with this?

3 Answers

In decidePolicyForNavigationAction delegate method you can declare a new WKWebView class and add it as a subview to previous WKWebView class based on navigationType or request. For example see below code snippet how am doing and which works fine to me.

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{

if (navigationAction.navigationType == WKNavigationTypeLinkActivated) { // OR if (![navigationAction.request.URL.Path isEqualToString:@"Previous URL Path"]) {

    // Add cancel button at top of new tab
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(5, 5, 50, 20);
    [btn setTitle:@"Close" forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor redColor];
    [btn addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];

    CGRect nFrm = oldWebVw.frame; // Set frame as per your requirement.
    nFrm.origin.y = 100;
    nFrm.size.height -=120;

    newWebVw = [[WKWebView alloc] initWithFrame:nFrm];
    newWebVw.backgroundColor = [UIColor blueColor];
    [newWebVw addSubview:btn];
    [oldWebVw addSubview:newWebVw];
    [newWebVw loadRequest:navigationAction.request];
    decisionHandler(WKNavigationActionPolicyCancel); // You must cancel the policy else the new request loades on previous WKWebView class
    return;
  }
  decisionHandler(WKNavigationActionPolicyAllow);
} 

here's an opensource ios browser that supported multiple tabs

Radium Browser

Related