Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

yaboy10holla

macrumors newbie
Original poster
Feb 19, 2011
17
0
Hi guys, sorry I am very very new to programming but I am trying to create a mini web browser type app just for some practice. I want a user to be able to type a URL in a UITextField, then click a button and have the site pop up in the webview. Here is my action:

Code:
-(IBAction) displaySite:(id)sender {
	NSURL *inputURL;
	NSString *inputURLString;
	inputURLString=textBar.text;
	inputURL=[[NSURL alloc]initWithString:inputURLString];
	[websiteView loadRequest:[NSURLRequest requestWithURL:inputURL]];
textbar is what I named the textfield object.

thanks for the help!
 
Last edited by a moderator:
The code seems to work fine, see attachment.

What exactly is it doing that is not working on your end ? Since you just provided your IBAction code, there's not much to go on here to give you pointers.

EDIT : Are all your connections in IB made ? I attached a screenshot of what I had to connect up. Also, you did declare the IBOutlets in your class implementation of the view controller ?

Maybe you should post your ViewController's .m and .h files in full.
 

Attachments

  • Screen shot 2011-02-20 at 10.31.10 AM.png
    Screen shot 2011-02-20 at 10.31.10 AM.png
    244.1 KB · Views: 68
  • Screen shot 2011-02-20 at 10.36.30 AM.png
    Screen shot 2011-02-20 at 10.36.30 AM.png
    47.9 KB · Views: 67
Thanks a lot for helping me out!

I figured out the problem: when i was testing it out I was only typing "espn.com" not "http://www.espn.com"

not sure why you can't type in the short version, but itll do for now!

thanks again
 
ok now what is wrong with this? I am trying to get a segmented control to access two different websites. here is my action


Code:
-(IBAction)getURL:(id)sender {

	NSString *siteID;
	NSString *ESPN;
	NSString *Soccer;
	
	siteID=[urlPicker titleForSegmentAtIndex:urlPicker.selectedSegmentIndex];

	
	while (siteID=ESPN) {
		[websiteView loadRequest:[NSURLRequest requestWithURL:@"http://www.espn.com"]];
	}
		
	while (siteID=Soccer) {
		[websiteView loadRequest:[NSURLRequest requestWithURL:@"http://www.soccernet.com"]];
	}
	
	[siteID release];
	
}

this causes the simulator to turn off when I click the picker, "terminating due to uncaught exception."

any help is appreciated!
 
Last edited by a moderator:
1) = is assignment. == checks for equality
2) You cannot check for string equality this way: this would check pointer equality instead. You want to you isEqualToString:

Code:
if ([string isEqualToString:@"string"])
 
Guess I'll just add the part about the while. Are you sure you want to loop here ? This looks like it would get stuck in an infinite loop once you do allocate your NSString objects and assign them a value.

Leave the looping to the Application main loop, your IBAction is an event that will get called each time the appropriate action is triggered as set in Interface Builder.

I figured out the problem: when i was testing it out I was only typing "espn.com" not "http://www.espn.com"

not sure why you can't type in the short version, but itll do for now!

Well, NSURL isn't meant to act like browser bars. You're just feeding it a string and it's constructing a URL object from that string. It doesn't know about any defaults your app would want or even what type of URL you're trying to achieve. HTTP is one of many protocols that URLs can use. It could also use mailto:, ftp:, file:, etc.. etc..

It's up to your app to create logic to build sane defaults into the string to feed to NSURL. You'll have to add some string parsing/modification code to add any missing HTTP:// or ending .com/.net/.org domains you'd want since you're making a Web Browser essentially. Bonus points awarded for checking a site through the resolver so that if a .com doesn't exist, your app changes the URL to other TLDs.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.