IOS8:UIWebView内存泄漏

时间:2015-01-08 13:28:04

标签: ios memory-leaks uiwebview

应用说明: 我想开发一个应用程序,在设定的时间内显示网站。如果时间过去了,它将加载下一个网站等。它必须以循环方式运行:如果上一个网站显示,它首先出现。

AppDelegate未被更改。 Storyboard有一个带有ViewController的导航控制器,其中包括UIWebView:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIWebViewDelegate>

@end

ViewController.m:

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIWebView *mWebView;

@end

@implementation ViewController

NSMutableArray *mSites;
NSInteger mIndex;

- (void)viewDidLoad {
    NSLog(@"%s", __PRETTY_FUNCTION__);

    [super viewDidLoad];

    mSites = [[NSMutableArray alloc] init];
    [mSites addObject:@"http://www.bild.de"];
    [mSites addObject:@"http://www.spiegel.de"];

    [self setWebView];
    mIndex = -1;

    [self showNext];
}

- (BOOL)prefersStatusBarHidden
{
    NSLog(@"%s", __PRETTY_FUNCTION__);

    return YES;
}

- (void)setWebView
{
    NSLog(@"%s", __PRETTY_FUNCTION__);

    [(self.mWebView.subviews)[0] setBounces:NO]; // No overscroll
    [self.mWebView setDelegate:self];
}

- (void)freeMemory
{
    NSLog(@"%s", __PRETTY_FUNCTION__);

    [self.mWebView loadHTMLString:@"" baseURL:nil];
    [self.mWebView stopLoading];
    [self.mWebView setDelegate:nil];

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

- (void)webViewDidFinishLoad: (UIWebView *)webView
{
    NSLog(@"%s", __PRETTY_FUNCTION__);

    // Clear cache
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
}

- (void)handleTimer: (NSTimer *)timer
{
    NSLog(@"%s", __PRETTY_FUNCTION__);

    [self freeMemory];
    [self setWebView];

    [self showNext];
}

- (void)showNext
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    // Loop
    if (mIndex < [mSites count] - 1) {
        mIndex++;
    } else {
        mIndex = 0;
    }

    NSString *site = [mSites objectAtIndex:mIndex];

    NSURL *url = [NSURL URLWithString:site];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.mWebView loadRequest:request];

    [NSTimer scheduledTimerWithTimeInterval:15
                                     target:self
                                   selector:@selector(handleTimer: )
                                   userInfo:nil
                                    repeats:NO];
}

- (void)didReceiveMemoryWarning 
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

    [self.mWebView reload];
}

@end

故障: 我用于测试iPad Air(A1474)并使用了两个网站:http://www.bild.dehttp://www.spiegel.de。大约过后由于内存使用,30分钟崩溃了应用程序。该应用程序使用超过300Mb。

我找到了更多处理内存泄漏问题的解决方案,并将它们集成到我的代码中:

- (void)freeMemory
{   
    [self.mWebView loadHTMLString:@"" baseURL:nil];
    [self.mWebView stopLoading];
    [self.mWebView setDelegate:nil];

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

- (void)webViewDidFinishLoad: (UIWebView *)webView
{   
    // Clear cache
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
}

- (void)didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning]; 
    [self.mWebView reload];
}

但解决方案没有生效。该应用程序运行时间不超过一小时。

让某人有一个解决方案,我可以为每个人提供至少一天的应用运行时间吗?

提前感谢您的建议。

0 个答案:

没有答案