Using the same UIWebView across different viewControllers

TL;DR: I need to implement a singleton UIWebVIew that’s shared across multiple ViewControllers. The question contains all of my approaches so far.


  • How to keep logged in session in UIWebview?
  • Opening a new UIViewController when a link on a UIWebView is clicked
  • How to autoplay a YouTube video in a UIWebView
  • uiwebview not resizing after changing orientation to landscape
  • Swift stringByEvaluatingJavaScriptFromString
  • How to do authentication in UIWebView properly?
  • @property (strong, nonatomic) UIWebView *singleWebView;


    @property (weak, nonatomic) IBOutlet UIWebView *webView;
    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear: animated];
        NSLog(@"Removing webview from the first VC");
        [self.webView removeFromSuperview];
        self.webView.delegate = nil;
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        appDelegate.singleWebView = self.webView;
        self.webView = nil;
    - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
        NSURL *URL = [request URL];
        if ([[URL absoluteString] isEqualToString:@"myapp://postsShow"]) {
            [self performSegueWithIdentifier:@"postsShowSegue" sender:self];
            return false;
        return true;


    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [self.viewContainer addSubview: appDelegate.singleWebView];

    In the first view controller, I load my single page app, which shows a list of posts first. Then, the user views one of the posts and he will be redirected to the second View Controller. I want to reuse the UIWebView that was used in the first ViewController in the second ViewController so I don’t have to reload the webpage.

    The problem is that, after addSubview the UIWebView doesn’t seem to be loaded. I only see a UIView that I use as a container. It would be really helpful if you could give me some debugging points.

    enter image description here

    Solutions Collect From Internet About “Using the same UIWebView across different viewControllers”

    I finally figured out. The retain count was always 2, even after I removed the UIWebView instance from the FirstViewController. I removed the UIWebView from the storyboard and created it programmatically when the firstViewController was loaded. I left everything else the same. Finally I could start reusing the webView.