What is wrong with this simple code?

Discussion in 'iOS Programming' started by yaboy10holla, Feb 20, 2011.

  1. yaboy10holla, Feb 20, 2011
    Last edited by a moderator: Feb 20, 2011

    yaboy10holla macrumors newbie

    Joined:
    Feb 19, 2011
    #1
    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!
     
  2. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #2
    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.
     

    Attached Files:

  3. yaboy10holla thread starter macrumors newbie

    Joined:
    Feb 19, 2011
    #3
    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
     
  4. yaboy10holla, Feb 20, 2011
    Last edited by a moderator: Feb 20, 2011

    yaboy10holla thread starter macrumors newbie

    Joined:
    Feb 19, 2011
    #4
    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!
     
  5. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    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"])
    
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Also, your ESPN and Soccer go unassigned in your method.

    And you're releasing siteID when you don't own it.
     
  7. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #7
    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.

    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.
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Wow, I totally missed that issue! I guess that's why sometimes it really helps to have multiple eyes on code.
     

Share This Page