user754314
user754314

Reputation: 83

UIWebView - Trouble displaying current URL in text field

I'm having problems getting the current URL through a URL request and passing it on to the text field above my web view. I tried logging it and it returns NULL, and the text field remains blank.

This is the delegate I added to my implementation file:

- (void)webViewDidStartLoad:(UIWebView *)webView {
    NSURL* url = [webView.request URL];
    _webAddress.text = [url absoluteString];
}

And here is my WebViewController.h file:

#import <UIKit/UIKit.h>


@interface WebViewController : UIViewController {

    IBOutlet UIWebView *_webView;
    IBOutlet UITextField *_webAddress;
    IBOutlet UIActivityIndicatorView *activityIndicator;
    NSTimer *timer;
    NSString *_webURL;
}

@property (nonatomic, retain) IBOutlet UIWebView *webView;
@property (nonatomic, retain) IBOutlet UITextField *webAddress;
@property (nonatomic, retain) NSString *webURL;

- (IBAction) doneButton:(id)sender;


@end

And here is my WebViewController.m file:

#import "WebViewController.h"


@implementation WebViewController

@synthesize webView = _webView;
@synthesize webAddress = _webAddress;
@synthesize webURL = _webURL;


// Dismiss WebViewController
- (IBAction) doneButton:(id)sender {

    [[self parentViewController] dismissModalViewControllerAnimated:YES];        
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)webViewDidStartLoad:(UIWebView *)webView {
    NSURL* url = [webView.request URL];
    _webAddress.text = [url absoluteString];
}

- (void)viewDidLoad
{
    [_webView addSubview:activityIndicator];
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_webURL]]];
    timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(loading) userInfo:nil repeats:YES];

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)loading {
    if (!_webView.loading) 
        [activityIndicator stopAnimating];

        else
            [activityIndicator startAnimating];
}

- (void)viewDidUnload
{
    _webURL = nil;
    _webView = nil;
    _webAddress = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}


- (void)dealloc
{
    [_webURL release];
    [_webView release];
    [_webAddress release];
    [super dealloc];
}


@end

Does anybody know what I am doing wrong here? Thank you.

Upvotes: 2

Views: 2504

Answers (2)

Adarsh V C
Adarsh V C

Reputation: 2314


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{

NSURL* url = [request  URL];
 _webAddress.text = [url absoluteString];


}

Upvotes: 0

Joe
Joe

Reputation: 3761

Use the - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType delegate instead. This will give you the actual request.

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebViewDelegate_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40006951-CH3-SW6

Upvotes: 1

Related Questions