Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-21-2011, 12:39 PM   #1 (permalink)
Registered Member
 
Join Date: May 2011
Location: Nevada
Posts: 11
maziar is on a distinguished road
Default 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:

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

And here is my WebViewController.h file:

Code:
#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:

Code:
#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.
maziar is offline   Reply With Quote
Old 05-21-2011, 03:36 PM   #2 (permalink)
Registered Member
 
Join Date: May 2011
Location: Nevada
Posts: 11
maziar is on a distinguished road
Default

I found out where I went wrong. I had to add the <UIWebViewDelegate> to my .h file, and I forgot to connect my Web View to the delegate in my File's Owner.

It's working now but the initial URL that the Web View opens with does not show, just blank, but any links clicked after that show in the text field.

I also had to change to
Code:
ViewDidFinishLoad
instead of
Code:
ViewDidStartLoad
in order to also get the initial URL that the Web View launches with.
maziar is offline   Reply With Quote
Reply

Bookmarks

Tags
ios, iphone, nsurlrequest, objective-c, uiwebview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 354
7 members and 347 guests
doffing81, dre, iOS.Lover, jenniead38, Kirkout, PlutoPrime, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:08 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0