Load URL with WebView

Discussion in 'Mac Programming' started by Blakeasd, Jan 8, 2011.

  1. Blakeasd macrumors 6502a

    Joined:
    Dec 29, 2009
    #1
    Hello,
    This is a very newby question, but can someone look at this code and tell me why it isn't working?

    Code:
    
    
    [myWebView loadRequest:@"google.com"];
    
    

    Thanks
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    The documentation states that the parameter to loadRequest must be a NSURLRequest object. You are passing a NSString. You must create a NSURLRequest object representing the URL you want to load. I would suggest using requestWithURL: which takes a NSURL (again not a NSString). You can create a NSURL from a NSString using URLWithString:.

    Note that the compiler/Objective-C/Cocoa will not magically turn objects into the correct type for you: this is not AppleScript. You must pass the correct type of object.
     
  3. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #3
    Re:

    Here is my new code but it still is not working:

    Code:
    
    [ myWebView loadRequest:[URLWithString:@"http://www.google.com"]];	
    
    
    
    It says URLWithString is not declared. I have Webkit imported, but it's just not working.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    It's very clear you didn't understand what I typed. Or even Objective-C in general (as if you did you'd not have done that). URLWithString is a class-method of NSURL. If used correctly it'll return a NSURL. Which you then use to create the NSURLRequest object.

    I suggest you take a few steps back, stop trying to write code and learn the language and how to use the documentation links I provided.
     
  5. taptapdev macrumors newbie

    Joined:
    Jan 6, 2011
    Location:
    Bay Area
    #5
    [myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:mad:"http://www.google.com"]]];
     
  6. Blakeasd, Jan 8, 2011
    Last edited: Jan 8, 2011

    Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #6
    Figured it out. Here is the code I used for anyone who wants to learn from it:
    Code:
    [[myWebView mainFrame]loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
    
    
     
  7. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #7
    Or more simply

    Code:
    [myWebView setMainFrameURL:@"http://www.google.com"];
    
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8

Share This Page