【问题标题】:Non-iCloud use of UIDocumentUIDocument 的非 iCloud 使用
【发布时间】:2012-04-19 12:10:04
【问题描述】:

当用户禁用 iCloud 时,我正在尝试使用没有 iCloud 的 UIDocument。我有以下代码:

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];

这段代码给了我错误:

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

任何想法为什么?我查看了调试器中的 URL - 它看起来是有效的。我尝试在模拟器和手机上运行它 - 同样的问题!

顺便说一句,我正在运行 iOS 5.0 的设备,以防万一。

【问题讨论】:

    标签: iphone ios ios5 icloud


    【解决方案1】:

    这很可能是您正在构建的 URL

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

    实际上不是 URL,而是路径。不要忘记一个有效的 URL 需要一个方案;对于文件,这是“file://”。尝试使用 [NSURL fileURLWithPath:] 构建文件 URL

    url = [NSURL fileURLWithPath:newFilePath];
    

    【讨论】:

    • 非常感谢!这解决了它!
    猜你喜欢
    • 1970-01-01
    • 2013-08-16
    • 2012-12-12
    • 2018-03-13
    • 2018-11-12
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多