I load HTML file which loads images using Javascript files to WebView. Every time when I load html file to Web view it shows me some memory increased in Activity Monitor. Even, when I release Webview then also it does not release the memory.
I am not caching the resources in Webview. But, when I used instruments to analyse the memory , it shows me images resources are cached in memory which does not get released.
I tried to load same HTML files in Safari browser, I found the same behavior.
Can anyone please guide me where I am missing? How can I optimize the memory loading to WebView?
Do not use Activity Monitor for profiling memory performance in your app.
Use the Instruments app and the various memory-related tools it provides, such as Allocations and Leaks. You will then be able to determine if your app is truly leaking.
More inputs how I try to avoid caching in WebView.
I am using below piece of code to remove caching.
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
WebPreferences *the_pPrefs = [m_pWebView preferences];
[the_pPrefs setCacheModel:WebCacheModelDocumentViewer];
[the_pPrefs setUsesPageCache:NO];
[m_pWebView setResourceLoadDelegate:self];
Even I handle resource load request in below delegate method.
- (NSURLRequest *)webView:(WebView *)sender
resource:(id)identifier
willSendRequest:(NSURLRequest *)aRequest
redirectResponse:(NSURLResponse *)redirectResponse
fromDataSource:(WebDataSource *)dataSource
{
NSURL * url = [aRequest URL];
NSURLRequest * cachelessRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:100];
return cachelessRequest;
}
I hope I doing the right things to avoid caching. Please give your thoughts for the same.
Related
I am trying to decrease to load time of in app websites. Our application is currently using WKWebView and right now we have this code to load the webpage
var webView : WKWebView
if let urlStr = self.webUrl, url = NSURL (string: urlStr) {
let requestObj = NSURLRequest(URL: url)
self.webView.loadRequest(requestObj)
}
Is there a way to improve the performance? Would using a dispatch method help? I attempted to use dispatch_async and didn't notice any changes. Also is there a way to track how long it took the webpage to load?
WKWebView wasn't the source of the problem. It is now apparent that the lag came from other sources in the application and WKWebView has little impact on the actual performance of loading pages. SafariWebView, although easier to implement lacks customization and seems to be slower than WKWebView.
I am trying to get local storage working with WKWebView.
I have created a small demo project. It is a Mac app that shows a window with some buttons and a WKWebView.
The three buttons show three variations of the same page: the Sticky Notes WebKit demo:
Remote loads the Sticky Notes WebKit demo page in the WKWebView.
Local loads the same content from a copy in the app bundle.
Safari loads the Sticky Notes WebKit demo in your browser.
The first two do not work; the latter does.
What should happen is it should appear with one default sticky note, and an enabled New Note button will add new sticky notes.
In the Remote and Local variations, no note appears, and the New Note button is disabled, indicating that it couldn't access the local storage.
I'm hoping that I'm just doing something wrong in loading the WKWebView, but it's possible that this just isn't supported.
Here's my code for the Remote variation:
WKWebViewConfiguration *config = [WKWebViewConfiguration new];
config.websiteDataStore = [WKWebsiteDataStore defaultDataStore];
self.webView = [[WKWebView alloc] initWithFrame:self.container.frame configuration:config];
self.webView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
[self.container addSubview:self.webView];
NSURL *url = [NSURL URLWithString:#"https://webkit.org/demos/sticky-notes/"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
Any assistance much appreciated!
Update: It turned out that while the local storage mechanism I was using does work in Safari and not WKWebView, it was easier than expected: just use the localStorage JavaScript variable.
I have created plugin for Mail.app on Mac OS.
I'm using WebView to display web pages, all would be fine but web pages load slowly.
Then I created test cocoa application to compare loading time.
I was surprised when the test application loads page ~5 times faster.
In developer bar I saw my test application receives 304 code that indicates "the resource for the requested URL has not changed and cached resource can be used".
In contrast to the test application the plugin always receives 200 http code and loads resource again.
Maybe I should specify to use a cache in the webview, or I have some bundle permissions problems.
In the plugin, I tried to specify SharedURLCache like this
NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:1024*1024*20
diskCapacity:1024*1024*5
diskPath:NSHomeDirectory()];
[NSURLCache setSharedURLCache:cache];
Then I tried subscribe to the ResourceLoadDelegate on the WebView and change request object like this
- (NSURLRequest *)webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)dataSource
{
if ([request cachePolicy] != NSURLRequestReturnCacheDataElseLoad)
{
return [NSURLRequest requestWithURL:[request URL]
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:[request timeoutInterval]];
} else {
return request;
}
}
Also I tried to change properties on WebView
[[webView preferences] setUsesPageCache:YES];
[[webView preferences] setCacheModel:WebCacheModelPrimaryWebBrowser];
but it's all not working.
Thanks for help.
Situation:
A native IOS5 App downloads a website from FTP to local storage and a webview displays the downloaded page. If there is a new version on the FTP-Server, the App will automaticly download the new version and override the old one in the local storage. Now the webview should load the new version.
Problem:
When the new version overwrides the old version, the name of some images will be the same. Now the webview will load every image from cache insteat from filesystem. -.-
Question:
Is there a possibility to prevent the webview loading all images from cache or clear the whole cache? What I cant do is changing the html or js of the downloaded page. So the approache to add a timestamp behinde every image (test.img?timestamp=23444) cant be done. The change has to be done in the IOS App.
What ive tryed already but failed:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
NSURL *url = [NSURL fileURLWithPath:[ftpPath stringByAppendingPathComponent:#"index.html"]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:1.0];
[[NSURLCache sharedURLCache] removeCachedResponseForRequest: urlRequest];
[webView stopLoading];
[webView loadRequest:urlRequest];
[webView reload];
webView.delegate = self;
Thank you so much for any help, i dont get it.
Cya Nando
I am building a web browser in Xcode cocoa application, using the interface builder. I have it all working when it comes to typing in web address and clicking go.
but I would like for it to automatically load a web page instead of seeing white when I open my project, I looked in inspector to see if you can set a specific web address but I couldn't find anything to do this.
How about if you load the URL that you would like in the viewdidload function:
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *targetURL = [NSURL fileURLWithPath:WebsitePath];
NSURLRequest *WebRequest = [NSURLRequest requestWithURL:targetURL];
[self.WebViewer loadRequest:WebRequest];
self.WebViewer.scalesPageToFit = YES;
}
Maybe you can try to load the WebView when the application finish launching,just add the URL Request in the applicationDidFinishLauching parameter,else if your WebView is on the MainView you can add a loadingScreen that appear while the webView is loading!
Lucky!