【问题标题】:Shared files between IOS app and watch extensionIOS 应用和手表扩展之间的共享文件
【发布时间】:2023-03-15 15:24:01
【问题描述】:

我有一个带有手表扩展程序的 IOS 应用。我使用带有 Core Data 的只读 SQLite 文件(已填充且未编辑)。

使用此代码从捆绑资源访问文件可以正常工作。

- (NSURL *)storeURL{
    NSString * databaseFileName = @"db.sqlite";
    return  [NSURL fileURLWithPath: [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: databaseFileName]];
}

但是,这段代码会创建一个新的 sqlite 文件并将其附加到 NSPersistentStoreCoordinator:

- (NSURL *)applicationDocumentsDirectory {
    return [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.xxx.xxx"];
}

- (NSURL *)storeURL{
    NSString * databaseFileName = @"db.sqlite";

    NSURL * url = [self applicationDocumentsDirectory];
    NSString * path = [url.absoluteString stringByAppendingPathComponent:databaseFileName];

    path = [path stringByReplacingOccurrencesOfString:@"file:" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, path.length)];

    return [NSURL fileURLWithPath: path];
}

如何将 SQLite 文件添加到 Xcode 中的共享应用组容器中?

【问题讨论】:

    标签: ios watchkit watchos


    【解决方案1】:

    虽然这在 watchOS 1 中是可能的(因为手表扩展程序曾经在 iPhone 上运行),但您不能再使用共享应用程序组在 iPhone 和 Apple Watch 之间共享核心数据存储。

    可以做的就是将该只读 SQLite 文件的副本包含在您的 watchOS 应用程序包中,方法是将其添加到手表应用程序的目标中。然后您可以使用您原来的storeURL 代码将该存储添加到持久存储协调器。

    唯一的缺点是分布会更大,因为嵌入式手表应用现在包含 SQLite 文件的单独副本。

    【讨论】:

    • 感谢您的详细解答。我已经做到了,手表应用程序运行得很好。 sqlite 文件大小为 178 MB 的问题。该应用程序将被 Apple 拒绝,因为最大大小为 50 MB。
    • 我会按照this question 的建议来减小文件大小。
    猜你喜欢
    • 2015-11-20
    • 2018-07-12
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    相关资源
    最近更新 更多