TwitterCounter for @bigclick_dean

dBlog.com.au

My Development Blog

Archive for the ‘ iPhone Development ’ Category

In this installment we will be creating the “framework” for our application, first up we will be creating our UITabBarController and then we will add some ViewControllers to represent our different views.

1. Create a “Window-Based” application template

I had originally intended to create the application based on a “Navigation-Based” application template but now I have decided to create everything from scratch and not use Interface Builder. So if you have already completed part 2 then I suggest you go back there and follow the instructions to create the new “Window-Based” application template.

2. Setting up the UITabBarController and its delegate

Open up RSSReaderAppDelegate.h and edit the code to look like below


#import <UIKit/UIKit.h>

@interface RSSReaderAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UITabBarController *tabBarController;

@end

What we have done here is added the UITabBarControllerDelegate and created a UITabBarController instance called tabBarController.

Now you will need to open up RSSReaderAppDelegate.m and synthesize the tabBarController instance and alter the applicationDidFinishLaunching function to look like this


- (void)applicationDidFinishLaunching:(UIApplication *)application {

tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];

// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];

// Override point for customization after application launch
[window makeKeyAndVisible];
}

This will allocate and init the UITabBarController and then add it to the window (we haven’t added any buttons yet, but we will do this shortly).

If you run your application now you should see the UITabBar at the bottom of the window like so:
UITabBar with no buttons

3. Creating our UINavigationController

Right Click on the Classes folder in the left hand column and click Add > New File… , the following window will appear
XCode: Add new File

Now choose UIViewController subclass and click next, now name the class MainViewController.m and click Finish.

You should now have a MainViewController.h and a MainViewController.m file in your Classes folder. Open up MainViewController.h and edit its contents to below:


#import <UIKit/UIKit.h>

@interface MainViewController : UINavigationController {
UINavigationController *navigationController;
}

@property (nonatomic, retain) UINavigationController *navigationController;

@end

Notice we have changed the subclass from UIViewController to UINavigationController and we have added a UINavigationController instance.

Now open up MainViewController.m and edit its contents to below:


#import "MainViewController.h"

@implementation MainViewController

@synthesize navigationController;

- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"Main View Did Load: %@", self.tabBarItem.title);
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}

- (void)dealloc {
[super dealloc];
}

@end

All we have done here is synthesized the navigationController instance and put a NSLog call to display when the controller is created and what its TabBar item title was when it was created.

If you run the application now you wont see anything different, this is because we haven't added anything to the UITabBarController yet. To add the buttons you will need to open up the RSSReaderAppDelegate.m file and edit the applicationDidFinishLaunching function to look like below:


- (void)applicationDidFinishLaunching:(UIApplication *)application {

tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];

// Create instances of the MainViewController for the 4 TabBar buttons
MainViewController *viewController1 = [[[MainViewController alloc] initWithNibName:nil bundle:nil] autorelease];
viewController1.tabBarItem.title = @"Browse All";
MainViewController *viewController2 = [[[MainViewController alloc] initWithNibName:nil bundle:nil] autorelease];
viewController2.tabBarItem.title = @"Most Recent";
MainViewController *viewController3 = [[[MainViewController alloc] initWithNibName:nil bundle:nil] autorelease];
viewController3.tabBarItem.title = @"Favourites";
MainViewController *viewController4 = [[[MainViewController alloc] initWithNibName:nil bundle:nil] autorelease];
viewController4.tabBarItem.title = @"Settings";
tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3, viewController4, nil];

// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];

// Override point for customization after application launch
[window makeKeyAndVisible];
}

What we are doing here is creating 4 instances of the MainViewController and setting the tabBarItem title (this displays below TabBar icon). Once we have created all four of the View Controllers we add them to the tabBarController by setting its viewControllers property to an array containing the pointers for our 4 View Controllers. Remember that you need to have a nil value at the end of the array to indicate the last element.

If you run your application now you should see 4 TabBar items with the titles we have assigned them above. You will also notice that we have a Navigation bar at the top of our window, as we don't have a UITableView setup yet the Navigation bar will just be blank.

iPhone SDK: RSS Reader - TabBar & NavigationController working

4. Setting up our UITableViews

Now for starters we are going to need 4 UITableViewControllers for our Browse, Recent, Favourites and Settings Tabs. We will start with the stock standard ones and customize them as we go along.

Right Click the Classes folder in the left hand column and click Add > New File...
Select UITableViewController subclass and click Next
Call the first one BrowseViewController.m and then click Finish
Repeat these steps and create a RecentViewController, FavouritesViewController and SettingsViewController

