Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm opening a local file using UIWebView:loadRequest with an NSURLRequest which is in turn set from a URL.

The base location for url is obtained using:

    baseDirectory = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory
                                                                        inDomain:NSUserDomainMask
                                                                appropriateForURL:nil   
                                                                            create: YES
                                                                            error:&err];

This returns a URL of the form:

file://localhost/var/mobile/Applications/Library/ApplicationSupport/ABC/XYZ/page.html

However when the UIWebViewDelegate shouldStartLoadForRequest:(NSURLRequest*) method gets called the NSURLRequest that is passed has changed to the following:

file:///var/mobile/Applications/Library/ApplicationSupport/ABC/XYZ/page.html

Both of these therefore reference the same file, however I have a situation where I need to make a comparison between the two (I needs to compare the /ABC/XYZ/page.html parts) and NSURL:isEqual returns NO when comparing the two.

Is there either: a) a method of NSFileManager that will return file:///var/mobile/... instead of file://localhost/var/mobile/...

or

b) easily extract just the /ABC/XYZ/page.html part and perform the comparison on that?

share|improve this question
    
This problem isn't right but hear me out: Wouldn't it make sense for apple to not allow apps to get information form the root of the file system? Apps are not meant to even access simple file such as HTML files that are outside of there sandbox. –  Sirens Jun 29 '12 at 1:05
add comment

2 Answers

up vote 0 down vote accepted

If you know that both files will always be on the same machine, then [[URL1 path] isEqualToString:[URL2 path]].


The following unit tests pass:

- (void)testURLPath
{
    NSURL *URL = [NSURL URLWithString:@"file://localhost/foo/bar/baz"];
    NSString *path = [URL path];
    STAssertEqualObjects(path, @"/foo/bar/baz", nil);
}

- (void)testURLPathCompare
{
    NSURL *URL1 = [NSURL URLWithString:@"file://localhost/foo/bar/baz"];
    NSURL *URL2 = [NSURL URLWithString:@"file:///foo/bar/baz"];
    NSString *path1 = [URL1 path];
    NSString *path2 = [URL2 path];
    STAssertTrue([path1 isEqualToString:path2], nil);
    STAssertTrue([[URL1 path] isEqualToString:[URL2 path]], nil);
}
share|improve this answer
    
But isn't the path part going to include /localhost in but not in the other. –  Amino acids Jun 29 '12 at 1:36
    
@ChrisP.Bacon added unit tests to demonstrate. –  Jeffery Thomas Jun 29 '12 at 2:23
add comment

You could try calling URLByStandardizingPath on both URLs and comparing those results instead of the originals.

If that doesn't work, you could compare just the last two path components by calling pathComponents on both URLs and comparing the last and next-to-last objects in both returned arrays.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.