强制iOS应用程序(Ad Hoc分发)在一段时间后停止工作

时间:2013-09-26 18:04:55

标签: ios adhoc

哪种方法可以防止应用程序在“测试”几天后使用?假设我必须使用Ad Hoc分发来分发我的应用程序,用户只有一周的时间来测试,之后他就不能使用该应用程序。

提前致谢。

2 个答案:

答案 0 :(得分:3)

我执行以下操作以在应用中设置时间限制以进行beta测试:

#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

其中BETA是一个编译标志,我只为adhoc版本设置。

我将此代码放在applicationWillEnterForeground: app委托方法中。

答案 1 :(得分:0)

每次Xcode构建应用程序时,它都会在应用程序包中创建一个文件Info.plist。我们可以从该文件中获取修改日期,以确定自生成以来的时间。

#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