J. Taiga
J. Taiga

Reputation: 655

How to implement multiple tabs like in safari?

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?

Upvotes: 4

Views: 3018

Answers (3)

Brad Parks
Brad Parks

Reputation: 72011

here's an opensource ios browser that supported multiple tabs

Radium Browser

Upvotes: 0

Ganesh G
Ganesh G

Reputation: 2061

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);
} 

Upvotes: 2

Hussein Dimessi
Hussein Dimessi

Reputation: 376

first of you need to use WKWebView instead of UIWebView, now to your point about the tabs, there isnt actually an already made component that will handle this for you but what you can do is intercept the requests from the webview's delegate (WKNavigationDelegate for WKWebView and UIWebViewDelegate for UIWebView) and whenever you feel that a request needs to be open in a separate webview then you block that request in the current webview and create a new view with a new webview nested in it and a close button and whatever else you feel a 'tab' needs to have and execute that request (the one you blocked in the original webview) in there

Upvotes: 3

Related Questions