Opening Keynote files in uiwebview - objective-c

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;

Related

Web View Not Saving HTML5 Local Storage Settings

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.

How do you set what about:home is on webView, Obj-C

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.

AVAudioPlayer refuses to play anything, but no error, etc

This is the simplest possible AVAudioPlayer code, and it's just failing to play anything back. No errors, nothing in the console at all, the file is definitely being found as if I change the URL string to something which isn't there, I do get a crash. What am I doing wrong here? I've tried it with and without the delegate, as well as with and without prepareToPlay, and I can't get anything out. I've tried various sound files, too. Really tearing my hair out on this one!
#implementation ViewController
- (IBAction)playSound:(id)sender
{
NSURL *soundLocation = [[NSURL alloc] initWithString:#"Lot01.wav"];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundLocation error:nil];
[audioPlayer setDelegate:self];
[audioPlayer play];
}
#end
Turned out it was an issue with ARC releasing, I fixed it by adding a #property and #synthesize pair for the AVAudioPlayer in question, and declaring it as strong. It got rid of all of these errors, and played the file with no problems.
It seems like it's not correctly getting the path to your file, maybe try something like this.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"Lot01" ofType:#"wav"];
NSURL *url = [NSURL fileURLWithPath:filePath];
then init the AVAudioPlayer exactly how you were except in that case it'd be withContentsOfURL:url.

Declare an NSString With Format Specifiers and use it as a URL to open in UIWebview

I have an int containing a number. I am wanting to declare an NSString so I can use use format specifiers when assigning a value to it.
I thought it might be something like this:
NSString[NSString stringWithFormat] myString;
myString = [#"http://myurl.com/%d",myInt];
I gather this is not the case, so question one is: How do I declare an NSString that can handle format specifiers and then assign it a value using format specifiers? The purpose of this NSString is to hold a URL, exactly like the second line above.
Question two is, How do I then use this string as a URL to open in a UIWebView?
I assume I use something like this:
[webView loadRequest:
Sadly, this is as far as my knowledge stretches. Is there a way I can tell my UIWebView (webView above) to use the NSString with the URL I mentioned earlier?
I intend on having the NSString as a global variable, as it will be assigned it's value inside a C function. And 'webView' will use it inside a (what I think is a) method. All of this code is in the same file, the Delegate.m file. It is all executed on launch of the application.
Your string should look like this:
NSString *myString = [NSString stringWithFormat:#"http://myurl.com/%d", myInt];
What you missed: adding the * to indicate a pointer, and thinking that you had to/could first state that the string would have a format and then later state the format. It all happens at once, creating the string with the specified format.
Edited to add NSURL
To create a url you're creating an object of class NSURL, like this:
NSURL *myURL = [[NSURL alloc] initWithString:myString];
And then you create the url request:
NSURLRequest *request = [NSURLRequest requestWithURL:myURL];
And finally, tell your webView to load the request:
[webView loadRequest:request];
For your first part:
NSString *myString = [NSString stringWithFormat:#"http://myurl.com/%d", myInt];
Then, based on a tutorial from iphonesdkarticles.com:
//Create a URL object.
NSURL *url = [NSURL URLWithString:myString];
//URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];

nsstring - out of scope

-(void)loadWebAdress:(NSString*)textAdress {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
adressurl=[NSString stringWithFormat:#"http://%#", textAdress];
NSURL *url=[NSURL URLWithString:adressurl];
NSURLRequest *requestObj=[NSURLRequest requestWithURL:url];
[webview loadRequest:requestObj];
}
although url takes it's value from adressurl, adressurl is all the time out of scope when checked in debugger. what is going on? i would like to use it in some other places too. not only in this method. because is out of scope, the app crashes. But, I repeat, it is the one who gives its value to url.
It depends on where the adressurl variable is declared. Since it is generated from the method parameter, it seems odd that you'd want to use it elsewhere in the code. If you have it as a static variable, it could be getting stomped by other code. (For example, if you set it to one value in this method, and another elsewhere, it's not unusual to get crashes, especially if you don't coordinate or synchronize between the two. One reason to avoid globals/statics.) You're free to use the same local variable name in different methods if you like.
Here's what I'd suggest doing instead: (Note: I've fixed some typos.)
- (void) loadWebAddress:(NSString*)textAddress {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#", textAddress]];
[webview loadRequest:[NSURLRequest requestWithURL:url]];
}
This is shorter and avoids unnecessary variables. Since the "http://" prefix is fairly common, it doesn't seem like reusing that will provide that much benefit. Is there something else I'm missing?
Edit: To clarify a typo in my comment, you can get the URL as a string from the UIWebView as follows:
[[[webview request] URL] absoluteString]
This uses the following methods chained together:
-[UIWebView request]
-[NSURLRequest URL]
-[NSURL absoluteString]