3

I am learning to program a simple Web Browser Mac App in Objective C. I have a simple textfield as an address bar and a Webview. As such, I wish to update the address the website in the textfield whenever the page changes when I click a link.

After googling, I realise I could use a method

    didStartProvisionalLoadForFrame

in the WebFrameLoadDelegate Protocol. I then proceed to type

@interface AppDelegate : NSObject <NSApplicationDelegate,WebFrameLoadDelegate>

which resulted in an error. After further search I found out that is due to WebFrameLoadDelegate being an informal Protocol.

My question is then this: What is the syntax for using the method below specified by the informal Protocol?

- (void)webView:(WebView *)webView didFinishLoadForFrame:(WebFrame*)frame;
1
  • For an informal protocol you don't need to do anything other than implement the delegate methods (and set your delegate). Commented Oct 7, 2012 at 2:50

2 Answers 2

4

With an informal protocol, you implement the methods without declaring conformation to a protocol. WebFrameLoadDelegate is not a type, just the name of an NSObjectcategory.

They exist because, previously, there was no way to specify that a method in a protocol was optional (the @optional and @required keywords did not exist.) When the requisite keywords were added to Objective-C, Apple started moving to formal protocols with optional methods, which makes for better type safety and self-documenting code.

Sign up to request clarification or add additional context in comments.

Comments

3

If you're manually creating a webview, then just set your own class as the delegate as following [webview setDelegate:self]. But if you made the webview in IB instead, you can link the delegate there instead.

Once you set your class as the delegate of the webview, you can just implement that method without any extra code.

- (void)webView:(WebView *)webView didFinishLoadForFrame:(WebFrame*)frame
{
    //Do stuff here.
}

And the WebView will simply call your method whenever it finishes loading a frame.

If you want to know how this works, it all goes back to the construction of a delegate. WebView will check if delegate is nil or not. If it's not, then it will call respondsToSelector: to check if the delegate has implemented the delegate method. An informal delegate is just an old way of allowing optional protocol method, which shouldn't be used anymore as stated by @JonathanGrynspan

And if you didn't know, you don't have to explicitly declare that your class confirms to a protocol (even if it's not informal) if you connect it in IB.

By the way, if you're working on a webview, check out Apple's development guide on it. It gives you a step-by-step tutorial on creating the basics of a webview including when to change the webview's URL address.

2 Comments

Well it didnt work when i dragged it to file's owner. however it did work when i drag it to app delegate.
Oops. I thought you were using a document-based application/window controller. If it's in the mainmenu.nib, drag it to the app delegate instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.