2013-04-11 83 views
1

我现在正在开发一个Iphone应用程序,它需要用户登录Facebook并将其数据传递到webService并登录到我的游戏。基本上我的游戏是由HTML运行的,并且会在我的应用程序中通过UIWebView显示。运行时收到内存警告UIWebView

这里是我的代码加载web视图:

-(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)才能自动释放一些不需要的源,但我仍会在我的应用程序中收到内存警告。有什么我实施错误? 谢谢。

+0

在乐器+ heapshots中使用分配=> http://useyourloaf.com/blog/2011/03/08/using-heapshots-to-find-abandoned-memory.html – Peres 2013-04-11 09:01:56

回答

0
  1. 请确保您的HTML游戏使用桌面Web浏览器没有内存泄漏。 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);

会有所帮助。