Sega dude
Sega dude

Reputation: 1113

Getting the current URL and title from WebView

I am trying to get the current URL and title from the WebView. I've used this for the URL

- (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
{
    // Only report feedback for the main frame.
    if (frame == [sender mainFrame]){
        NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
        [addressBar setStringValue:url];
    }
}

and this for the title:

- (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
{
    // Report feedback only for the main frame.
    if (frame == [sender mainFrame]){
        [[sender window] setTitle:title];
    }
}

This code comes straight from Apple's WebKit Objective-C Programming Guide. I only slightly modified the URL method to the addressBar instead of textField. But it doesn't work. The addressBar field is never populated with the URL of the page and the window title doesn't update ether. Everything is connected correctly in interface builder. Why won't it work?

Upvotes: 0

Views: 2463

Answers (3)

Biggs
Biggs

Reputation: 267

If the page has already loaded then it is not a provisionaldatasource, replace this with

[[[[frame dataSource] request] URL] absoluteString];

Upvotes: 1

Martyn Chamberlin
Martyn Chamberlin

Reputation: 1287

Here's you you get this working.

  1. Set up your frameLoadDelegate for the WebView object. In your visual interface, control-drag from your WebView element to your NSWindow that is containing the WebView. When you release the mouse, a small black drop-down menu will appear. Select frameLoadDelegate. Once you've done this, messages such as webView:DidReceiveTitle:forFrame will be sent to the instance of your NSWindow.
  2. Create a Subclass out of NSWindow and assign your NSWindow object to this subclass. Since this new child object will inherit everything from NSWindow, it will receive the webView:DidReceiveTitle:forFrame message.
  3. Paste in your code above into this new child class. This effectively overrides the method definitions from the parent class, and gives you autonomous control over what happens.

Hope that helps.

Upvotes: 0

PolygonHJ
PolygonHJ

Reputation: 82

Unfortunately, I myself don't know. But what I have found is a great website with tutorials one of which is to make an RSS feed and in that it takes the title from the RSS. You'll see it if your scroll down. Hope you can modify it to work!!

http://www.raywenderlich.com/2636/how-to-make-a-simple-rss-reader-iphone-app-tutorial

Upvotes: 0

Related Questions