NSURL URLWithString is shortening very long strings with an ellipsis "...", creating invalid NSURL objects

588 views Asked by At

I am trying to use NSURL URLWithString to create an NSURL object from a potentially very long file path with a very long file name. When I convert the file path to an NSURL object using URLWithString, the path gets shortened and an ellipsis " ... " is put in the NSURL:

Path as NSString: /var/mobile/Containers/Data/Application/APPLICATION_ID/Documents/TEMP/this_is_a_really_really_and_i_mean_reaeeeeaaaallly_long_file_name/this_is_a_really_really_and_i_mean_reaeeeeaaaallly_long_file_name.xml

// Convert to NSURL using this technique:
[NSURL URLWithString:pathAsString]

Path as NSUrl:    /var/mobile/Containers/Data/Application/APPLICATION_ID/Documents/TEMP/this_is_a_really_really_and_ ... name.xml

I can't find any way around this problem, and I get an error when I try to open the file at the NSURL I'm trying to create, saying that there is no such file or directory:

Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x15ec82b0 {NSFilePath=/var/mobile/Containers/Data/Application/APPLICATION_ID/Documents/TEMP/this_is_a_really_really_and_ ... name.xml, NSUnderlyingError=0x15eac4a0 "The operation couldn’t be completed. No such file or directory"}

I don't know why the ellipsis is being added and I can't figure out how to get around this problem.

3

There are 3 answers

3
Christian Schnorr On BEST ANSWER

Your code

NSString *const path = @"/var/mobile/Containers/Data/Application/APPLICATION_ID/Documents/TEMP/this_is_a_really_really_and_i_mean_reaeeeeaaaallly_long_file_name/this_is_a_really_really_and_i_mean_reaeeeeaaaallly_long_file_name.xml";
NSURL *const url = [NSURL URLWithString:path];
NSLog(@"%@", url);
NSLog(@"%@", url.path);

prints

/var/mobile/Containers/Data/Application/APPLICATION_ID/Documents/TEMP/this_is_a_really_really_and_i_mean_reaeeeeaaaallly ... name.xml
/var/mobile/Containers/Data/Application/APPLICATION_ID/Documents/TEMP/this_is_a_really_really_and_i_mean_reaeeeeaaaallly_long_file_name/this_is_a_really_really_and_i_mean_reaeeeeaaaallly_long_file_name.xml

The URL has the path stored correctly, NSURLs description simply shortens it when logging the URL. Your problems appears to lie somewhere else.

0
Tobias On

You should probably use the NSURL factory method.

+ (id)fileURLWithPath:(NSString *)path
0
waldgeist On

I ran into the same problem, because Apache Cordova used the "straightforward" conversion of a NSURL into a string - which abbreviated my URL and broke the interface between my mobile app and my web app.

The simple solution is to use

url.absoluteString

instead of just

url

Obviously, even the iOS cracks at Apache were not aware of this strange behaviour of NSURL. I wouldn't have expected this either.