3

Hello everyone I have an objective-c dilema :P I am quite new to objective-c, and I have tried searching for an answer, but to no avail.

So, here is my situation. I will put the code here now, or else it won't make as much sense. I am putting down what I need, this code does not work right now, and I will get to why later

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMMM"];
NSString *monthString = [formatter stringFromDate:date];
NSLog(@"MONTH STRING %@", monthString);
NSString *baseURLStr = @"http://www.mysite.ca/example";
NSURL *url = [NSURL URLWithString:[baseURLStr stringByAppendingFormat:@"announcements%20%@%20%d%20carson.ashx", monthString]];

[webView loadRequest:[NSURLRequest requestWithURL:url]];
[NSCalendar release];

So, this is where the problem lies... Encoded (HTML): stringByAppendingFormat:@"announcements%@%20%d%20example.ashx", monthString, day]];

Decoded it looks like this

stringByAppendingFormat:@"announcements %@ %d example.ashx"]];

That should be easier to understand.. When I run with the %20's, it says "The requested document was not found" When I run with spaces (" ") it is just white, and nothing loads.

I know the URL is correct, and if I take the variables out, it is the exact same problem, but when I move the %20's back to the baseURLStr, it works and loads just fine, so it is something to do with the HTML Codes and the "stringbyAppendingFormat" string.

Any help is appreciated!

Thanks for your time

-Kyle

一二三
  • 21,059
  • 11
  • 65
  • 74

4 Answers4

9

whenever an NSURL returns nil (0x0) after init it is almost always related to in improper url string. And its very picky about getting a properly formatted string.

my recommendation is to simply build your string, without any escapes or url encoding, then simply call

myUrlString = [myUrlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

here is the header declaration for it

- (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc NS_AVAILABLE(10_3, 2_0);

this way, I always know I get it formatted the way that the NSURL class wants it.

here is an example

NSString *sUrl = @"http://www.myside.ca/example/announcements carson.ashx"; //notice the embedded space
sUrl = [sUrl stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [NSURL URLWithString:sUrl];
Jason Cragun
  • 3,233
  • 1
  • 26
  • 27
  • But what about the monthString :/ –  May 01 '11 at 01:14
  • show me an exact example of what the finished url should look like... without any encoding... – Jason Cragun May 01 '11 at 01:17
  • 1
    Just have to be careful the current url string doesn't already include any percent escapes, otherwise that'll make the url invalid – ToddH Jan 25 '13 at 19:27
  • @JasonCragun, Thank you!! your answer helped me fixing my problem. However, `stringByAddingPercentEscapesUsingEncoding` is deprecated since iOS 9.0. I used `urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];` instead and it works just fine! – Sam Sep 27 '19 at 13:33
0

If I understand you correctly, you're trying to generate a string with literal "%20"s embedded in it. The "%" character is special in format strings. If you want to insert a literal percent character, you need to escape it by putting two consecutive "%"s. Like

[NSURL URLWithString:[baseURLStr stringByAppendingFormat:@"foo%%20bar"]];

That would append "foo%20bar" to the end of the string.

deong
  • 3,820
  • 21
  • 18
  • I am not trying to put in a % the %20's for the spaces don't seem to be working like an actual space... –  May 01 '11 at 00:49
0

You probably just need to encode the percent signs. Try: stringByAppendingFormat:@"announcements%%20%@%%20%d%%20carson.ashx"]

Scott Forbes
  • 7,397
  • 1
  • 26
  • 39
  • Right. The problem may be that `stringByAppendingFormat:` is eating the percent sign in your %20 – if so, you can fix this by using %%20 instead. – Scott Forbes May 01 '11 at 00:56
0

If you're not using printf-style formats, don't use stringByAppendingFormat:. Use stringByAppendingString: instead.

Second, is the resulting URL really supposed to be http://www.myside.ca/exampleannouncements%20%@%20%d%20carson.ashx? Or is there supposed to be a slash in the middle: http://www.myside.ca/example/announcements%20%@%20%d%20carson.ashx?

Also, http://www.myside.ca/exampleannouncements%20%@%20%d%20carson.ashx is an invalid URL. The percent signs that are not part of an escape (e.g. not part of %20) must themselves be encoded, as %25. Technically, the @ should also be escaped (as %40), but IIRC NSURL will let that slide.

Anomie
  • 92,546
  • 13
  • 126
  • 145
  • I get a semantic issue when I use String. And there is supposed to be a slash –  May 01 '11 at 01:10
  • i forgot to add a part to the code :/ Its the monthString part –  May 01 '11 at 01:13