• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

[Obj-C] Create a browser using the iPhone SDK

Joined
Sep 2, 2006
Messages
1,965
Reaction score
33
So i've been messing around with the iphone sdk, and i thought i'd share how to make a browser using only a couple of lines of code, and the interface builder.

If you have no background in Objective - C, is a good read

First of all, here's the source code for the app :

Open up XCode, select the iphone View Based app, name it anything you want, i named mine browser, i would recommend doing the same so following would be easier.

Navigate to your BrowserViewController.h

Interface Builder Outlets

We'll be using three IBOutlets, one for the webview, one for the adress, and one for the activity indicator

Add the following under @interface BrowserViewController : UIViewController { and before the }

Code:
        IBOutlet UIWebView *webView;
	IBOutlet UITextField *webAdress;
	IBOutlet UIActivityIndicatorView *active;

Then, after the } add

Code:
-(IBAction)classText;

Setting up the browser

Open up your BrowserViewController.m

Under @implementation BrowserViewController insert
Code:
-(IBAction)classText {

next we're going to want to define if we're loading a webpage or a file with NSURL.

Put this right below the { after class text

Code:
 [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[webAdress text]]]]; }

It should look like
Code:
@implementation BrowserViewController

-(IBAction)classText; {

	[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[webAdress text]]]];
}

Activity indicator and homepage

Stay on BrowserViewController.m, Remove the /* from -(void)viewDidLoad so it's considered code and not a comment, then after it add the loadrequest for the NSURL, and the activity indicator which will check if a page is loading or not loading
Code:
	[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.cnn.com/"]]];
	[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkLoad) userInfo:nil repeats:YES];
	[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkNotLoad) userInfo:nil repeats:YES];


We'll now be adding some voids, which will control the UIActivityIndicatorView

These will either stop animating, or start animating the ActivityIndicatorView based on activity.

Code:
}
- (void)checkLoad {
	if (webView.loading) {
		[active startAnimating];
	}
}
- (void)checkNotLoad {
	if (!(webView.loading)) {
		[active stopAnimating];
	}
}

Your BrowserViewController.m (from the -(void)viewDidLoad part till the next /* ) Should look like
Code:
- (void)viewDidLoad {
    [super viewDidLoad];
	
	[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.cnn.com/"]]];
	[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkLoad) userInfo:nil repeats:YES];
	[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkNotLoad) userInfo:nil repeats:YES];

	 
}
- (void)checkLoad {
	if (webView.loading) {
		[active startAnimating];
	}
}
- (void)checkNotLoad {
	if (!(webView.loading)) {
		[active stopAnimating];
	}
}


The Interface Builder

This is the easiest part, all we're going to do is link the multiple buttons to the webview. i won't be showing how to bring in buttons and the webview, since that is extremely easy, ill show what links to what.

This is for the webView

Omer - [Obj-C] Create a browser using the iPhone SDK - RaGEZONE Forums


This is for the File's Owner
Omer - [Obj-C] Create a browser using the iPhone SDK - RaGEZONE Forums



Output
Omer - [Obj-C] Create a browser using the iPhone SDK - RaGEZONE Forums


If there's anything else you guys want a guide for, post a comment/PM me. ill see what i can do.
 
Last edited:
Legendary Battlemage
Loyal Member
Joined
May 4, 2008
Messages
644
Reaction score
19
Very nice, have you made any other apps?
 
Back
Top