webkit button action help

Discussion in 'Mac Programming' started by CocoaBean, Feb 23, 2009.

  1. macrumors newbie

    Joined:
    Feb 23, 2009
    #1
    This is my first post so hello to everyone on here, hopefully this will be a valuable source for me.

    I'm doing what a lot of people do and am having a go at making a simple web browser in xcode with objective c with the webkit framework.

    I have created a browser which is fine - when I enter a url in the NSTextbox and press enter, the site loads in the webview.
    However, what I would like to do is to add a 'go' button which, when clicked, will make the webview load the url which is in the textbox.

    I have been trying to find a solution but am still confused.
    Can anyone suggest how to get around this hurdle and offer some advice?

    Thanks
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    1. Learn to assign a target and action to a button.
    2. Learn to connect a text field and read its value.
    3. Learn to load a URL in a WebView via code.

    Once you have all three you're set.
     
  3. thread starter macrumors newbie

    Joined:
    Feb 23, 2009
  4. thread starter macrumors newbie

    Joined:
    Feb 23, 2009
    #4
    I'm still stuck - I am fine with making targets/actions and have followed a few webkit tutorials on the internet (the ones with 0 lines of code, just to see if it will work and they did). However when I try to code it myself, I just can't get it to work.
    The tutorials seem to be quite old and there are some won't work, I'm using the latest version of x code.
    I have also read Apple's webkit documentation and understand it but still no luck at actually building a successful application.

    Any more advice will be appreciated.
     
  5. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    WebKit is just a framework and if you don't know Cocoa then it's going to be virtually impossible to learn. It's not like learning iMovie or iPhoto. If you really want to learn I'd suggest picking up this book.
     
  6. thread starter macrumors newbie

    Joined:
    Feb 23, 2009
    #6
    thanks for replying,

    I already have the book you have suggested and have read it but I still have no luck with manually programming the webkit framework. It'll be something simple probably but at the moment, I just can't seem to do it :confused:
     
  7. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
  8. thread starter macrumors newbie

    Joined:
    Feb 23, 2009
    #8
    yes, I've put it below, hopefully, if there are errors they can be corrected.

    I have created a class called AppController, both .h and .m files are included.

    AppController.h
    Code:
    #import <Cocoa/Cocoa.h>
    
    @interface AppController : NSObject {
    
    IBOutlet WebView *webView;
    IBOutlet NSTextField *urlText;
    }
    -(IBAction)navigate:(id)sender;
    
    @end
    
    

    AppController.m
    Code:
    #import "AppController.h"
    #import <WebKit/WebKit.h>
    
    @implementation AppController
    
    -(IBAction)navigate(id)sender
    {
    [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
    }
    
    @end
    
    
    The connections:

    AppController class to urlText (text field where user enters url)
    AppController class to webView
    Button to AppController class with action of navigate.

    The WebKit framework is added to the project too.
     
  9. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #9
    Ah. Ok here's a tip. Look at what the URLWithString: method takes as an argument (from the docs) and look at what you're passing it.
     
  10. thread starter macrumors newbie

    Joined:
    Feb 23, 2009
    #10
    I changed the navigate method to the following:

    Code:
    
    -(IBAction)navigate:(id)sender
    {
    	NSString *urlString = [urlText stringValue];
    	[[WebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
    }
    
    
    is this ok?
    also it now says that 'WebView may not respond to +mainFrame' and I don't know why.

    Also another code modification I thought of is:

    Code:
    -(IBAction)navigate:(id)sender
    {
    
    	[[WebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[urlString stringValue]]]];
    }
    
    
    Any thoughts on the above?

    Thanks
     
  11. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
  12. thread starter macrumors newbie

    Joined:
    Feb 23, 2009
    #12
    Ahh, I see - I have now changed the [WebView mainFrame] to [webView mainFrame] and the warning goes away. However I have 2 errors which are below:

    Code:
    #import "AppController.h"
    (*error: syntax error before WebView)
    
    #import <WebKit/WebKit.h>
    
    
    
    @implementation AppController
    
    -(IBAction)navigate:(id)sender
    {
    	NSString *urlString = [urlText stringValue];
    	[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
    (*error: webView undeclared (first use in this function)
    }
    
    @end
    
    
    For the syntax error I think it means in the AppController header file but it looks ok to me.

    webView has been declared in the header file too so I don't know why it says it's undeclared?
     
  13. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #13
    It doesn't know what a WebView is, so in the header file, right after your #imports, put
    Code:
    @class WebView;
     
  14. thread starter macrumors newbie

    Joined:
    Feb 23, 2009
    #14
    Ok, I have done that and it now compiles fine, although when a url is typed, the web View doesn't navigate to it and the button doesn't do it either.
     
  15. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #15
    Double check your connections in IB. Make sure your text field is connected, your web view is connected, and your button's action is connected.
     
  16. thread starter macrumors newbie

    Joined:
    Feb 23, 2009
    #16
    At Last!! It now works!

    Thank you very much for your time helping me with this :)
     
  17. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #17
    Very nice. Now delete the project and do it all over again from memory ;)
     
  18. thread starter macrumors newbie

    Joined:
    Feb 23, 2009
  19. macrumors newbie

    Joined:
    Jun 27, 2009
    #19
    I am getting very confused and ultimately frustrated with the way WebKit is running (or maybe it's just the ObjC conventions to begin with). I am an experienced programmer, but the whole object naming and interaction between GUI elements and program code is driving me nuts.

    Enter my WebView. I have almost the same exact situation described here. I have WebView listed as a class in the header file and have it linked to the framework. Now I have a few problems:

    My code WILL NOT COMPILE without my web request starting as [[WebView mainFrame... rather than the "correct" [[webView mainFrame...

    I am completely, utterly lost as to how the code "knows" what WebView I am referencing in my GUI. I never named my WebView in IB (nor do I know how to). What if I had several WebViews in my GUI? How would I reference them? Maybe my experience in Java and .Net is throwing me off from these strange conventions of actions and outlets. Any clarification would be greatly appreciated!

    Now, other solutions I have tried are as follows (as opposed to the loadRequest in this article) to instantiate an initial browser location:

    [webView setMainFrameURL:urlText];

    However, as with the loadRequest, I get an xcode error of "error: undeclared first use"
     
  20. macrumors newbie

    Joined:
    Jun 27, 2009
  21. macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #21
    Have you added the WebKit framework to your application, if not you will need to add it to access WebViews ect.

    Have you set any connections in Interface Builder?

    If you have can you post up your code.

    Stephen
     

Share This Page