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.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With