Now that we have all our stock standard UITableViewControllers created we have to connect them up, we will be doing this in the MainViewController.m file, so open it up and edit its viewDidLoad function to below:


- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"Main View Did Load: %@", self.tabBarItem.title);
if(self.tabBarItem.title == @"Browse All") {
BrowseViewController *browseViewController = [[BrowseViewController alloc] init];
[self pushViewController:browseViewController animated:YES];
[browseViewController release];
} else if (self.tabBarItem.title == @"Most Recent") {
RecentViewController *recentViewController = [[RecentViewController alloc] init];
[self pushViewController:recentViewController animated:YES];
[recentViewController release];
} else if (self.tabBarItem.title == @"Favourites") {
FavouritesViewController *favouritesViewController = [[FavouritesViewController alloc] init];
[self pushViewController:favouritesViewController animated:YES];
[favouritesViewController release];
} else if (self.tabBarItem.title == @"Settings") {
SettingsViewController *settingsViewController = [[SettingsViewController alloc] init];
[self pushViewController:settingsViewController animated:YES];
[settingsViewController release];
}

}

Now this may not be the most traditional method and isn't very good for massive applications that use heaps of view controllers, etc but it worked fine for my needs and meant I only needed one UINavigationController (MainViewController). What it does is it checks the title of the TabBar item that it is linked to, it then decides which UITableViewController to push into view.

Also don't forget to #import the four new header files


#import "BrowseViewController.h"
#import "RecentViewController.h"
#import "FavouritesViewController.h"
#import "SettingsViewController.h"

If you run your application now you will wee that we have a UITableView visible on each of the TabBar items, but we currently cant tell which one is which as they all look the same.

5. Identifying and Customising our UITableViews

Starting with our BrowseViewController we want to open up the BrowseViewController.m file and alter it's viewDidLoad function to below:


- (void)viewDidLoad {
[super viewDidLoad];

self.title = @"Browse All";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
}

We have set the title to "Browse All" and we have added a button to the right hand side of the navigation bar, this button is an add button (as indicated by UIBarButtonSystemItemAdd) and currently its action is set to nil, in the future we will connect this up to an UIActionSheet.

Now go through the other three controllers (RecentViewController, FavouritesViewController and SettingsController) and just set the title, not the rightBarButtonItem!

If you run your application now you should be able to navigate through it and see the title change and on the Browse All tab you should see an add button at the top right, although it wont do anything when you press it.

Browse All TabMost Recent TabFavourites TabSettings Tab

6. Download the Project Files

Click Here to download my version of the Project files, you can use these to work your way through any problems or even to work backwards to see how I do things.

7. Whats Next?

In the next installment we will be creating our SQLite database and adding some feeds and groups, we will also be setting up the BrowseViewController to handle multiple levels of groups with only one view (Drilldown) and we will also be looking at storing images in a SQLite database with a BLOB field.

PLEASE CLICK HERE TO RE-TWEET THIS TUTORIAL

Please do anything you can to spread these tutorials around as the more people that read the faster I will write tutorials!

Popularity: 15% [?]

Well I have been thinking about what features the RSS Reader tutorial should cover and I have also been getting some requests from readers (if anyone else has a request please feel free to send them through or tweet them to me).

I have come up with the following list of features and how we will be implementing them.

1. Store and retrieve RSS Feeds from a SQLite Database

We will be storing our feed details in a SQLite database and then caching the feed items in a local SQLite database once they are downloaded, this will also allow us to mark when articles have been read and also to have a favourite tab (more on this later). We will also be storing an image (favicon) in the SQLite database as a BLOB.

2. Feed Grouping and Table Drilldown

As I follow a large number of bloggers I like to be able to group my feeds into their topic or other groupings, this will also allow me to show you how to do UITableView drilldown with a single view controller. This will also go into a bit more info on SQLite and how to dynamically set a UITableViews source information.

picture-33

3. UIActionView to add RSS feed / Feed Group

This was one thing that I thought would be a nice touch, I see many people asking how to get this to work on other blogs so I thought I would include it for your enjoyment ;-)

picture-34

4. TouchXML to process RSS feeds

TouchXML is a 3rdParty library that makes manipulating and traversing XML documents a breeze, we will be using it to grab our RSS feeds and transcode them for storing in our SQLite database.

5. Integrated UIWebView

I have had many, many requests from readers for a tutorial on integrating a UIWebView with a blog reader to provide the all-in-one solution for your blog reading needs!

