简体   繁体   中英

UIWebView not loading URL when URL is passed from UITableView

So i'm building a webView into my application to show the contents of a URL that I am passing from a selection in a UITableView. I know the UIWebView is loading content properly because if you hard code say http://www.google.ca into the NSURL then it loads fine, however when I'm passing the URL that I parsed from an RSS feed back from the UITableView it won't load the URL properly. I tried the debugger and the URL is coming out as nil right before I try and parse it, however I can use NSLog to print the value of it out to the console.

here's the code in my UIViewController that has my UIWebView

 #import <UIKit/UIKit.h>


@interface ReadFeedWebViewController : UIViewController 
{
    NSString *urlToGet;
    IBOutlet UIWebView *webView;
}

@property(nonatomic, retain) IBOutlet UIWebView *webView;
@property(nonatomic, retain) NSString *urlToGet;

@end

Here's the code for my implementation's viewDidLoad method...

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
{
    [super viewDidLoad];

    NSLog(@"Url inside Web View Controller - %@", urlToGet);
    NSURL *url = [NSURL URLWithString:urlToGet];

    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    [self.webView loadRequest:requestObj];
}

Once again, I can print the URL to NSLog fine and if I hard code the URL into the NSURL object then it will load fine in the UIWebView. Here is where I'm setting the value in my UITableViewController...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{   
    ReadFeedWebViewController *extendedView = [[ReadFeedWebViewController alloc] init];

    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
    extendedView.urlToGet = [[stories objectAtIndex: storyIndex] objectForKey:@"link"];
    //NSLog([[stories objectAtIndex: storyIndex] objectForKey:@"summary"]);
    NSLog([[stories objectAtIndex: storyIndex] objectForKey:@"link"]);

    [self.navigationController pushViewController:extendedView animated:YES];

    [extendedView release];
}

However, since I can print the value using NSLog in the extendedView view controller I know it's being passed properly. Cheers

The urlToGet is null into your ReadFeedWebViewController in the viewDidLoad method because when this method is call this variable is not yet affected. The viewDidLoad is call when the initialisation of the ViewController is finished. I should you to call it in the viewWillAppear method like that:

- (void)viewWillAppear:(BOOL)animated {
    NSURL *url = [NSURL URLWithString:urlToGet];

    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    [self.webView loadRequest:requestObj];
}

Also, there is a memory leak in your code. You should release extendedView after pushing it on the navigation controller.

ReadFeedWebViewController *extendedView = [ReadFeedWebViewController new];
if (extendedView != nil) {
    ... setup extendedView here ...
    [self.navigationController pushViewController:extendedView animated:YES];
    [extendedView release];
}
Try this
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                        timeoutInterval:20.0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM