如何在本地存储和获取iphone数据

时间:2022-12-08 11:43:07

What I want is:

我想要的是:

  1. Store data in iphone.
  2. 将数据存储在iphone中。
  3. Display stored data.
  4. 显示存储的数据。

Can I know simple way to do it?

我可以知道简单的方法吗?

Thanks

谢谢

3 个解决方案

#1


0  

iPhone SDK Tutorial: Reading data from a SQLite Database and 12 SQLite Resources for iPhone Developers should get you started.

iPhone SDK教程:从SQLite数据库中读取数据,为iPhone开发人员提供12个SQLite资源应该可以帮助您入门。

#2


0  

If you're not opposed to just serializing objects and storing them as files, I think the easiest (not the best ofcourse) way would be to do just write to a file on disk. (Cusomize as needed, this is a basic routine which assumes a lot of things like key being unique etc.) -

如果你不反对只是序列化对象并将它们存储为文件,我认为最简单(不是最好的)方法就是写入磁盘上的文件。 (根据需要进行Cusomize,这是一个基本的例程,假设很多东西,如密钥是独特的等等) -

-(BOOL) setObjectWithKey:(NSString *) key andValue:(NSString *) value {
    NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:value forKey:key];
    [archiver finishEncoding];
    [data writeToFile:[self dataFilePath:key] atomically:YES];
    [archiver release];
    [data release];
    return YES;
}
-(void) removeObjectFor:(NSString *) key
{
    NSFileManager *fm = [NSFileManager defaultManager];
    BOOL exists = [fm fileExistsAtPath:[self dataFilePath:key]];
    if (exists == YES) {
        [fm removeItemAtPath:[self dataFilePath:key] error:NULL];
    }
    return;
}

The Object above can actually be any Data model of yours (instead of a plain NSString) consisting of default Foundation Objects like NSStrings, ints etc. In which case, you have to implement some serialization code in your model object. Refer NSCoding, NSCopying.

上面的Object实际上可以是你的任何数据模型(而不是普通的NSString),它由默认的Foundation对象组成,如NSStrings,int等。在这种情况下,你必须在模型对象中实现一些序列化代码。参考NSCoding,NSCopying。

#3


0  

How much data are you trying to store?

您要存储多少数据?

For very basic needs I've used localStorage. It probably wouldn't be good for a lot of data, but can be easily used to access smaller portions of data.

对于非常基本的需求,我使用了localStorage。它对于大量数据可能不太好,但可以很容易地用于访问较小部分的数据。

localStorage.setItem(key, value)
localStorage.getItem(key) //returns value associated with this key

To store more info you could possibly write to and read from a local file using fileWriter and fileReader if you want to implement it using javascript on the phonegap side of things. Just look at the phonegap api.

要存储更多信息,您可以使用fileWriter和fileReader写入本地文件并从中读取,如果您想在phonegap方面使用javascript实现它。看看phonegap api。

#1


0  

iPhone SDK Tutorial: Reading data from a SQLite Database and 12 SQLite Resources for iPhone Developers should get you started.

iPhone SDK教程:从SQLite数据库中读取数据,为iPhone开发人员提供12个SQLite资源应该可以帮助您入门。

#2


0  

If you're not opposed to just serializing objects and storing them as files, I think the easiest (not the best ofcourse) way would be to do just write to a file on disk. (Cusomize as needed, this is a basic routine which assumes a lot of things like key being unique etc.) -

如果你不反对只是序列化对象并将它们存储为文件,我认为最简单(不是最好的)方法就是写入磁盘上的文件。 (根据需要进行Cusomize,这是一个基本的例程,假设很多东西,如密钥是独特的等等) -

-(BOOL) setObjectWithKey:(NSString *) key andValue:(NSString *) value {
    NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:value forKey:key];
    [archiver finishEncoding];
    [data writeToFile:[self dataFilePath:key] atomically:YES];
    [archiver release];
    [data release];
    return YES;
}
-(void) removeObjectFor:(NSString *) key
{
    NSFileManager *fm = [NSFileManager defaultManager];
    BOOL exists = [fm fileExistsAtPath:[self dataFilePath:key]];
    if (exists == YES) {
        [fm removeItemAtPath:[self dataFilePath:key] error:NULL];
    }
    return;
}

The Object above can actually be any Data model of yours (instead of a plain NSString) consisting of default Foundation Objects like NSStrings, ints etc. In which case, you have to implement some serialization code in your model object. Refer NSCoding, NSCopying.

上面的Object实际上可以是你的任何数据模型(而不是普通的NSString),它由默认的Foundation对象组成,如NSStrings,int等。在这种情况下,你必须在模型对象中实现一些序列化代码。参考NSCoding,NSCopying。

#3


0  

How much data are you trying to store?

您要存储多少数据?

For very basic needs I've used localStorage. It probably wouldn't be good for a lot of data, but can be easily used to access smaller portions of data.

对于非常基本的需求,我使用了localStorage。它对于大量数据可能不太好,但可以很容易地用于访问较小部分的数据。

localStorage.setItem(key, value)
localStorage.getItem(key) //returns value associated with this key

To store more info you could possibly write to and read from a local file using fileWriter and fileReader if you want to implement it using javascript on the phonegap side of things. Just look at the phonegap api.

要存储更多信息,您可以使用fileWriter和fileReader写入本地文件并从中读取,如果您想在phonegap方面使用javascript实现它。看看phonegap api。