-4

假设 John 使用我的应用程序 3-6 分钟。然后我想弹出一个视图,在我的情况下,包括一个广告。

像这样的东西,

 AdViewController *adViewController = [[AdViewController alloc] init];
 [self presentViewController:adViewController animated:YES completion:nil];

但是我怎样才能让它在随机时间后弹出呢?我想我必须使用委托文件并使用 arc4random 函数。

约翰看过广告后,他必须关闭它,但这不是问题..

有人可以给我一个代码示例吗?

4

1 回答 1

1

简单的解决方案是

  • 创建一个 NSTimer 并让它每 300 秒(5 分钟)触发一次
  • NSTimer 将触发一个显示您的弹出窗口的操作。

我不明白为什么这么难理解?

//use arc4random() if you need random time


NSTimer *timer2 = [NSTimer scheduledTimerWithTimeInterval:300.0 target:self selector:@selector(rateThisApp) userInfo:nil repeats:YES];

// *********
// ********* RATE APP ***********
// *********
- (IBAction)rateThisApp
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Rate this App"
                                                                message:@"Are you enjoying this app? Please leave a rating at the app store and tell us what you think of this app and its features. We would love to hear from you!"
                                                               delegate:self cancelButtonTitle:@"Not Now"
                                                      otherButtonTitles:@"Rate Now", nil];
        [alert show];
        alert.tag = 400;

}


-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag == 400)
    {
        if (buttonIndex == 0)
        {
            //dont do anything, user hit cancel
        }
        else if (buttonIndex == 1)
        {
            [[UIApplication sharedApplication]
             openURL:[NSURL URLWithString:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=1234567"]];
        }
    }

}
于 2013-11-22T14:34:09.130 回答