I have a web app that works fine in Safari (it uses local storage and saves the settings and restores them).
I created a web view in Xcode 4.5.2 that loads my web app. I know by default web view doesn't support local storage so I added code to enable it but now the app doesn't work at all.
My code in AppDelegate.m:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
WebPreferences *prefs = [WebView preferences];.
[prefs _setLocalStorageDatabasePath:#"~/Library/TestApp/LocalStorage"];
NSString *path = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"];
NSURL* fileURL = [NSURL fileURLWithPath:path];
NSURLRequest* request = [NSURLRequest requestWithURL:fileURL];
[self.webView.mainFrame loadRequest:request];
}
#end
This part is what I added to enable local storage:
WebPreferences *prefs = [WebView preferences];.
[prefs _setLocalStorageDatabasePath:#"~/Library/TestApp/LocalStorage"];
I get the following error: "Expected expression" - "No known class method for selector 'preferences'"
-preferences is an instance method on WebView, not a class method. You'll want to do WebPreferences *prefs = [self.webView preferences] to retrieve the preferences for your WebView. In addition to calling -[WebPreferences _setLocalStorageDatabasePath:], I believe that you'll also want to call -[WebPreferences setLocalStorageEnabled:] to ensure that local storage is enabled.
Related
I wrote a simple Mac OS application. The view only contains a hidden WebView and a NSTextView. At a certain time 100 URLs will be parsed, one after another. A URL is loaded in the WebView, some DOM tree parsing happens, rinse and repeat.
Everything works fine except there are some major differences in loading times when the computer is in idle (no user activity). When i am using the computer a URL takes about 2 seconds to load. When i am not using the computer for some time, a URL takes about 1 minute to load.
When i come back to the computer after some time and see in the TextView that the last URLs took about a minute, the next ones immediately will be loaded in seconds. So it has to be something with the user activity on the system. Right? Anybody knows why?
I am on OS X 10.9.4.
Here is the code how i load the URLs:
#pragma mark -
- (void)processNextUrl
{
// No url, stop
if (_processQueue.count == 0)
return;
// Process url
NSString *urlString = [_processQueue objectAtIndex:0];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[_webView mainFrame] loadRequest:request];
}
#pragma mark - WebFrameLoadDelegate
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
// Page ready for parsing?
if ([[sender mainFrame] isEqual:frame] == NO)
return;
// Append date, time and url to TextView
NSString *url = [[[[frame dataSource] request] URL] absoluteString];
[self logText:url];
// Parser does some DOM parsing and outputs to TextView
Parser *parser = [[Parser alloc] initWithWebFrame:frame delegate:self];
[parser parse];
}
#pragma mark - ParserDelegate Methods
- (void)parser:(Parser *)parser didFinishParsingUrl:(NSString *)url
{
[_processQueue removeObjectAtIndex:0];
[self processNextUrl];
}
I'd bet AppNap is kicking in and starving your process of resources.
You may want to either disable AppNap in the Finder's Get Info window of your app - a temporary test to see if I'm right of course - or just use NSProcessInfo's beginActivityWithOptions:reason: to tell the system that the activity was user initiated and the app should not be throttled.
I am creating a Cocoa web browser, and I noticed that if the webview loads a nil location, it just loads about:home. Since I have not set it, the page just appears white. Is there a way I can change what about:home looks like. Even if it is a simple .rtf file or something.
I looked around, but don't see any way to do this. Am I suppose to create a NSURL and set it to whatever file?
Thanks. Oh, and if code is ever needed, I would be glad to add it.
Try something like this:
// Inside your App Delegate
-(void)applicationDidFinishLaunching:(NSNotification *)notification {
// Assuming WebView is called myWebView
NSString *currentURL = [myWebView mainFrameURL];
if(!currentURL) {
NSString *homeResource = [[NSBundle mainBundle] pathForResource:#"home" ofType:#"html" inDirectory:#"default"];
NSURL *homeURL = [NSURL fileURLWithPath:homeResource];
NSURLRequest *request = [NSURLRequest requestWithURL:homeURL];
[myWebView loadRequest:request];
}
}
You'll need to have a pre-made file called home.html within a folder called default located in the Resources section of your project.
I suppose this isn't exactly replacing about:home, but you can always check for about:home and handle that appropriately as well.
In iOS 6 the QLPreviewController no longer loads a PDF from a URL. It works fine in iOS 5. I have implemented the QLPreviewControllerDataSource methods as documented here.
#pragma mark - QLPreviewControllerDataSource
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
return 1;
}
- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index;
{
NSURL *fileURL = [NSURL URLWithString:#"http://www.bliley.net/XTAL/PDF_Instructions/Test_File.pdf"];
return fileURL;
}
This works perfectly in iOS 5, however in iOS 6 the console outputs:
Couldn't issue file extension for path: /XTAL/PDF_Instructions/Test_File.pdf
Have you tried using fileURLWithPath instead of URLWithString? I had other issues that were fixed by doing so.
Also not sure if QLPreviewController will handle remote URLs. If not, you could download the file and then display it.
I downloaded the file from remote url and saved locally, then I display the PDF using the QLPreviewController .In iOS 6 its working.
First i saved the file from remote url using the following code :
NSString *local_location;
NSString *path = [[NSBundle mainBundle] pathForResource:#"sampleData" ofType:#"plist"];
path = NSTemporaryDirectory();
local_location= [path stringByAppendingPathComponent:[NSString stringWithFormat:#"My_Invoice.pdf"]];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString: remoteurl]];
[request setDownloadDestinationPath:local_location];
[request startSynchronous];
For showing the Pdf :
QLPreviewController* preview = [[QLPreviewController alloc] init];
preview.dataSource = self;
[self presentModalViewController:preview animated:YES];
QLPreviewController delegate methods are :
- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{
return 1;
}
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
return [NSURL fileURLWithPath:local_location];
}
I am having a similar issue and seems like it might stem from a stricter enforcement of the file-type URL of QLPreviewItem
#property (readonly) NSURL *previewItemURL;
Discussion
This property is used by a Quick Look preview controller to get an item’s URL. In typical use, you would implement a getter method in your preview item class to provide this value.
The value of this property must be a file-type URL.
If the item is not available for preview, this property’s getter method should return nil. In this case, the Quick Look preview controller displays a “loading” view.
Availability
Available in iOS 4.0 and later.
Declared In
QLPreviewItem.h
UPDATE: I have opened a bug with Apple dealing with this issue for iOS 6 and it seems they have aced it as a bug so may offer a fix in the near future. The bug I opened had to do with using custom NSURLProtocols for the preview, but may apply to other aspects as well.
Link to class
But note that QLPreviewController expects a URL to a local resource
You would need to download and save the PDF file locally first and then create a proper file URL to the local file.
I'm very new to objective-c, but I've learned how to create a uiwebview. I'm trying to open a keynote file in uiwebview using this code from Apple's dev site:
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
// Calling -loadDocument:inView:
[self loadDocument:#"mydocument.key.zip" inView:self.myWebview];
However, I'm getting the error "Use of undeclared identifier 'self'" Do I need to declare the identifier 'self' in WebViewController.h? Any tips would be greatly appreciated.
No, you do not, self is basically "this" in Java and other languages. Looks to me that myWebView is not a property and therefore does not have a getter, and self.myWebview is trying to get the getter. You could try:
[self loadDocument:#"mydocument.key.zip" inView:myWebview];
Other possibility that comes to mind is that the method implementation for loadDocument is after the call to the method and not before. This really is not a problem unless you don't have the method declared in your interface like this:
#interface myClass
- (void)loadDocument:(NSString *)sender inView:(UIWebView) webView;
I am using the following code to fetch the contents of a web page but it needs to follow all the network rules etc as defined in the user's system prefs
NSURL *url = [NSURL URLWithString:#"http://thetalkingcloud.com/static/ping_desktop_app.php?v=1.0"];
NSError *theNetworkError;
NSString *content = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&theNetworkError];
Specifically, does stringWithContentsOfURL connect using the system wide proxy settings? (if there are any defined)
I just did some tcpdumps between Safari and an Obj-C program using stringWithContentsOfURL. Safari respected my proxy while stringWithContentsOfURL did not.