PDA

View Full Version : UIWebView won't load




nashyo
Mar 11, 2012, 09:36 AM
What's wrong with this code


NSLog(@"The link is %@", [selectedContent objectForKey:@"link"]);

NSString *string = [[NSString alloc] initWithString:[selectedContent objectForKey:@"link"]];
NSLog(@"The string is %@", string);
NSURL *myURL = [[NSURL alloc] initWithString:string];
NSLog(@"The NSURL is %@", myURL);
NSURLRequest *myRequest = [[NSURLRequest alloc] initWithURL:myURL];

Console:
The link is http://www.apple.com
The string is http://www.apple.com
The NSURL is (null)



chown33
Mar 11, 2012, 11:15 AM
Make sure the string doesn't have any unexplained characters. Use one of the percentEncoding methods of NSString to produce a percent-encoded form, then NSLog that string.

nashyo
Mar 11, 2012, 12:04 PM
Thanks for the feedback chown33. However, I have literally copy and pasted the string, as it appears in the dictionary and the web view loads no problem at all. I'm baffled as to why the above object is null when I call the dictionary directly.

chown33
Mar 11, 2012, 01:03 PM
Dig deeper.

Set aside your expectations, and only gather individual facts which are supported by evidence. Attempt to prove or disprove this assertion: "The object whose key is 'link' is an NSString, and it contains only valid URL-safe characters."
It's clear that you believe this to be true, but belief isn't evidence.

Use the debugger. Make sure that what you think is an NSString is really an NSString. This line is not guaranteed to produce an actual NSString object in the variable 'string':
NSString *string = [[NSString alloc] initWithString:[selectedContent objectForKey:@"link"]];

Nor does NSLog'ing require an NSString. Any NSObject, regardless of type, has a -description method that returns NSString. That's what %@ uses, and that method IS guaranteed to produce an NSString.

Do like I said and make sure there are no unexpected characters. After ensuring you have a real NSString, check its length, then break it apart into characters, if necessary, and confirm that each is a valid URL character.

nashyo
Mar 11, 2012, 03:02 PM
Sound advice. Thank you.

nashyo
Mar 12, 2012, 08:39 AM
Dig deeper.

Set aside your expectations, and only gather individual facts which are supported by evidence. Attempt to prove or disprove this assertion: "The object whose key is 'link' is an NSString, and it contains only valid URL-safe characters."
It's clear that you believe this to be true, but belief isn't evidence.

Use the debugger. Make sure that what you think is an NSString is really an NSString. This line is not guaranteed to produce an actual NSString object in the variable 'string':
NSString *string = [[NSString alloc] initWithString:[selectedContent objectForKey:@"link"]];

Nor does NSLog'ing require an NSString. Any NSObject, regardless of type, has a -description method that returns NSString. That's what %@ uses, and that method IS guaranteed to produce an NSString.

Do like I said and make sure there are no unexpected characters. After ensuring you have a real NSString, check its length, then break it apart into characters, if necessary, and confirm that each is a valid URL character.

I haven't had time to put this into practice but I intend to check the string for unsafe URL characters in a few hours. However, I do have some evidence that I can't explian...

This returns null
NSURL *myURL = [NSURL urlwithstring:[selectedContent objectforkey:@"link"];
If I extract the NSLog string in the console by physically highlighting and copying the NSLog message for

NSLog("%@", [selectedContent objectforkey:@"link"]);

Then paste that as the string for the following

NSURL *myURL = [NSURL urlwithstring:@"pasted NSLog string"];

the NSURL is not null. Why is there a difference?

chown33
Mar 12, 2012, 12:03 PM
I haven't had time to put this into practice but I intend to check the string for unsafe URL characters in a few hours.

That's backwards. The first thing you need to do is confirm that the object you assume is a string is really an NSString. If it's not, then you can't possibly "check the string" for unsafe URL characters, because it's not a string.

Here's the central point: you still believe the object you're working with is an NSString. Based on what evidence? You haven't described where the dictionary containing the "link" key came from, how it was built, nor how the object associated with "link" was obtained. Maybe the source of that dictionary isn't checking the kinds of objects it's putting in the dictionary. Or maybe you've just blindly assumed that some object whose %@ looks like a string is always going to be an NSString.

You keep treating the object like it's a string because you still believe it's a string. Forget that assumption. How do you identify what the actual class of an object really is? Maybe use the debugger. Or try logging the object's -class instead of the object itself:
NSLog( @"mystery class: %@", [[selectedContent objectforkey:@"link"] class] );
This is what I mean about gathering evidence. The output is evidence that the object in question is or isn't an NSString. If it's not an NSString, then you can proceed accordingly.