2013-04-11 118 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);

會有所幫助。