- 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,
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 }
Then, after the } add
Setting up the browser
Open up your BrowserViewController.m
Under @implementation BrowserViewController insert
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
It should look like
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
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.
Your BrowserViewController.m (from the -(void)viewDidLoad part till the next /* ) Should look like
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
This is for the File's Owner
Output
If there's anything else you guys want a guide for, post a comment/PM me. ill see what i can do.
If you have no background in Objective - C,
You must be registered to see links
is a good readFirst of all, here's the source code for the app :
You must be registered to see links
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
This is for the File's Owner
Output
If there's anything else you guys want a guide for, post a comment/PM me. ill see what i can do.
Last edited: