Prevent IOS WebView from caching images - caching

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

Related

Cocoa: WKWebView local storage

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.

MWPhotoBrowser code update with directory

I am using the github project called MWPhotoBrowser that pulls image files from the web into a gallery on the iPhone. The problem is the code calls for the specific url of the image file. I want to pull images from inside a directory. Any ideas on how this can be done? Here's part of the code.
[photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:#"http://farm4.static.flickr.com/3567/3523321514_371d9ac42f_b.jpg"]]];
This solution involves use of the NSURL API is it can be used for web or local resources:
NSString *filePath = //path to your file
NSURL *filePathURL = [[NSURL alloc] initFileURLWithPath:filePath];
[photos addObject:[MWPhoto photoWithURL:filePathURL]];

how to make NSURL point to local dir?

reading Adium code today, found an interesting usage of NSURL:
NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:#"adium://%#/adium", [messageStyle.bundle bundleIdentifier]]];
[[webView mainFrame] loadHTMLString:[messageStyle baseTemplateForChat:chat] baseURL:baseURL];
I tried to log the url and got this adium://im.adium.Smooth Operator.style/adium, Then I created a blank project to see how to create such an NSURL but failed. When I sending loadHTMLString message to a webview's frame in my project, if the baseURL is nil, everything is fine, if not, I got a blank page in the view.
here is my code, the project name is webkit
NSURL *baseURL = [NSURL URLWithString:#"webkit://resource"];
//if baseURL is nil or [[NSBundle mainBundle] bundleURL], everything is fine
[[webView mainFrame] loadHTMLString:#"<html><head></head><body><div>helloworld</div></body></html>"
baseURL: baseURL];
[frameView setDocumentView:webView];
[[frameView documentView] setFrame:[frameView visibleRect]];
the question is how to make a self defined protocol instead of http://?
adium://%#/adium , first section is called protocol you can also register your protocol webkit: Take a look at How to map a custom protocol to an application on the Mac? and Launch Scripts from Webpage Links
[NSURLProtocol registerClass:[AIAdiumURLProtocol class]];
[ESWebView registerURLSchemeAsLocal:#"adium"];
I tried to find where did adium define the adium schema in the info.plist, unfortunately, there's nothing there, only some irc/xmpp protocols.
so I finally launched the debugger, and found the code above in the AIWebKitDelegate init method, anyway this is another way to register a self defined protocol~

how to make web browser in Xcode load a web page automatically

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!

Making a button change the webview

I'm trying to make a mac app using the apple developer tools, specifically xcode. I'm basically imbedding my website into in in a sort of iTunes-esque way- side bar at right, user clicks a button on side bar and webview web page changes. Problem is I'm having trouble trying to get the button to load a specific URL into the webview.
WebView* webView = [[WebView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];
NSString *urlAddress = #”http://www.example.com”;
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
So you first get the url as a string, turn it into an NSURL object, and then into an NSURLRequest object and finally give that to the webview object to load. You could also do this with a webview created in Interface Builder by ommiting the first line (where the webview is being initialised) and make sure you add the webView property to the viewcontroller as an IBOutlet and connecting it to the webview in Interface Builder.
Edit: It seems I was using iPhone syntax for the webview. Loading a url on a mac webview however is still similar, so all I've changed is the class name.

Resources