NSArray how do I restrict data in array when app not purchased?

I have an app that has a total of 32 images that appear at random;

I would like it to be so that, if the user has not purchased a particular upgrade then only the first 16 (f1.png - f16.png only) from the array will show, however if they have bought the purchase, then all 32 would be available at random.

The in-app id's that would allow ALL of the images are;

com.myphotofun.upgrade16more & com.myphotofun.complete

The code to call the random array is;

-(void)randImage
{
    NSArray *myImageNames = [NSArray arrayWithObjects:@"f1.png", @"f2.png", @"f3.png",@"f4.png",@"f5.png",@"f6.png",@"f7.png",@"f8.png",@"f9.png",@"f10.png",@"f11.png",@"f12.png",@"f13.png",@"f14.png",@"f15.png",@"f16.png","f17.png","f18.png","f19.png","f20.png","f21.png","f22.png","f23.png","f24.png","f25.png","f26.png","f27.png","f28.png","f29.png","f30.png","f31.png","f32.png",nil];

           int index = arc4random() % [myImageNames count];

    UIImage *myImage = [UIImage imageNamed:[myImageNames objectAtIndex:index]];

    [PaintingImage setImage:myImage];
}

Now changed to (with thanks to guenis) ;

-(void)randImage
{

NSInteger index = arc4random_uniform([[NSUserDefaults standardUserDefaults] boolForKey:PRODUCT_PURCHASED] ? 32 : 16);

UIImage *myImage = [UIImage imageNamed:[NSString stringWithFormat:@"f%d.png",index +1]];

    [PaintingImage setImage:myImage];

}

Any code assistance would be much appreciated. Thanks,

Chris

Answers


-(void)randImage
{
    NSArray *myImageNames = [NSArray arrayWithObjects:@"f1.png", @"f2.png", @"f3.png",@"f4.png",@"f5.png",@"f6.png",@"f7.png",@"f8.png",@"f9.png",@"f10.png",@"f11.png",@"f12.png",@"f13.png",@"f14.png",@"f15.png",@"f16.png","f17.png","f18.png","f19.png","f20.png","f21.png","f22.png","f23.png","f24.png","f25.png","f26.png","f27.png","f28.png","f29.png","f30.png","f31.png","f32.png",nil];

    NSInteger index = arc4random_uniform([[NSUserDefaults standardUserDefaults] boolForKey:PRODUCT_PURCHASED] ? 32 : 16);

    UIImage *myImage = [UIImage imageNamed:[myImageNames objectAtIndex:index]];

    [PaintingImage setImage:myImage];
}

Don't forget to set the value when product is purchased

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:PRODUCT_PURCHASED];
[[NSUserDefaults standardUserDefaults] synchronize];

Also if your images are named exactly like you have shown I would prefer accessing them with dynamic strings instead of storing them in an nsarray:

    NSInteger index = arc4random_uniform([[NSUserDefaults standartUserDefaults] boolForKey:PRODUCT_PURCHASED] ? 32 : 16);

    UIImage *myImage = [UIImage imageNamed:[NSString stringWithFormat:@"f%d.png",index + 1]];

Hi you can try with this

    - (void)randImage {

    int items = isPurchased ? 32 : 15;

    NSMutableArray *images = [[NSMutableArray alloc] init];

    for (int i = 1; i <= items; i++) {
        [images addObject:[NSString stringWithFormat:@"f%i.png", i]];
    }

    int index = arc4random() % [images count];

    UIImage *myImage = [UIImage imageNamed:[images objectAtIndex:index]];
    [PaintingImage setImage:myImage];

    //Check if it works
    NSLog(@"%@", [images objectAtIndex:index]);
    NSLog(@"%i", images.count);
}

already tested

Good Luck


Need Your Help

How to expand absolute positioned wrapper div when using absolute positioned child div?

html css

I've searched the site for an answer to this one, and from what I can see, the other questions don't involve the parent div using position:absolute;

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.