NSURl urlWithString replaces body with dots - ios

I try to convert NSString to NSURL however I receive truncated url.
NSURL *url = [NSURL fileURLWithPath:music.url];
If I make NSLog music.url I get:
/Users/Nikita/Library/Developer/CoreSimulator/Devices/D93FE148-7F72-43BB-A545-06D76C71300B/data/Containers/Data/Application/48BC6629-F054-4679-AC7B-709815EC5E2A/Library/Caches/4219609-357670045.mp3
And when NSLog received url:
/Users/Nikita/Library/Developer/CoreSimulator/Devices/D93FE148-7F72-43BB-A545-06D76C71300B/data/Containers/Data/Applicat ... 0045.mp3
The link is partially truncated.
What is the reason?
Thank you

music.ulr is obviously an instance of NSString. url is an instance of NSURL. Using NSLog() an instance of a class makes its own decision how to be logged.
Objective-C object, printed as the string returned by descriptionWithLocale: if available, or description otherwise. Also works with CFTypeRef objects, returning the result of the CFCopyDescription function.
NSLog() prints only a description, not the complete object, whatever this should be.
Obviously the developer of NSString thought, that it is a good idea to print out the complete string and wrote a description method doing that. The developer of NSURLthought that it is enough to print the string partially, so you can recognize it.

Related

Unity / Objective-C: Trouble converting char* to NSString

