0

The following code:

- (void)ParseFile:(id)sender
{
NSLog(@"Parsing URL: '%@'", FileURL);
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:FileURL];
[parser setDelegate:self];
parser.shouldProcessNamespaces = NO;
if ([parser parse]) {
    NSLog(@"parse successful.");
    [self performSelector:@selector(PostProcessing:) withObject:nil afterDelay:0.1];
    }
else {
    NSError* perror = [parser parserError];
    NSLog(@"parseFailed. Error %ld at line %ld col %ld:", (long)[perror code], (long)[parser lineNumber], (long)[parser columnNumber]);
    NSLog(@" > %@", [perror localizedDescription]);
    NSLog(@" > %@", [perror localizedFailureReason]);
    }
return;
}

When called by:

FileURL = [NSURL fileURLWithPath:@"/Users/me/Documents/Target.xml"];
[self performSelector:@selector(ParseFile:) withObject:nil afterDelay:0.1];

Returns the following:

Parsing URL: 'file:///Users/me/Documents/Target.xml'
parseFailed. Error 0 at line 0 col 0:
  > (null)
  > (null)

(yes, the file exists). But when I call it from the following,

NSOpenPanel* panel = [NSOpenPanel openPanel];
panel.canChooseDirectories = YES;
panel.canChooseFiles = YES;
panel.allowsMultipleSelection = NO;
[panel beginWithCompletionHandler:^(NSInteger result) {
    if (result == 1) {
        FileURL = panel.URL;
        NSLog(@"OnFileOpen:  '%@'", FileURL);
        [self performSelector:@selector(ParseFile:) withObject:nil afterDelay:0.1];
        }
    return;
    }];

and select the same file, it works as expected:

OnFileOpen:  'file:///Users/me/Documents/Target.xml'
Parsing URL: 'file:///Users/me/Documents/Target.xml'
parse successful.

WTF is the difference?

4
  • Is you app sandboxed and does your app have permission to access "User selected Files" and files in the Documents folder? Commented Mar 9, 2022 at 23:10
  • No and Yes, in that order. Even so, why does it work if I select the file via NSOpenPanel? Wouldn't the same restrictions apply? Commented Mar 10, 2022 at 1:46
  • The URL from NSOpenPanel is a "User selected File", the user did select it. Commented Mar 10, 2022 at 8:36
  • Willeke - your answer made me go back and double-check on the Sandbox. I thought I had eliminated it, but in fact I had not. That solved the problem. Thank you. Commented Mar 10, 2022 at 14:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.