PDA

View Full Version : webkit button action help




CocoaBean
Feb 23, 2009, 04:01 PM
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



kainjow
Feb 23, 2009, 04:27 PM
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.

CocoaBean
Feb 24, 2009, 07:41 AM
Ok, thanks I'll see if I can do it.

CocoaBean
Feb 26, 2009, 03:43 PM
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.

kainjow
Feb 26, 2009, 08:36 PM
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 (http://www.amazon.com/Cocoa-Programming-Mac-OS-3rd/dp/0321503619/ref=pd_lpo_k2_dp_k2a_3_txt/189-2166287-1671655?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=lpo-top-stripe-2&pf_rd_r=16GZYVGWQDFMF1MKNQ9W&pf_rd_t=201&pf_rd_p=304485601&pf_rd_i=0321213149) book.

CocoaBean
Feb 27, 2009, 09:49 AM
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:

kainjow
Feb 27, 2009, 10:36 AM
ok, do you have any code written yet?

CocoaBean
Feb 27, 2009, 11:09 AM
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

#import <Cocoa/Cocoa.h>

@interface AppController : NSObject {

IBOutlet WebView *webView;
IBOutlet NSTextField *urlText;
}
-(IBAction)navigate:(id)sender;

@end




AppController.m

#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.

kainjow
Feb 27, 2009, 01:51 PM
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.

CocoaBean
Feb 27, 2009, 04:24 PM
I changed the navigate method to the following:



-(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:


-(IBAction)navigate:(id)sender
{

[[WebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[urlString stringValue]]]];
}



Any thoughts on the above?

Thanks

kainjow
Feb 27, 2009, 09:44 PM
webView != WebView

CocoaBean
Feb 28, 2009, 07:23 AM
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:


#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?

kainjow
Feb 28, 2009, 09:08 AM
It doesn't know what a WebView is, so in the header file, right after your #imports, put @class WebView;

CocoaBean
Feb 28, 2009, 09:14 AM
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.

kainjow
Feb 28, 2009, 09:41 AM
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.

CocoaBean
Feb 28, 2009, 11:01 AM
At Last!! It now works!

Thank you very much for your time helping me with this :)

kainjow
Feb 28, 2009, 12:49 PM
Very nice. Now delete the project and do it all over again from memory ;)

CocoaBean
Mar 1, 2009, 08:54 AM
:eek: Haha, I'll try :D

ajr9166
Jun 28, 2009, 01:00 AM
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"

ajr9166
Jul 8, 2009, 06:36 AM
Any advice?

SRossi
Jul 8, 2009, 07:02 AM
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