【问题标题】:Received memory warning when running UIWebView运行 UIWebView 时收到内存警告
【发布时间】:2013-04-11 08:58:53
【问题描述】:

我现在正在开发一个 Iphone 应用程序,该应用程序需要用户登录 Facebook 并将他们的数据传递给 webService 并登录到我的游戏。基本上我的游戏是由 HTML 运行的,它会在我的应用程序中由 UIWebView 显示。

这是我加载 webView 的代码:

-(void)callWebView {
self.webView.delegate = self;
NSURL *url = [NSURL URLWithString:@"http://www.my_game_url"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestURL];
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;

if ( [ urlString isEqualToString: @"http://my_game_logout_url" ] ) {

    [myWebView stopLoading];
    [self.webView removeFromSuperview];

    MyAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    [appDelegate.session closeAndClearTokenInformation];
    [FBSession.activeSession close];

    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies]) {
        [storage deleteCookie:cookie];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];

    return NO;
    }
return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[self.webView stopLoading];

MyAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate.session closeAndClearTokenInformation];
[FBSession.activeSession close];
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
webView.delegate = nil;
[webView stopLoading];
}

- (void)dealloc {
[webView setDelegate:nil];
}

使用 UIWebView 运行我的游戏很好。问题是我稍后会收到内存警告,我的应用会崩溃。我必须启用自动引用计数 (ARC) 以自动释放一些不需要的源,但我仍然会在我的应用程序中收到内存警告。我有什么错误吗? 谢谢。

【问题讨论】:

标签: ios objective-c uiwebview


【解决方案1】:
  1. 使用桌面网络浏览器确保您的 HTML 游戏没有内存泄漏。 Chrome 开发者工具是一个方便的工具。 UIWebView 使用的 javascript 引擎性能不佳,您应该仔细优化您的游戏。

  2. 试试 [self.webView _setDrawInWebThread:YES];创建 UIWebView 之后。它是一个私有 API,能够节省 UIWebView 的 CPU 和内存使用。但您的应用可能会被 App Store 拒绝。

    SEL sel1 = NSSelectorFromString([NSString stringWithFormat:@"%@%@%@", @"_setDr", @"awInWebTh", @"read:"]);

    objc_msgSend(self.webView, sel1, YES);

会有帮助。

【讨论】:

    猜你喜欢
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    相关资源
    最近更新 更多