UIWebView does not free memory

I checked all related questions and answers on both Stackoverflow and the web, but I haven't come to a conclusion. I am developing an ipad application and what it does is request my web server to download the PDF as NSData and display it in the UIWebView. The problem is that the application (closes) segues from pdfviewer to mainviewcontroller, no memory is released which increases memory usage time. It starts at 14MB and then every time I go to the pdfviewer from the main controller it folds up. Since I am using arc, I cannot free it. I also clear the cache.

Mainview.m controller

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if([segue.identifier isEqualToString:@"showpdf"]){
        PdfViewer *pdfviewer=(PdfViewer*)[segue destinationViewController];
        pdfviewer.ReportID=TheReportID;
        pdfviewer.TabID=_TabID;
    }

}

      

PdfViewer.h

@property (strong, nonatomic) IBOutlet UIWebView *MainWebView;

      

PdfViewer.m

-(void)viewDidLoad
   {    
      NSURL *url = [NSURL fileURLWithPath:filePath];
      NSURLRequest *request = [NSURLRequest requestWithURL:url];
      [_MainWebView loadRequest:request];
   }

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if([segue.identifier isEqualToString:@"ShowMain5"]){
        MainViewController *mv=(MainViewController*)[segue destinationViewController];
        mv.TabID=_TabID;
    }

}
-(void)dealloc{
   NSLog(@"test");
}

      

I also tried to add UIWebview programmatically instead of IBoutlet but still couldn't solve the problem.

Programmatically (alternatively)

 @implementation PdfViewer

    UIWebView *webview;

    - (void)viewDidLoad
    {    
       webview=[[UIWebView alloc]initWithFrame:CGRectMake(35, 35, 768,959)];
       [webview loadRequest:request];
       [self.view addSubview:webview];
    }

      

When the user clicks the back button, everything I do goes back to the mainviewcontroller modally.All segues are modal FYIs.

As I figured out that pdfviewer does not call dealloc when it goes to mainviewcontroller, I checked if anyone had a similar problem with viewcontrollers before and I found this question UIViewController -dealloc method is not called now I think memory remains on that same level, but now it gives pdfviewer no window hierarchy error.

+3


source to share


3 answers


For my experience with UIWebView [s] the solution was to have a singleton instance for it, so at least this basic interface and properties

 // MXMWebView.h
 @interface MXMWebView : NSObject <UIWebViewDelegate, UIScrollViewDelegate> {

  }

  + (instancetype)sharedInstance;

  @property(nonatomic, strong) UIWebView *webView;

      

and



 //MXMWebView.m
  @implementation MXMWebView

  @synthesize webView;
  @synthesize delegate;

  #pragma mark - Singleton Methods

  + (instancetype)sharedInstance {
     static id _sharedInstance = nil;
     static dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{
          _sharedInstance = [[self alloc] init];
     });

   return _sharedInstance;

      

}

+2


source


As I understood, pdfviewer does not call dealloc when it goes to mainviewcontroller. I checked if anyone had a similar problem with viewcontrollers before and I found this question UIViewController -dealloc method was not called . So I added

-(void)viewDidAppear:(BOOL)animated{
   [super viewDidAppear:animated];
   self.view.window.rootViewController=self;
}

      

both mainviewcontroller.m and pdfviewer.m as well as



-(void)dealloc{

     [_MainWebView loadHTMLString:@"" baseURL:nil];
     [_MainWebView stopLoading];
     [_MainWebView setDelegate:nil];
     [_MainWebView removeFromSuperview];

    [[NSURLCache sharedURLCache]removeAllCachedResponses];
    [[NSURLCache sharedURLCache] setDiskCapacity:0];
    [[NSURLCache sharedURLCache] setMemoryCapacity:0];

}

      

method pdfViewer.m and it solved the problem. Thank you all for their input.

+1


source


Set webview delegate to zero while segues from pdfviewer to mainviewcontroller

[_MainWebView setDelegate:nil];

      

--- change

[_MainWebView removeFromSuperView];

      

0


source







All Articles