假設 iOS app 預設啟用時,停留在某個 ViewController:
@interface ViewController () <UIAlertViewDelegate>
// ...
@end
@implementation ViewController
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (alertView.tag) {
case YOUR_APP_ID:
{
//NSLog(@"buttonIndex: %d", buttonIndex);
NSDate * now = [[NSDate alloc] init];
switch (buttonIndex) {
case 1: // YES
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%d", alertView.tag]]];
[[NSUserDefaults standardUserDefaults] setObject:now forKey:@"rateDone"];
break;
case 2: // Remind me later
[[NSUserDefaults standardUserDefaults] setObject:now forKey:@"rateDate"];
break;
default:
[[NSUserDefaults standardUserDefaults] setObject:now forKey:@"rateDone"];
break;
}
[[NSUserDefaults standardUserDefaults] synchronize];
}
break;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
@try {
//[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"rateDone"];
//[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"rateDate"];
//[[NSUserDefaults standardUserDefaults] synchronize];
if (![[NSUserDefaults standardUserDefaults] objectForKey:@"rateDone"]) {
NSDate * now = [[NSDate alloc] init];
if (![[NSUserDefaults standardUserDefaults] objectForKey:@"rateDate"]) {
[[NSUserDefaults standardUserDefaults] setObject:now forKey:@"rateDate"];
[[NSUserDefaults standardUserDefaults] synchronize];
} else {
NSDate *prevDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"rateDate"];
if ([now timeIntervalSinceDate:prevDate] > 60 * 60 * 10) {
UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"Rate this app" message:@"If you enjoy using this app, would you mind taking a moment to rate it?" delegate:self cancelButtonTitle:@"NO, Thanks" otherButtonTitles:@"YES", @"Remind me later", nil];
alterView.tag = YOUR_APP_ID;
[alterView show];
}
}
}
}
@catch (NSException *exception) {
}
@finally {
}
}
@end
沒有留言:
張貼留言