UIWebView doesn't seem to like RSS links

Discussion in 'Mac Programming' started by Ride9650, May 15, 2010.

  1. Ride9650 macrumors 6502

    Joined:
    Jun 29, 2007
    #1
    I've a rather frustrating issue, I can't get my UIWebview to load any links fo encounted formated like:

    http://feedproxy.google.com/~r/TheBoyGeniusReport/~3/95JeYV6zcMk

    Regular urls work fine.

    I'm assuming theres an issue with the redirection but I haven't found anything useful that explains whats happening or how to fix it.

    I was hoping someone might be able to help me understand whats going on. I'm not a total newb at programming but I am still new to Objective-C so, if you could keep it as simple as possible that'd be great.

    Thanks!

    EDIT:
    OH! If it helps, I just discovered the reason that nothing happens. A 400 Error is sent! But my question still remains the same.
     
  2. Ride9650 thread starter macrumors 6502

    Joined:
    Jun 29, 2007
    #2
    OK! Upon further investigation, I must have simply copy and pasted an extra bit of white space or something because that link and other similarly formed links work now.

    However it raised a new question.

    I got things to work by directly putting the URL in a NSString.

    What I'm trying to do though, is grab the link from an RSS feed first, before sending it to my WebView.

    I can get the url just fine, but when I pass it to my NSUrl, the statement doesn't seem to be able to see it and thinks the result is null.

    This is my code for that section
    Code:
    NSString *link = [[stories objectAtIndex:storyIndex] objectForKey:@"link"];
    
    	url = [[NSURL alloc] initWithString:link];
    	NSLog(@"%@",url);
    	request = [[NSURLRequest alloc] initWithURL:url];
    
    Can anyone help? I'm sure its something ridiculously simple, but I just can't seem to figure out what it is.
     
  3. Ride9650 thread starter macrumors 6502

    Joined:
    Jun 29, 2007
    #3
    How to properly encode URLS for use in a program?

    First off, I know this is more Iphone related, and I've already posted in the iphone programming section, but due to lack of response and how this is still a somewhat generalized issue , I thought I'd try here too.

    So I'm trying to build a RSS app, and at first, I baffled at why the urls I was passing to a UIWebview component weren't loading the page.

    As it turns out, I didn't encode the string properly somehow, despite, passing the url to a string first(the url is coming from the info selected in a UITableView cell), then passing that string to a NSUrl object.

    I came across the "stringByAddingPercentEscapesUsingEncoding" method which has at least, gotten a reaction from the Webview, but I keep getting a Bad Request error from feeds that aren't styled in a regular url format like
    "http://www.thisismysite.com/feed/xml"

    Strange thing is, pasting the url directly as a string into the NSUrl object works fine no matter the address.

    I'm at a loss as to whats going on. Can anyone help?
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Post your code.

    Provide an example of a URL that fails.
     
  5. Ride9650 thread starter macrumors 6502

    Joined:
    Jun 29, 2007
    #5
    of course! my bad

    Code:
    
    	
    	NSString *link = [[stories objectAtIndex:storyIndex] objectForKey:@"link"];
    	
    
    	NSString *escapedURL = [link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];	
    	
    	
    /*Something else i tried	
    	NSString * escapedURL = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)link,NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8);
    			*/																																	   																				
    																				  
    																				
    	
    	url = [[NSURL alloc] initWithString:escapedURL];
    	request = [[NSURLRequest alloc] initWithURL:url];	
    
    As to urls that fail, it seems to generally be any url thats formatted like "http://feeds.thissite.com" though I've come across a few in that format that still work for some reason.

    One that fails is
    http://feeds.gawker.com/~r/gizmodo/...eagate-confirms-3tb-hd-coming-later-this-year
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    Show the type of the variables 'stories' and 'storyIndex', and show how both variables are given a value.

    You're only showing part of the picture. You've left out the all-important "What these things are" and "How these things got to this state" parts.


    When I enter the following command into Terminal:
    Code:
    curl -i http://feeds.gawker.com/~r/gizmodo/full/~3/uLu33eRY-n8/seagate-confirms-3tb-hd-coming-later-this-year
    I see the following output:
    Code:
    HTTP/1.1 301 Moved Permanently
    Location: http://gizmodo.com/5540996/seagate-confirms-3tb-hd-coming-later-this-year?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+gizmodo%2Ffull+%28Gizmodo%29
    Content-Type: text/html; charset=UTF-8
    Date: Mon, 17 May 2010 21:26:15 GMT
    Expires: Mon, 17 May 2010 21:26:15 GMT
    Cache-Control: private, max-age=0
    X-Content-Type-Options: nosniff
    X-XSS-Protection: 0
    Server: GSE
    Transfer-Encoding: chunked
    
    <HTML>
    <HEAD>
    <TITLE>Moved Permanently</TITLE>
    </HEAD>
    <BODY BGCOLOR="#FFFFFF" TEXT="#000000">
    <H1>Moved Permanently</H1>
    The document has moved <A HREF="http://gizmodo.com/5540996/seagate-confirms-3tb-hd-coming-later-this-year?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+gizmodo%2Ffull+%28Gizmodo%29">here</A>.
    </BODY>
    </HTML>
    
    The server response is obviously a redirect. So when you get the contents of this failing URL, are you handling redirects or not?


    When I use the URL from your original post, I get similar results (a redirect):
    Code:
    curl -i http://feedproxy.google.com/~r/TheBoyGeniusReport/~3/95JeYV6zcMk
    
    HTTP/1.1 301 Moved Permanently
    Location: http://www.boygeniusreport.com/2010/05/14/samsungs-galaxy-s-all-but-confirmed-for-life-with-t-mobile/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+TheBoyGeniusReport+%28Boy+Genius+Report%29
    Content-Type: text/html; charset=UTF-8
    Date: Mon, 17 May 2010 21:33:54 GMT
    Expires: Mon, 17 May 2010 21:33:54 GMT
    Cache-Control: private, max-age=0
    X-Content-Type-Options: nosniff
    X-XSS-Protection: 1; mode=block
    Server: GSE
    Transfer-Encoding: chunked
    
    <HTML>
    <HEAD>
    <TITLE>Moved Permanently</TITLE>
    </HEAD>
    <BODY BGCOLOR="#FFFFFF" TEXT="#000000">
    <H1>Moved Permanently</H1>
    The document has moved <A HREF="http://www.boygeniusreport.com/2010/05/14/samsungs-galaxy-s-all-but-confirmed-for-life-with-t-mobile/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+TheBoyGeniusReport+%28Boy+Genius+Report%29">here</A>.
    </BODY>
    
     
  7. Ride9650 thread starter macrumors 6502

    Joined:
    Jun 29, 2007
    #7
    Sorry bout that! didn't seem relevant but

    "stories" is a mutable array to hold the results of the xml parsing.

    "storyIndex" is an int marking the position on a UITableView thats displaying all the information of a particular feed.
    Code:
    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
    	
    I know theres a redirect involved in the example link, which I have not handled(but its something I'm certainly gonna look up now). Things were working fine when putting in the link directly like


    Code:
    url = [[NSURL alloc] initWithString:@"http://feeds.gawker.com/~r/gizmodo/f...ater-this-year"];
    
    so I didn't think that anything really needed to be done.
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    Based on this, I'd say the problem lies in what comes before the assignment of a value to the 'url' variable.

    From your description, that's XML parsing. Or maybe it's handling redirects.

    Anyway, the way to approach this is to stop and look at the string being used in NSURL's initWithString: method. You need to make sure it has everything properly percent-escaped, and so on. Basically, you need to account for the correctness of every single character in that string.

    If I were you, I'd run your iPhone program under the simulator on a Mac, and use a tool like Interarchy to watch the actual HTTP traffic. If some request is wrong, you'll see it in the network traffic. If a response is unexpected for whatever is handling it, you'll see it in the network traffic. Looking at the traffic and at the actual string used to init the NSURL are the two places that seem most likely to show something useful.


    And how about posting some of the URLs that work, but which are in the format that you think fails. Do any of them perhaps not reply with a redirect?
     
  9. Ride9650 thread starter macrumors 6502

    Joined:
    Jun 29, 2007
    #9
    Finally figured out a solution!

    As it turns out, it was a problem with the encoding format.
    It kept adding a "%0A" as part of the stringByAddingPercentEscapesUsingEncoding method, removing that allows the link to load and redirect properly.

    Thanks for the advice chown!
     
  10. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #10
    I'm sure it wasn't adding "%0A". It was doubtless converting the existing newline/linefeed character (Unicode U+000A) that was already on the end of the string you were starting with.

    No valid URL string will ever start or end with unescaped whitespace. This suggests a faulty parser, or a misinterpretation of the parser's output.

    You should be using the NSString method stringByTrimmingCharactersInSet: and the whitespaceAndNewlineCharacterSet. See NSString reference docs.
     

Share This Page