6. Custom Cell Renderer

Another thing that I see pop up often is requests for a tutorial on creating custom cell renderers for UITableViews, I will go through the process for creating custom cells in code.

7. UITabBarController

Originally I was just going to use a navigation controller and make the application pretty basic, but once I got all the feature requests through I realised that we would need more navigation options, I decided to use a UITabBar and Navigation Controller. This will mean a little adjustment to the “Part 2″ tutorial, but I will go back in the next installment and cover that.

8. Core Location

I have been trying to think of a way to integrate Core Location into this app, in reality there isn’t a huge need for Core Location in a RSS Reader application but I really wanted get it in there somehow…I have decided to make a server-side PHP script that will allow you to find blogs that people close to you are, don’t really know how useful it is but it will go over the basics of CoreLocation and integrating with a remote web-service.

9. What Next?

If anyone has any more feature requests them feel free to fling them my way, once this app is complete it will be submitted to the app store and hopefully approved. The full source code will be freely available for you to modify and hack up as much as you want.

In the next part we will dive into the code and start getting something running in our simulator.

Popularity: 3% [?]

This installment will walk you through creating a new Xcode project and getting everything ready to start the coding. The iPhone SDK comes ready to dive in but we need to confiure our SQLite and TouchXML libraries and get our workspace up to scratch!

1. Creating a new Xcode project

Start Xcode by clicking the icon in your dock or by running “Xcode” in your /Developer/Applications folder.

Click File > New Project and the following window will appear

Xcode New Project Window

For our RSS Reader application we are going to be using the “Window-Based Application” template, this can be found under the “iPhone OS” group in the left hand column. Simply double click the “Window-Based Application” in the Right hand column and give your project a name (I will be calling the project RSSReader).

Once you have created the project the main Xcode window will appear

Default Xcode window

2. Laying out your screen

I personally don’t like the default layout for Xcode and it can get quite hectic when you are trying to debug, code and watch the build logs at the same time. So what can you do? Thankfully Apple has provided an “All-in-one” layout and here is how you can activate it.

First you need to close your newly created project, don’t worry as it will all be saved already.
Now go to Xcode > Preferences and the following window will appear

Xcode preferences window

The General tab should be selected, if not then click on it.
You will see the “Layout” drop-down with “Default” selected, click this and choose “All-in-one” and click OK to close the window.

Now go to File > Open Recent Project and select RSSReader.xcodeproj (or whatever you called your project) from the list.

You may not notice a huge difference but there should be some icons at the top left corner of the window that will allow you to switch between the different pages (Project and Debug) and you will also now have “Project Find”, “SCM Results” and “Build” tabs in the Project page. Below is how mine looks and you will notice that I have expanded the “RSSReader” group in the left hand column.

Xcode All-in-one Window

Now that we have the All-in-one layout activated we can adjust a few things to make it a “perfect” working environment.

If you expand the “Classes” folder in the left hand column you will see a series of files (header and main files for our AppDelegate), click one of there and the right hand column should look like this.

Code Editor View

You need to drag the horizontal seperator between the two panels on the right hand side all the way to the top to give you the most code-view area possible, once you have done that it should look like this.

Resized Code Editor View

3. Add the SQLite framework to our project

By default the SQLite framework isn’t included in the project template and you will need to add it manually.

To do this you will need to right click on the Frameworks folder in the left hand pane, then click on Add > Existing Frameworks… Now navigate to /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.0.sdk/usr/lib/ and double click the “libsqlite3.0.dylib” file. This folder may change depending on the version of your iPhone SDK, the only bit that should change is the “iPhoneOS2.0.sdk” bit, this wont be a problem as it is still the same version of the framework across all the SDK releases.

The following pop-up will appear, simply click Add and the library will be added to your project.

Add Framwork Dialog

4. Add the TouchXML libraries to our project

As we will be using TouchXML for our xml parsing, we will need to import the required files we downloaded in Part 1 and also edit some of the project settings.

Go to Project > Edit Project Settings and a new window will appear with 4 tabs at the top (General, Build, Configurations and Comments). We are only interested in the Build tab at the moment, so go ahead and click the Build Tab and the following window will appear.

Project Settings Window

Make sure that you change the Configuration dropdown to “All Configurations” otherwise you may have trouble when testing on a device later on.

The settings that we are interested in are “Header Search Paths” and “Other Linker Flags”, you can easily find these by typing the beginning of the name into the Quick Find at the top right of the window.

