Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-iCloud use of UIDocument

I'm trying to use UIDocument without iCloud when the user has iCloud disabled. I have the following code:

NSURL *url;
if (_isiCloudEnabled) {
    NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
    url = [ubiq URLByAppendingPathComponent:[NSString stringWithFormat:@"%f.adoc",[[NSDate date] timeIntervalSince1970]]];
} else {
    NSString *homeDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *newFilePath = [homeDirectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%f.adoc", [[NSDate date] timeIntervalSince1970]]];
    url = [NSURL URLWithString:newFilePath];
}

ASListyDocument *d = [[ASListyDocument alloc] initWithFileURL:url];

This code gives me the error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'must pass a valid file URL to -[UIDocument initWithFileURL:]'

Any ideas why? I looked at the URL in the debugger - it appeared valid. I tried running it in the simulator and on the phone - same problem!

By the way, I'm running on a device with iOS 5.0, in case it matters.

like image 268
Adrian Sarli Avatar asked Apr 19 '12 12:04

Adrian Sarli


1 Answers

It's likely the URL you're building with

NSString *homeDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *newFilePath = [homeDirectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%f.adoc", [[NSDate date] timeIntervalSince1970]]];
url = [NSURL URLWithString:newFilePath];

isn't actually a URL, but a path. Don't' forget that a valid URL needs a scheme; for files, this is "file://". Try building the file URL using [NSURL fileURLWithPath:]

url = [NSURL fileURLWithPath:newFilePath];
like image 75
Adam Wright Avatar answered Oct 18 '22 19:10

Adam Wright