Pergunta

Which would be the best approach to prevent that an app could be used after some days of "testing"? Lets say I have to distribute my app using Ad Hoc distribution, the user has only one week to test, after that he should not be able to use the app.

Thanks in advance.

Foi útil?

Solução

I do the following to put a time limit in the app for beta testing:

#ifdef BETA
    NSString *compileDate = [NSString stringWithFormat:@"%s %s", __DATE__, __TIME__];
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"MMM d yyyy HH:mm:ss"];
    NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    [df setLocale:usLocale];
    NSDate *aDate = [df dateFromString:compileDate];
    NSDate *expires = [aDate dateByAddingTimeInterval:60 * 60 * 24 * 7]; // 7 days
    NSDate *now = [NSDate date];
    if ([now compare:expires] == NSOrderedDescending) {
        NSAssert(0, @"Sorry, expired");
    }
#endif

where BETA is a compile flag I set only for adhoc builds.

I put this code in the applicationWillEnterForeground: app delegate method.

Outras dicas

Every time an app is built by Xcode, it creates a file Info.plist in the app's bundle. We can get the modification date from that file to determine how long it has been since it was built.

#if BETA
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    const NSTimeInterval kExpirationAge = 60 * 60 * 24 * 7;   // 7 days

    NSString* infoPlistPath = [[NSBundle mainBundle] pathForResource: @"Info" ofType: @"plist"];
    NSDictionary* fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:infoPlistPath error:NULL];
    NSDate* buildDate = (NSDate*) [fileAttributes objectForKey:NSFileModificationDate];
    const NSTimeInterval buildAge = -[buildDate timeIntervalSinceNow];

    if (buildAge > kExpirationAge) {
        UIAlertView* av = [[UIAlertView alloc] initWithTitle:@"App Expired"
                                                     message:@"This version is expired.  Please update to the latest version of this app."
                                                    delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [av show];

        // after 10 seconds the app exits
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
            exit(0);
        });
    }
}
#endif
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top