I have what should be a dead-simple piece of code that is failing for me in strange ways.
void MediaShare(char* text, char* furl) {
NSString *status = [NSString stringWithUTF8String: text];
NSString *media = [NSString stringWithUTF8String: furl];
[[SocialShare sharedInstance] mediaShare:status media:media];
text is just a line of text for Twitter sharing, and furl is just a file location string. I am getting a crash down past this function that comes down to bad data getting passed. Putting a breakpoint at the head of this function yields the following-
Image of Xcode variable monitor
The two values look fine, although not sure if the * values that only contain the first char are a problem.
Anyway, jumping to the end, and status and media appear to be converted to hex values.
Converted to hex values?
Any ideas? To give the full story, this is a Unity plug-in. I am simply passing in two strings from Unity C# to this function.
Thanks so much.
The code looks fine so far, if the input values are well formed C char* strings that can be interpreted as an UTF-8 encoded string.
status and media being hex values at the end of the function hint that they are. Both are pointers to Objective-C objects, so this is expected. Print them to the debug console or use po status at the debug console to check their contents (it will print the result of the -description method of status which is the string content in that case).
The subsequent crash might be caused elsewhere in the code.
What's the crash's log output?

split string in ios string from json data?

I have one string. Now I wanted to split this string. For static separation I know the code but I don’t code for dynamic value.
my string is
NSString *str = #"https://graph.facebook.com/v2.5/181054825200000/feed?fields=created_time,message,picture,full_picture,comments.limit%280%29.summary%28true%29,likes.limit%280%29.summary%28true%29&limit=5&format=json&access_token=CAALjFrE5mNYBAOg1EDiUrsE2kr1kIRrLIv7g4OweSMvHso2exB5Dttshn7dgOlW24ZCXSnDZAWiV6xMUKXedTXUhiHpdmZBPCGzD1orFlrLRP2gaBZCbZBZBnjUHewF9hZBmJKxtiwVzpw9gnnQXk5Hfx0ZBM2ksAUzkSWR5feaNMbf3UUmUpJlxeh0gKdDrzWBvIJRPy0xGqL0ZAMFsRhyCZCTX42l1sZAceZB0VCeDZB95mrAZDZD&until=1456345291&__paging_token=enc_AdCKD3tSYMoZB3MCKaJkYnbVmBgUyY2tBceGDD2G1hqxRDiQKZCsSbmvWZASLvlCMf0BVzq2uZAScSWp7ZAavZB2d72BIHJISefk09noRuv9gA5b5hFwZDZD";
but i don’t how to show any value dynamically .(for e.g. until (in string))
please help me for this issue.
Thank You.
If you are parsing a URL you should really use NSURLComponents. It makes breaking a URL into the different parts much easier, and the code is tested and verified by Apple.
For separate string by a separator you can use this.
NSString *url = #"<url>";
NSArray *array = [url componentsSeparatedByString:#"<seperator string>"];
NSLog(#"%#", array);
But for URL parsing ,As per Duncan's answer, yes it is good to parse a URL using NSURLComponents. By using this class you can get any desired part of an URL.

How to Hardcode a NSURL path?

So, this seems like it should be super easy, but i'm stumped...
I have an iOS application that I am developing where I am downloading video content from a server, storing it in a custom folder in the Documents, and upon a certain trigger, trying to play the video. I am experiencing some loading errors in my MovieViewController, so am trying to debug the NSURL that is constructed from the file path in a separate standalone application. It is worth noting that the same content works fine when added to the Bundle, but there's quite a lot, so can't afford to do that.
Anyways...
I have set breakpoints in my app and copied out the path of the NSURL that gets loaded into my movie player, and am attempting to hardcode it into an NSURL in my test app. But I can't seem to just assign it with a preexisting value... Eg.
NSURL *url = "file:///var/mobile/Containers/Data/Application/21078F3B-12C5-4D42-8B8B-3C85CB7A0A91/Documents/SecondStory/BloodAlley/MEDIA/copperthief.mp4"
(which is what I copied out of the Variables View of the Debug area).
It gives me the error:
*"Implicit conversion of a non-Objective-C pointer type 'char *' to 'NSURL ' is disallowed with ARC"
... which I cannot seem to track down on SO ....
Of course, I have the NSString representation of the file path before it gets converted to an NSURL, so can just try to reconstruct it, but this got me curious why I couldn't just assign a value...
As the error states, "file:///var/.../copperthief.mp4" is a char * not an NSURL.
To convert it to an NSURL, first you have to get the char *'s NSString representation by adding the # prefix:
#"file:///var/.../copperthief.mp4"
then convert the NSString to an NSURL using URLWithString::
[NSURL URLWithString:#"file:///var/.../copperthief.mp4"];
The first thing is that "..." is not an NSString. It's a C string. An NSString would be #"...", wouldn't it? And even then, if you wanted an NSURL, you'd have to convert from NSString to NSURL, wouldn't you? I mean, you can't assign a string of any kind to an NSSURL variable and expect it to work, can you? This is a computer language; you can't say just any old thing that comes into your head - you have to obey the rules.

ios issue with stringByAddingPercentEscapesUsingEncoding

In my app I need to send some parameters to the url, when I am trying with the stringByAddingPercentEscapesUsingEncoding it is not converting correctly. If I am not using this encoding I am getting null(Exception) from the nsurl.Here is me code.
http://www.mycompurl.co?message=xyz&id=____ here I am sending the id 1 or 2 or any number.
when I convert this string to url by using stringByAddingPercentEscapesUsingEncoding I got
"http://www.mycompurl.co?message=xyz&id=**%E2%80%8B**1" (when I send 1 as parameter). Then I got the 0 data from the Url.
str = [NSString stringWithFormat:#"%#?message=xyz&id=​%#",Application_URL,bootupdateNew];
str = [str stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
url=[NSURL URLWithString:str];
NSError* error = nil;
data1 = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
Thank you In advance
Basics
A URL is composed of several components.
Each component has its own rule how the component's source string must be encoded, so that this component becomes valid within the URL string.
Applying stringByAddingPercentEscapesUsingEncoding: will never always produce a correct URL if the string consists of more than one component (and if we assume, we have an unbounded set of source strings - so that the encoded string actually differs from the source string).
It even won't work always with a string which represents any single component.
In other words, for what's worth, stringByAddingPercentEscapesUsingEncoding: should not be used to try to make a URL out of several components. Even getting the URL query component correctly encoded is at least error prone, and when utilizing stringByAddingPercentEscapesUsingEncoding: it still remains wonky. (You may find correct implementations on SO, though - and I posted one myself).
But now, just forget about it:
It took awhile for Apple to recognize this failure, and invented NSURLComponents. It's available since iOS 7. Take a look! ;)

NSURL withString adds gibberish

I am trying to build a NSURL using a path string. The string looks fine but when I try to put that in an NSURL it gets a bunch of gibberish in it. Here is the code I am using to build up the url:
NSString* path = [[AppSettings instance].contentRootPath stringByAppendingPathComponent: item.fileName];
NSLog(path);
item.contentPath = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(item.contentPath.absoluteString);
And here is the log:
2012-07-25 10:28:39.983 mxHub[44036:207] /Users/casey.borders/Library/Application Support/iPhone Simulator/5.0/Applications/C2C1975D-7FAB-4E6C-A091-DC389223CC57/Documents/.content/PREZISTA-PI.pdf
2012-07-25 10:28:41.654 mxHub[44036:207] /Users/casey.borders/Library/Application扡潳畬整瑓楲杮䄀䅖獳瑥慃档e獡敳䍴捡敨楗桴剕㩌猀穩佥䕦瑮祲潆䭲祥:ㅱ䀲㨰䀴8敲潭敶湅牴䙹牯敋㩹挀牵敲瑮楓敺猀瑥慍䕸瑮祲楓敺:慭䕸瑮祲楓敺猀瑥慍卸穩㩥洀硡楓敺䀀䄢䅖獳瑥慃档䥥瑮牥慮≬䄀䅖獳瑥慃档䥥瑮牥慮l湩瑩楗桴楄瑣潩慮祲:慤慴獕湩䕧据摯湩㩧污潬䱷獯祳潃癮牥楳湯:慣䉮䍥湯敶瑲摥潔湅潣楤杮:湩瑩楗桴慄慴攺据摯湩㩧攀瑸湥敤䱤杯慄慴瑓楲杮湅潣楤杮攀瑸湥敤䱤杯慄慴䄀偖慬敹䥲整䅭捣獥䱳杯开捡散獳潌䅧牲祡攀敶瑮s灟慬敹䥲整䅭捣獥䱳杯䀀䄢偖慬敹䥲整䅭捣獥䱳杯湉整湲污"噁汐祡牥瑉浥捁散獳潌䥧瑮牥慮l潬䅧牲祡䄀偖慬敹䥲整䅭捣獥䱳杯癅湥t畮扭牥晏牄灯数噤摩潥牆浡獥椀摮捩瑡摥楂牴瑡e扯敳癲摥楂牴瑡e畮扭牥晏祂整味慲獮敦牲摥渀浵敢佲卦慴汬s畤慲楴湯慗捴敨d敳浧湥獴潄湷潬摡摥畄慲楴湯瀀慬批捡卫慴瑲晏獦瑥瀀慬批捡卫獥楳湯䑉渀浵敢佲卦牥敶䅲摤敲獳桃湡敧s敳癲牥摁牤獥s剕I汰祡慢正瑓牡䑴瑡e畮扭牥晏敓浧湥獴潄湷潬摡摥开汰祡牥瑉浥捁散獳潌䕧敶瑮䀀䄢偖慬敹䥲整䅭捣獥䱳杯癅湥䥴瑮牥慮≬吀ⱤⱒN噁汐祡牥瑉浥捁散獳潌䕧敶瑮湉整湲污搀捩t┊#噁汐祡牥瑉浥牅潲䱲杯开牥潲䱲杯牁慲y灟慬敹䥲整䕭牲牯潌g≀噁汐祡牥瑉浥牅潲䱲杯湉整湲污"噁汐祡牥瑉浥牅潲䱲杯湉整湲污䄀偖慬敹䥲整䕭牲牯潌䕧敶瑮攀牲牯潃浭湥t牥潲䑲浯楡n牥潲卲慴畴䍳摯e灟慬敹䥲整䕭牲牯潌䕧敶瑮䀀䄢偖慬敹䥲整䕭牲牯潌䕧敶瑮湉整湲污"噁汐祡牥瑉浥牅潲䱲杯癅湥䥴瑮牥慮l椀剳捥牯楤杮攀牲牯楗桴潄慭湩挺摯㩥獵牥湉潦:潶捩䍥湯牴汯敬䕲摮汐祡慢正湉整牲灵楴湯:潶捩䍥湯牴汯敬䕲摮敒潣摲湉整牲灵楴湯:潶捩䍥湯牴汯敬偲慬批捡䉫晵敦䅲慶汩扡敬戺晵敦㩲瘀楯散潃瑮潲汬牥楄卤慴瑲汐祡湩㩧畳捣獥晳汵祬:upport/iPhone ㈜ǪᶀЀꀠ/Āࠀ쵠޴imulator/5.0/Applications/C2C1975D-7FAB-4E6C-A091-DC389223CC57/Documents/.content/Demo.pdf
It's to do with NSLog, nothing to do with your urls :)
Try this :
NSString* path = [[AppSettings instance].contentRootPath stringByAppendingPathComponent: item.fileName];
NSLog(#"%#", path);
item.contentPath = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"%#", item.contentPath.absoluteString);
The first parameter for NSLog is a format string - this tells NSLog that there might be other values to insert into the output (these are the other optional parameters that NSLog takes - for example NSLog(#"%i", 5); would replace the %i with 5).
The NSLog(path); is fine because there aren't any special formatting characters in the path so it outputs as you would expect.
The second NSLog has % characters in - you added them when you asked to % escape the path :) For example, you replaced the space in 'Application Support' with %20.
This means that your url now contains the formatting code %20S (the S is the next character after the space, the first character in the word 'Support').
%S is interpreted by NSLog as 'take the next paramter that I have been given and treat it as a null terminated c-string'.
Unfortunately, you didn't give NSLog another parameter :) However, NSLog didn't know that - it just dutifully did as it was told and took the next value on the stack and added it to the output string.
The next value on the stack is garbage which is why your output is garbage!
You can see that the corrupted values appear instead of the spaces in your path - both spaces happen to be followed by an S so NSLog is dumping garbage in two places. That's why after the first load of garbage you can see 'upport/iPhone' and after the second 'imulator/5.0' :)
When you use NSLog(string), that string is treated as a format statement and any '%' characters will be interpreted.
Try printing the strings out using this form: NSLog(#"%#", string). As a general statement you should ALWAY use the above form and never the one you are using.

Resources