简体   繁体   English

iOS如何做预先填充的Core数据?

[英]iOS how to do pre-filled Core data?

I need my application work as follows: 我需要我的申请工作如下:

1- User download and startup the application. 1-用户下载并启动应用程序。

2- The application will automatically fill the (based core data) with specific records. 2-应用程序将自动使用特定记录填充(基于核心数据)。 3- The application will works ok. 3-该应用程序将正常工作。

4- When the user close the application and restart it. 4-用户关闭应用程序并重新启动时。

5_ The application will not automatically fill the core data with the specific records because it already there. 5_应用程序不会自动使用特定记录填充核心数据,因为它已经存在。

6- The user will not be able to add/remove/update the data. 6-用户将无法添加/删除/更新数据。

Is there a good technique to do that using only core data and accepted by Apple. 是否有一种很好的技术可以仅使用核心数据并被Apple接受。

  1. Populate the data in the simulator 填充模拟器中的数据
  2. Make a copy of the SQLITE database from the simulator folder 从模拟器文件夹中复制SQLITE数据库
  3. Add the database as a resource to your project. 将数据库作为资源添加到项目中。
  4. Do something like this before initializing your Core Data stack: 在初始化Core Data堆栈之前执行类似的操作:

code: 码:

// Put down default db if it doesn't already exist
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
    NSString *defaultStorePath = [[NSBundle mainBundle] 
                                  pathForResource:@"MyDataFile" ofType:@"sqlite"];
    if (defaultStorePath) {
        [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];

    }
}

Not shown, but critical: all the files you prepopulate data for must be marked as "do not backup", or you will be rejected, per the App Store guidelines. 未显示,但很关键:您预填充数据的所有文件必须标记为“不备份”,否则您将被拒绝,符合App Store指南。 See this SO question for some details on how to do this: Why was my application still rejected after excluding files from iCloud backup using this code? 有关如何执行此操作的详细信息,请参阅此SO问题: 为什么在使用此代码从iCloud备份中排除文件后,我的应用程序仍然被拒绝?

I can tell you one very easy way to do it. 我可以告诉你一个非常简单的方法。 In the app delegate, create a variable in NSUserDefaults and change its value when the application is loaded for the first time. 在app delegate中,在NSUserDefaults中创建一个变量,并在第一次加载应用程序时更改其值。 Then depending upon the value of NSUserDefaults, fill the data you want in the Core Data store. 然后,根据NSUserDefaults的值,在Core Data存储中填充所需的数据。 And this will happen only once because the value of NSUserDefaults variable is not going to change again. 这只会发生一次,因为NSUserDefaults变量的值不会再次改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iCloud和Core Data预填充数据库 - iCloud and Core Data pre-filled database iCloud + CoreData - 如何避免预先填充的数据重复? - iCloud + CoreData - how to avoid pre-filled data duplication? Facebook iOs SDK - 在用户的墙上发布预先填写的消息 - Facebook iOs SDK - post a pre-filled message on users' wall 使用预先填写的新电话号码在 iOS 上打开 whatsapp - Open whatsapp on iOS with new phone number pre-filled 是否可以使用来自应用程序的预填充数据打开IOS日历应用程序的添加事件到日历屏幕? - Can I open the the add event to calendar screen of the IOS calendar app with pre-filled data from my app? iOS 7预填充的核心数据数据库不包含所有数据 - iOS 7 pre filled Core Data database does not contain all data iPhone:Facebook FBDialog预填充消息 - iPhone: Facebook FBDialog with pre-filled Message 交付具有已启用读写功能(也是预填充)的sqlite数据库的iOS应用 - Shipping an iOS app with a read-write enabled (also pre-filled) sqlite database 使用Twilio从iOS手机发送预填充消息,而不是要求用户输入代码? - Using Twilio to send pre-filled message from iOS handset instead of asking user to enter code? 如何使用预填充的消息打开WhatsApp聊天,该消息跨越了我网站上的多行? - How to open a WhatsApp chat with a pre-filled message that spans on multiple lines from my website?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM