As I’ve said in previous posts, I’m still new to the whole iPhone programming world, and thus, when I’m given a project out of my capability I cheat.
I’m 90% deep in the web world and 10% deep in the iPhone programming world, so why can’t I use my web experience to “fund” my iPhone experience?
UIWebView has become my new friend
I can create a website and get it to run through a UIWebView on an iPhone app. All in all I have about 10 lines of Objective-C to be writing and thousands of lines of HTML, Javascript, JQuery and CSS which I am altogether more comfortable with.
Set up the UIWebView as a variable through interface builder’s IBOutlet, with @property and @synthesize then jump over to the implementation file (.m)
|
1 2 3 4 5 6 7 |
- (void)viewDidLoad { [super viewDidLoad]; NSString *file = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; NSURL *filePath = [NSURL fileURLWithPath:file]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:filePath]; [webView loadRequest:requestObj]; } |
The above code just loads your homepage into the UIWebView (names webView). I follows the below steps:
- Create a string called file, consisting of a path to index.html
- Convert this path into a URL by saving it into a URL variable with the fileURLWithPath method.
- Create a URLRequest to hold your URL
- Get the webView to load the URLRequest
- Bam! Done!
Get your files in!
Now however, you need to correctly import your files. This is something I had a little trouble with initially because I didn’t really understand the way X-Code imported files.
I transferred all my files into a folder within my X-Code project directory then imported that entire folder into my project. There’s a checkbox that says ‘Create Folder References for Any Added Folders’, which means you can keep your directory structure without going through your code and removing any paths.
I also made sure that there were no links to any external sources in the HTML because I want the app to become self contained.
Run the app and it should work! You have made your very own iPhone app with only 4 or 5 lines of Objective-C. Congratulations you cheater!
Comments
Powered by Facebook Comments