-1

I'm working on a master detail app and I'm having a problem reloading my webview after a selection is made in the tableView. I'm inclined to think the problem is something very simple with my NSURL assignment. Below is the relavent code.

this function is in my masterview;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UGStory *detailStory = [_ugFeed.stories objectAtIndex:indexPath.row];

    [_detailViewController displayMaster:detailStory.url];

    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

this one is in the detailview

- (void) displayMaster:(NSURL *)urlToLoad
{
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:urlToLoad];
    [_webView loadRequest:requestObj];
}

and this is where the NSURL is being set

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:   (NSString *)namespaceURI qualifiedName:(NSString *)qName 
{
    if (_read == YES)
    {
        if ([elementName isEqualToString:@"title"]) {
           UGStory* temp = [_stories objectAtIndex:_i];
           temp.title = _currentTag;
        }
        if ([elementName isEqualToString:@"link"]) {
            UGStory* temp = [_stories objectAtIndex:_i];
            temp.url = [NSURL URLWithString:_currentTag];
            _read = NO;
            _i++;
        }
    }
    _currentTag = nil;
    return;
}

I suspect that my assignment of temp.url is not working as expected. I've printed the _currentTag using NSLog and it hold the correct value when it occurs.

url is declared as a strong nonatomic property. When I execute this all of these functions are called when I would expect them to, but after the execution of the displayMaster function the contents of the WebView does not change.

EDIT: I've deleted some of my OP because I have clearly identified which line of code is causing the problem. It is the URL assignment in didEndElement, URLWithString is returning nil.

if ([elementName isEqualToString:@"link"]) {
            NSLog(@"urlString = %@", _currentTag);
            temp.url = [NSURL URLWithString:_currentTag];
            NSLog(@"url = %@, %@", temp.url, [temp.url absoluteString]);
            _read = NO;
            _i++;
        }

The output in my log looks like the following for every object in my array.

2012-01-07 20:14:22.971 MusicNews[1162:f803] urlString = http://www.ultimate-guitar.com/news/general_music_news/30_seconds_to_mars_no_plans_to_split.html 2012-01-07 20:14:22.982 MusicNews[1162:f803] url = (null), (null) 2012-01-07 20:14:22.982 MusicNews[1162:f803] urlString = http://www.ultimate-guitar.com/news/upcoming_releases/kiss_two_days_from_finishing_new_album.html 2012-01-07 20:14:22.983 MusicNews[1162:f803] url = (null), (null)

Can anyone explain why URLWithString isn't working here?

evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
  • what do you get if you do `NSLog(@"url = %@, %@", urlToLoad, [urlToLoad absoluteString]);` in `displayMaster`? also, do the URL strings you're getting from the parser start with "http://"? – Mike K Jan 07 '12 at 23:35
  • I'm getting "url = (null), (null)" from call to NSLog in displayMaster. The URL string do include http://, all of them look good. – evanmcdonnal Jan 08 '12 at 02:41
  • hmm, ok, so the url is getting released. what happens if you do `NSLog(@"title = %@", detailStory.title);` in `- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath` ? just wondering if the title is also missing. – Mike K Jan 08 '12 at 02:53
  • That displays the correct title. It's just the url. – evanmcdonnal Jan 08 '12 at 03:00
  • hmm. and in `UGStory`, the URL is declared as `@property (strong, nonatomic) NSURL *url;` (key thing being the strong)? – Mike K Jan 08 '12 at 03:05
  • Yeah it's strong and nonatomic. – evanmcdonnal Jan 08 '12 at 03:13
  • @MikeK To further test this I added NSLog(@"url = %@, %@", urlToLoad, [urlToLoad absoluteString]); right after the line where I assign the UGStory's url in didEndElement and that is also printing "url = (null), (null)" in the log... – evanmcdonnal Jan 08 '12 at 04:09
  • ahh, ok, looks like the URLWithString is failing, which probably means there's something about the URL string that it doesn't like. there are a few restrictions on what it will accept: http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/occ/clm/NSURL/URLWithString: – Mike K Jan 08 '12 at 04:22
  • @MikeK Yeah I was checking that out but it's not of much help to me because I don't know anything about RFCs. Do you know if the NSXMLParser ignores white spaces? As far as I understand the strings containing my URLs should not have white spaces before the first char, but I'm not knowledgable on the NSXMLParser so I'm suspecting that as a cause of the problem. If you know anything about RFCs I posted a couple of the URLs in the OP, they are all very similar to those (no special chars, just words separated by underscores). If you want to answer the question I'll accept your answer. – evanmcdonnal Jan 08 '12 at 05:30
  • so those URLs work for me.. maybe there are leading or trailing whitespaces? try `NSString *urlString = [_currentTag stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];` – Mike K Jan 08 '12 at 07:01

0 Answers0