You need to add “/usr/include/libxml2″ to the “Header Search Paths” and you need to add “-lxml2″ to the “Other Linker Flags”.

Once you have done this you can close the settings window and you will be back at your project window.

Now that the libxml2 libraries have been included you will need to import the TouchXML files to your project, to keep everything neat and tidy we will create a new “Group” to organise the files.

Right click on the “Classes” folder in the left hand column and click Add > New Group and call it TouchXML

Now its time to import the TouchXML files, simply right click on the new “TouchXML” folder in the left hand pane and click Add > Existing Files…

You will be presented with a finder window and you will need to navigate to the location that you downloaded the TouchXML files out to (in my case it was /Developer/ExtraLibraries/) and then keep going to the following path “touchcode-read-only/TouchXML/Common/Source/” and select all the files and click Add.

You should now see the CXML* files in your left hand pane and they are now available to your project.

5. And then?

We are now all setup and ready to start building the application, the next post will go over exactly what we will be building and the general structure of the application and how it will work. Remember to have a read through my other tutorial so you will have a bit of a headstart on the way to use the different technologies.

Remember to grab me on Twitter at @bigclick_dean for regular updates!

Popularity: 6% [?]

This tutorial series will walk you through the creation of a complete, ready to publish iPhone application. I have chosen to continue the Advanced RSS Reader application but I will be taking a step back and extending on some of the methods explained in those tutorials.

When this series is completed you will have learnt (hopefully!) how to setup a new iPhone application, use UITableViews & UINavigationControllers, interact with SQLite databases, read RSS/XML feeds by using TouchXML, test your project on actual iPhone hardware, prepare your application for submission to the App Store and much more.

1. Tools you are going to need

iPhone SDK -Apple has created a superb set of tools that are going to become your best friends while you work your way through this tutorial series and iPhone development in general.
TouchXML Libraries – As many of you may know the NSXMLDocument implementation was removed from the iPhone SDK due to its hefty processing requirements, so thats why we use TouchXML to provide the basic XML parsing options that we need.
SQLite Manager Plugin for FireFox – As we are going to be working with SQLite databases we need to be able to create our database structure and to browse through it easily. You can use the command line or any application that supports SQLite databases, although I have grown very fond of the SQLite Manager Plugin for Firefox (available at http://addons.mozilla.org/en-US/firefox/addon/5817)


2. Downloading & Installing the iPhone SDK

You can grab the iPhone SDK from the iPhone Dev Centre (http://developer.apple.com/iphone/), you will need to create an account to get access to the download, but don’t worry…it’s FREE!

Once the download has finished (its a heavy 1Gb+ download so now’s the best time to take a nap) you can simply run the package and go through the standard install process. By default it will install the developer tools into /Developer/ and there will no icons in your applications folder or your dock.

Under the /Developer/Applications folder you will see a bunch of applications and folder, the two we are going to be concentrating on are “Xcode” and “Interface Builder”.

picture-15picture-16

I suggest that you drag these to your dock for easy access as we are going to be using them alot! I will come back to these applications in the next tutorial as we need to install some more tools first.

3. Downloading and Setting up the TouchXML Libraries

You will need to check the latest code out from the TouchXML SVN Repository, if you don’t know how to use SVN I have included the required commands below.

1. Launch a new Terminal window
2. Create a new folder called “ExtraLibraries” where you would like to keep your iPhone development libraries (I save mine in /Developer/ExtraLibraries/ to keep evenything clean and reusable)

mkdir /Developer/ExtraLibraries

3. Change into the new folder

cd /Developer/ExtraLibraries

4. Run the SVN checkout code

svn checkout http://touchcode.googlecode.com/svn/trunk/ touchcode-read-only

You will see the filenames scrolling up the screen and depending on your connection it should only take around 60 seconds to grab all the required files. If you have a look in the newly created folder you will see the TouchXML folder along with some other libraries (TouchSQL, TouchJSON, TouchHTTPD, etc). We wont be using these other libraries but feel free to have a play around with them and see what you can do.

5. Now you have the TouchXML libraries on your local machine ready to start creating your first iPhone app.

4. Installing the SQLite Manager Plugin for FireFox

I like to use the visual editor in FireFox but by no means is it the only option, I will provide a database download for those that have trouble getting their own one built.

To install the FireFox plugin simply open up FireFox and visit http://addons.mozilla.org/en-US/firefox/addon/5817 and click “Add to FireFox”, you will need to go through the usual process of installing an add-on and after that it will be available at Tools > SQLite Manager, if you launch the manager the following window should appear:

picture-17

5. Are you ready?

Well that’s all the essential tools installed and ready to go! Go and have a play and a look at the applications and get ready for the next installment for the series.

I hear you asking “What is the next installment going to outline and when will it be out?”…The next installment is going to go through setting up a new project, organising our development windows and getting the TouchXML and SQLite libraries set up for our project. I am hoping to have the next installment out in a few hours and then it will be time to get into the “meat” of the project!

Now it’s time for a shameless plug…If you need any Web Design or Development in Australia or for that matter the world, then please check out Big Click Studios. Big Click is a Sydney Web Design Agency that I run and we do AMAZING work (I may be biased ;-) , click here to check out our portfolio.

Popularity: 8% [?]

Hi Everyone,

Sorry about the delays, I have been flat out with changing jobs (starting my own business actually) and getting all that setup and running smoothly.

I am hoping to get back into the iPhone tutorial writing headspace very soon and I have some great ideas to put down, the first one will be a lengthy (probably 10 part) tutorial series on developing a complete RSS application that uses SQLite, XML, Multiple Views, Save States, Caching, etc

I also have a simple puzzle game in the works that I would love to turn into another tutorial series so you can get a jump start on your iPhone Game Development.

Being in the Web/Application Development “scene” I have decided to give the whole Twitter craze a go and to see what all the fuss is about, so you can follow me by clicking the icon on the right.

If youo have any ideas or anything you would like me to write about then please leave a comment and I will give you a buzz.

Thanks,
Dean

P.S. Check out my new business at http://www.bigclick.com.au

Popularity: 3% [?]

Hey Everyone,

Sorry about the lack of updates over the past few weeks, I have been flat out getting some projects done so my blog has had to take a back seat.

But never fear the tutorials will still continue to come and you may even see an app or two of mine released on the App Store.

As most of you know Apple had announced iPhone Tech Talks all across the world but only with limited spots available (especially in Australia), but alas I have managed to get into the Sydney one and I will be there with bells on (metaphorical bells of course).

Hopefully I will be able to write some articles about the Talk and I will be able to pass as much information as possible to those people that cannot make the day.

If anyone else is heading to the Sydney Tech Talk just drop me a line and we can catch up for a beer and talk iPhone!

Have a great day and stay tuned for some updates soon.

Popularity: 4% [?]

Well I have been asked this a few times and I can see how it would be a very helpful addition to the UIWebView’s feature set. What I am talking about is adding the ability to intercept the event generated when a user selects a link in a webpage being displayed with a UIWebView, this will then allow you to perform any action that you want.

When would you use this I hear you say? Well one person asked me if I knew how to append the users current location onto all HTTP requests? and another just wanted to know how to perform custom actions from an embedded UIWebView.

Once again I will be extending the “Build your very own Web Browser!”, if you haven’t already completed it then I suggest that you head over there now and spend 5 minutes reading it and then downloading the project files at the end.

Start by opening up the WebBrowserTutorialAppDelegate.h file and editing the @interface line to read:

@interface WebBrowserTutorialAppDelegate : NSObject <UIWebViewDelegate> {

What we have done is to make the main AppDelegate a delegate for the UIWebView as well.

Now we need to set our webView to have the main AppDelegate as its delegate, you can do this by opening up WebBrowserTutorialAppDelegate.m and putting the following line just inside the applicationDidFinishLaunching function:

webView.delegate = self;

That is all pretty self explanatory, it just sets the delegate of our webView to self, which in this case is our main application delegate.

Now we are pretty much done, we just need to add the function to catch the link clicks. To do this we need to add a new function, copy the content below to the WebBrowserTutorialAppDelegate.m file:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
	NSURL *url = request.URL;
	NSString *urlString = url.absoluteString;
	NSLog(urlString);
	return YES;
}

This function will catch all requests and allow you to either manipulate them and pass them on or to perform your own custom action and stop the event from bubbling.

The first line gets the URL of the request, this is the contents inside the href attribute in the anchor tag.
The next line converts the URL to a string so we can log it out. You can access many parts of the NSURL, here are some of them and brief description of what they do.

* absoluteString – An absolute string for the URL. Creating by resolving the receiver’s string against its base.
* absoluteURL – An absolute URL that refers to the same resource as the receiver. If the receiver is already absolute, returns self.
* baseURL – The base URL of the receiver. If the receiver is an absolute URL, returns nil.
* host – The host of the URL.
* parameterString – The parameter string of the URL.
* password – The password of the URL (i.e. http://user:pass@www.test.com would return pass)
* path – Returns the path of a URL.
* port – The port number of the URL.
* query – The query string of the URL.
* relativePath – The relative path of the URL without resolving against the base URL. If the receiver is an absolute URL, this method returns the same value as path.
* relativeString – string representation of the relative portion of the URL. If the receiver is an absolute URL this method returns the same value as absoluteString.
* scheme – The resource specifier of the URL (i.e. http, https, file, ftp, etc).
* user – The user portion of the URL.

Then the third line simply logs the URL to the console, so you will new to open up the console while you run this in the simulator to see the results.

Finally the forth line returns YES, this will allow the UIWebView to follow the link, if you would just like to catch a link and stop the UIWebView from following it then simply return NO.

I hope this will help someone to make a browser with some nice user interaction features.

Thanks,
-Dean

Popularity: 14% [?]

Well I needed a way to get a formatted date string in NSString format so I dug around and came up with the following function.

-(NSString *) dateInFormat:(NSString*) stringFormat {
	char buffer[80];
	const char *format = [stringFormat UTF8String];
	time_t rawtime;
	struct tm * timeinfo;
	time(&rawtime);
	timeinfo = localtime(&rawtime);
	strftime(buffer, 80, format, timeinfo);
	return [NSString  stringWithCString:buffer encoding:NSUTF8StringEncoding];
}

What it does it it takes a NSString as input with set specifications (checkout http://developer.apple.com/documentation/Darwin/Reference/ManPages/man3/strftime.3.html for the full list of specifications) and it returns the compiled date string as a NSString ready for you to use.

It is pretty much just a wrapper for the C strftime function but I thought it would be helpful to share as you often need dates or times in games/applications.

Here are some usage examples and their expected outputs:

[self dateInFormat:@"%s"] // Should return a UNIX timestamp, i.e. "1222738875"

[self dateInFormat:@"Today is %A"] // Should return "Today is Tuesday"

[self dateInFormat:@"%+"] // Should return "Tue Sep 30 11:50:01 EST 2008"

[self dateInFormat:@"%Z (%z)"] // Should return timezone + UTC offset in brackets, i.e. "EST (+1000)"

[self dateInFormat:@"%Y-%m-%d-%H:%M:%S"] // Should return a date/time stamp as used my many programs (MySQL, PHP, etc) i.e. 2008-09-30-11:54:03
NOTE: You can also use [self dateInFormat:@"%Y-%m-%d-%X"] to get the same result as above.

I hope that this function will be able to same someone 5-10 minutes as I know I will be using it for a few of my projects.

Popularity: 2% [?]

Just letting everyone know that the source files for the first three tutorials are now available at the bottom of each tutorial. There has also been some minor updates to some of the tutorials to make them easier to understand.

Keep your eyes out for more tips and tutorials coming soon!

Popularity: 1% [?]

I thought i would make this a separate post to the Build your very own Web Browser! tutorial as it can be used for many different purposes and it took me a bit to figure it out.

For my example I am going to be trying to use a file in my local bundle in a UIWebView, my file is going to be called “sample.jpg” and it will be in the root folder of my project, this will work for any folder though.

I will show you the code and then I will explain it.

NSString *imagePath = [[NSBundle mainBundle] resourcePath];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

NSString *HTMLData = @"
<h1>Hello this is a test</h1>
<img src="sample.jpg" alt="" width="100" height="100" />";
[webView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: [NSString stringWithFormat:@"file:/%@//",imagePath]]];

Line 1
This will get the path to the main bundle root folder.

Line 2
We need the slashes to be double slashed to work correctly in the UIWebView, so we are searching for all instances of “/” and replacing it with “//”

Line 3
Same deal as above but we are searching for a space and replacing it with the HTML equivalent of %20

Line 4
This is putting some sample data together for out UIWebView, you will see that we have set the images source to just our “sample.jpg” file as it is sitting in the root folder, if it was in a folder called “images” we would need to set the source to “images//sample.jpg”. Remember the double slashing!

Line 5
This is pretty much the same as my example in the tutorial although we are setting the baseURL to the root of our application bundle, this allows us to reference everything relatively instead of absolute paths.

Well thats about it, if you have done my Build your very own Web Browser! tutorial you will be able to replace the contents of applicationDidFinishLaunching with the above code (don’t forget to leave the makeKeyAndVisible line in there) and you will be loading content from your local device in no time!

Popularity: 10% [?]