NSUrl fileURLWithPath returns strange chinese signs - ios

I have an NSString path to my Documents folder.
NSString* stringURL = #"/var/mobile/Applications/5667FADC-F848-40CF-A309-
7BFE598AE6AB/Library/Application Support/MyAppDirectory";
When I cast it to NSUrl with
NSURL* url = [NSURL fileURLWithPath:stringUrl];
and NSLog(#"Created URL: %#",url);, i get some strange result:
///var/mobile/Applications/5667FADC-F848-40CF-A309-7BFE598AE6AB/Library/Application㤈㤋ތȀ乽啓汲唠䱒›楦敬⼺⼯慶⽲潭楢敬䄯灰楬慣楴湯⽳㘵㜶䅆䍄䘭㐸ⴸ〴䙃䄭〳ⴹ䈷䕆㤵䄸㙅䉁䰯扩慲祲䄯灰楬慣楴湯㈥匰灵潰瑲䴯䅹灰楄敲瑣牯⽹upport/MyAppDirectory/
Why is this so ?
What am I doing wrong ?

I didn't see any Chinese character when I log the value.
NSString* stringURL = #"/var/mobile/Applications/5667FADC-F848-40CF-A309-7BFE598AE6AB/Library/Application Support/MyAppDirectory";
NSURL* url = [NSURL fileURLWithPath:stringURL];
NSLog(#"%#",url);

Classical mistake. Don't use NSLog (url), use NSLog (#"%#", url). The first argument to NSLog is a format string, and % characters in format strings are interpreted, not printed. For example, %s in a format string means another C-String is expected in the argument list. Since url could contain all kinds of characters, this is likely to lead to rubbish results or even crashes.

Based on the answer you accepted from a previous question; it's because the use of stringByAddingPercentEscapesUsingEncoding will generate a printf-like formatting string containing %20S (the space between Application Support is converted to %20), which confuses NSLog():
NSURL *url = [NSURL fileURLWithString:[stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] isDirectory:YES];
NSLog(url);
use NSLog("#%", url) to avoid this error.

Related

URL Query item from NSURLComponents are nill

I am creating a NSURL URL will contain some escape character (Japanese)
NSString* currentlocationbarString = #"mbos.help.jp/search?q=専門&pg=1"
NSString *escapedString = [currentlocationbarString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
NSURL* url = [NSURL URLWithString:escapedString];
//url is mbos.help.jp%2Fsearch%3Fq=%E5%B0%82%E9%96%80&pg=1
When I create NSURLComponents and try to get query items it gives me nil.
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url
resolvingAgainstBaseURL:YES];
NSArray *queryItems = urlComponents.queryItems;
//here issue with queryItems
if anybody has solution to get query items please help. Thanks in advance
Issue is not with Unicode Characters, whenever you add encoding use proper character set for my case I was using following setURLHostAllowedCharacterSet it means your NSURLComponents only give encoding for your Host, to get correct queryItems use URLQueryAllowedCharacterSet like this way.
NSString* currentlocationbarString = #"mbos.help.jp/search?q=専門&pg=1"
NSString *escapedString = [currentlocationbarString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL* url = [NSURL URLWithString:escapedString];
So now you can get queryItems.
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url
resolvingAgainstBaseURL:YES];
NSArray *queryItems = urlComponents.queryItems;
At least one of the characters 専門 that you use in your search string is invalid Unicode in the form of unpaired UTF-16 surrogate chars, and thus cannot be encoded by stringByAddingPercentEncodingWithAllowedCharacters:, which therefore returns nil.
You can find an example in this post.
Apparently, you had to check for Japanese characters, if encoding is possible.
I must say, I did not expect that either!

[__NSCFString absoluteString]: unrecognized selector sent to instance, attempt to convert url to string

Looking to simply convert the parse image url to a string so I can use SDWebImage for caching, etc.
After researching I found that I can convert from a url to string by calling absoluteString on the NSURL. I've also tried this:
NSURL *theUrl = [[obj objectForKey:#"image"] url];
NSString *finalUrl = [theUrl absoluteString];
[cell.carPhoto setImageWithURL:[NSURL URLWithString:finalUrl]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
but I am also crashing on this line with the same error
NSString *finalUrl = [theUrl absoluteString];
Any ideas? By the way obj is a PFObject
The error message is telling you that you have an instance of __NSCFString, and you're calling absoluteString on it. __NSCFString is a kind of NSString, so the situation is simply that you expect (for some reason) that you have an NSURL, but you actually have an NSString - so you don't need to call absoluteString.
It would be good to understand why you think you have an NSURL and whether you will ever have one in this piece of code. The inputs should really be consistent, but if not you can check the class and decide how to get the string version.
It looks like you're trying to convert a NSString to an NSURL with this line:
NSURL *theUrl = [[obj objectForKey:#"image"] url];
But that's not how you convert an NSString to an NSURL and so your NSString isn't being changed to an NSURL as you'd like. Instead, it seems as if it's remained an NSString, thus the error.
There's no need to convert the NSString to an NSURL if the only purpose of that NSURL is to be converted back to an NSString on the next line by accessing its absoluteString property; but if you do in fact need theURL to be an NSURL for reasons beyond the code you've posted (for example, if you need to access the NSURL variable later in your code), try this instead to properly convert your string into an url:
NSURL *theUrl = [NSURL URLWithString:[obj objectForKey:#"image"]];

Data argument not used by format string but it works fine

I used this code from the Stack Overflow question: URLWithString: returns nil:
//localisationName is a arbitrary string here
NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString* stringURL = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#,Montréal,Communauté-Urbaine-de-Montréal,Québec,Canadae&output=csv&oe=utf8&sensor=false", webName];
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];
When I copied it into my code, there wasn't any issue but when I modified it to use my url, I got this issue:
Data argument not used by format string.
But it works fine. In my project:
.h:
NSString *localisationName;
.m:
NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString* stringURL = [NSString stringWithFormat:#"http://en.wikipedia.org/wiki/Hősök_tere", webName];
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];
[_webView loadRequest:[NSURLRequest requestWithURL:url]];
How can I solve this? Anything missing from my code?
The # in the original string is used as a placeholder where the value of webName is inserted. In your code, you have no such placeholder, so you are telling it to put webName into your string, but you aren't saying where.
If you don't want to insert webName into the string, then half your code is redundant. All you need is:
NSString* stringURL = #"http://en.wikipedia.org/wiki/Hősök_tere";
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];
[_webView loadRequest:[NSURLRequest requestWithURL:url]];
The +stringWithFormat: method will return a string created by using a given format string as a template into which the remaining argument values are substituted. And in the first code block, %# will be replaced by value of webName.
In your modified version, the format parameter, which is #"http://en.wikipedia.org/wiki/Hősök_tere", does not contain any format specifiers, so
NSString* stringURL = [NSString stringWithFormat:#"http://en.wikipedia.org/wiki/Hősök_tere", webName];
just runs like this (with the warning Data argument not used by format string.):
NSString* stringURL = #"http://en.wikipedia.org/wiki/Hősök_tere";

URL for GET request

To get the JSON I was using this answer SBJsonWriter Nested NSDictionary
Now I have a sting {"key1":"bla1","key2":{"a":"a1","b":"b1","c":"c1","d":"d1"},"key3":"bla3"} that I've called theString
and I need to add it to a url http://mysyte.net:8888/JSON?
and to receive something like this http://lcwebtest.sytes.net:8888/JSON?{"key1":"bla1","key2":{"a":"a1","b":"b1","c":"c1","d":"d1"},"key3":"bla3"}
Here is what I do:
NSString *urlString = [NSString stringWithFormat:#"http://mysyte.net:8888/JSON?%#",theString];
NSLog gives http://mysyte.net:8888/JSON?{"key2":{"d":"d1","b":"b1","c":"c1","a":"a1"},"key1":"bla1","key3":"bla3"}
Then I make a url from it by
NSURL *url1 = [NSURL URLWithString:urlString];
BUT NSLog(#"%#",url1); gives me {null}
I assume NSURL does not want to read the "{" or "}" and thinks that the url was malformed.
How can I receive the url to make a GET request?
I assume NSURL does not want to read the "{" or "}" and thinks that the url was malformed.
That's right, you're not allowed to put some special characters in a URL. You want to escape the JSON string like this:
CFStringRef escaped = CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)theString, // (__bridge CFStringRef)theString if you use ARC
CFSTR(""),
CFSTR("?&=%,:+-"),
kCFStringEncodingUTF8
);
NSString *urlString = [NSString stringWithFormat:#"http://mysyte.net:8888/JSON?%#", (NSString *)escaped];
CFRelease(escaped);

Unrecognized selector error

I am getting this error: [NSURL stringByAppendingFormat:]: unrecognized selector sent to instance 0x5869210 when it gets to the append. strcust is a plain number and strURI is correct until the append.
NSString *strUIR = [NSURL URLWithString:#"https://cid.hooru.mobi:36610/?";
strURI = [strURI stringByAppendingFormat:#&Cust=IPRR%#", strCust];
Any thoughts would be appreciated. I am just trying to append Name/value pairs from variables. Can't get the append to work.
There are several problems with this piece of code:
You're declaring an NSString but you are assigning an NSURL
You're missing a right square bracket ']'
You're missing a double quote on your second line
You're trying to call an NSString method on an NSURL object
You're misspelling strURI on your first line (it is strUIR)
Try this:
NSString *strURI = #"https://cid.hooru.mobi:36610/?";
strURI = [strURI stringByAppendingFormat:#"&Cust=IPRR%d", strCust];
//Note the %d (if strCust is an int. If it's an NSString use %#)
NSURL *myUrl = [NSURL UrlWithString:strURI];
[NSURL URLWithString:] returns pointer of type NSURL. Merely collecting the return value that is NSURL* type in NSString* type does not convert it to NSString* type. Therefore strUIR is NSURL* type, even though declared as NSString strUIR, and hence you cannot pass any message to strUIR that is supposed to be passed to NSString type.
NSURL doesn't respond to stringByAppendingFormat:
You specify strURI as an NSString so the compiler won't raise a warning, but you set it to an NSURL.
Either initialize the full string before creating the NSURL, or use URLByAppendingPathComponent:, I would recommend the first option.
NSString *path = #"https://cid.hooru.mobi:36610/?"
...
path = [path stringByAppendingFormat:#"&Cust=IPRR%#", strCust];
NSURL *url = [NSURL URLWithString:path]
For more information, see the docs: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html

Resources