更新时保留购买的内容

时间:2012-05-03 17:15:01

标签: objective-c ios xcode ios4 in-app-purchase

我有某个艺术家的音乐应用程序,它包含不同的专辑,用户可以单独购买每个专辑,购买某个专辑时,其按钮从买入变为播放,此专辑的歌曲将列在应用程序中,但当我做了一个更新版本时,我发现当用户更新他的应用程序时,它将重置为默认状态,播放按钮将被购买按钮取代,用户必须重新下载他已经购买的专辑在第一个版本中,我如何保留所有版本中的所有内容,

这就是按钮如何在表格视图3中将图像从“购买”更改为“播放”

 NSArray *myPathList = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *myPath = [myPathList objectAtIndex:0];
NSString* storagePath = [myPath stringByAppendingPathComponent:@"purchasedAlbums.plist"];
NSArray* valueList = [[NSArray alloc] initWithContentsOfFile:storagePath];

UIImage *change1 = [UIImage imageNamed:@"playalbum-06.png"];
UIImage *change2 = [UIImage imageNamed:@"playalbum-07.png"];
UIImage *change3 = [UIImage imageNamed:@"playalbum-08.png"];
UIImage *change4 = [UIImage imageNamed:@"playalbum-09.png"];
UIImage *change5 = [UIImage imageNamed:@"playalbum-11.png"];
UIImage *change6 = [UIImage imageNamed:@"playalbum-12.png"];
UIImage *change7 = [UIImage imageNamed:@"playalbum-10.png"];
UIImage *change8 = [UIImage imageNamed:@"playalbum-13.png"];
UIImage *change9 = [UIImage imageNamed:@"ipadplay-16.png"];
UIImage *change10 = [UIImage imageNamed:@"ipadplay-17.png"];
UIImage *change11 = [UIImage imageNamed:@"ipadplay-18.png"];
UIImage *change12 = [UIImage imageNamed:@"ipadplay-19.png"];
UIImage *change13 = [UIImage imageNamed:@"ipadplay-20.png"];
UIImage *change14 = [UIImage imageNamed:@"ipadplay-21.png"];

int i;
for (i = 0; i < [AlbumsImages count]; i++) {


if ([[valueList objectAtIndex:i+1] intValue]) {
    if (i == 0) {
        [AlbumsImages replaceObjectAtIndex:0 withObject:change1];
    }
    if (i == 1) {
        [AlbumsImages replaceObjectAtIndex:1 withObject:change2];
    }
    if (i == 2) {
        [AlbumsImages replaceObjectAtIndex:2 withObject:change3];
    }
    if (i == 3) {
        [AlbumsImages replaceObjectAtIndex:3 withObject:change4];
    }
    if (i == 4) {
        [AlbumsImages replaceObjectAtIndex:4 withObject:change5];
    }
    if (i == 5) {
        [AlbumsImages replaceObjectAtIndex:5 withObject:change6];
    }
    if (i == 6) {
        [AlbumsImages replaceObjectAtIndex:6 withObject:change7];
    }
    if (i == 7) {
        [AlbumsImages replaceObjectAtIndex:7 withObject:change8];
    }
    if (i == 8) {
        [AlbumsImages replaceObjectAtIndex:8 withObject:change9];
    }
    if (i == 9) {
        [AlbumsImages replaceObjectAtIndex:9 withObject:change10];
    }
    if (i == 10) {
        [AlbumsImages replaceObjectAtIndex:10 withObject:change11];
    }
    if (i == 11) {
        [AlbumsImages replaceObjectAtIndex:11 withObject:change12];
    }
    if (i == 12) {
        [AlbumsImages replaceObjectAtIndex:12 withObject:change13];
    }
    if (i == 13) {
        [AlbumsImages replaceObjectAtIndex:13 withObject:change14];
    }

这就是下载时保存相册的地方

request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:URL]];
                [request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"] 
                                                     stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp3",[self FileNameGetter:Albumid FileID:CurrentSongID ]]]];

那就是应用程序加载时发生的事情

NSArray *myPathList = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *myPath = [myPathList objectAtIndex:0];
NSString* storagePath = [myPath stringByAppendingPathComponent:@"purchasedAlbums.plist"];
NSArray* valueList = [[NSArray alloc] initWithContentsOfFile:storagePath];
if ([valueList count]==0) {
    valueList = [NSArray arrayWithObjects: @"0" ,@"0" ,@"0" , @"0" ,@"0" ,@"0" , @"0" ,@"0" ,@"0" , @"0" ,@"0" ,@"0" , @"0" ,@"0" ,@"0" , nil];
    [valueList writeToFile:storagePath atomically:true];
} 
valueList=nil;
myPathList=nil;
myPath =nil;
storagePath =nil;
[valueList release];
[myPathList release];
[myPath release];
[storagePath release];

我非常感谢任何帮助:)

1 个答案:

答案 0 :(得分:1)

首先,如果您只想通过更新保留内容,请将其保存在DocumentsDirectory而不是Caches中。

为了保持购买进度,您可以使用restoreTransactions