You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS导航应用中实现插屏广告展示前的定时自定义加载页面

Got it, let's solve this problem properly. You need a customizable, non-interactive loading screen that stays up for a set duration (like 5 seconds) before showing your IronSource interstitial ad, and you want better control than just a basic activity indicator. Here's a step-by-step implementation:

1. Create a Custom Loading Screen Component

First, let's build a reusable loading view controller that you can tweak with custom text, styling, and indicators. This will cover the entire screen and block user interactions:

@interface LoadingAdViewController : UIViewController
@end

@implementation LoadingAdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Dim the background to focus attention on the loading state
    self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
    // Block user taps from passing through to the app below
    self.view.userInteractionEnabled = YES;
    
    // Build a vertical stack for the indicator and text
    UIStackView *stackView = [[UIStackView alloc] initWithArrangedSubviews:@[[self activityIndicator], [self loadingLabel]]];
    stackView.axis = UILayoutConstraintAxisVertical;
    stackView.spacing = 16;
    stackView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:stackView];
    
    // Center the stack on the screen
    [NSLayoutConstraint activateConstraints:@[
        stackView.centerXAnchor.constraintEqualToAnchor:self.view.centerXAnchor,
        stackView.centerYAnchor.constraintEqualToAnchor:self.view.centerYAnchor
    ]];
}

- (UIActivityIndicatorView *)activityIndicator {
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    indicator.translatesAutoresizingMaskIntoConstraints = NO;
    [indicator startAnimating];
    return indicator;
}

- (UILabel *)loadingLabel {
    UILabel *label = [[UILabel alloc] init];
    label.text = @"Sponsored ad loading";
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:16];
    label.textAlignment = NSTextAlignmentCenter;
    label.translatesAutoresizingMaskIntoConstraints = NO;
    return label;
}

@end

2. Implement the Loading Flow with Timing & Ad Readiness

We'll handle two scenarios to balance user experience and ad reliability:

  • Wait a fixed 5 seconds before showing the ad
  • If the ad finishes loading before 5 seconds, show it immediately

Update your main code to manage the loading screen, timer, and IronSource's ad status callbacks:

@interface YourAppDelegate () // Or the class where you trigger the ad flow
@property (nonatomic, strong) LoadingAdViewController *loadingVC;
@property (nonatomic, strong) NSTimer *adLoadingTimer;
@end

@implementation YourAppDelegate

// Call this method to start the full pre-ad loading flow
- (void)startAdLoadingFlow {
    // Present the custom loading screen over your root view
    UIViewController *rootController = self.window.rootViewController;
    self.loadingVC = [[LoadingAdViewController alloc] init];
    self.loadingVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
    [rootController presentViewController:self.loadingVC animated:YES completion:nil];
    
    // Start the 5-second timer
    self.adLoadingTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(showAdAfterTimer) userInfo:nil repeats:NO];
    
    // Make sure you've set up IronSource delegates first to listen for ad load status!
}

- (void)showAdAfterTimer {
    [self dismissLoadingScreenAndShowAd];
}

// Call this from IronSource's interstitial loaded delegate method
- (void)onInterstitialLoaded {
    // If the timer is still running, cancel it and show the ad right away
    if ([self.adLoadingTimer isValid]) {
        [self.adLoadingTimer invalidate];
        self.adLoadingTimer = nil;
        [self dismissLoadingScreenAndShowAd];
    }
}

// Helper to clean up and trigger the ad
- (void)dismissLoadingScreenAndShowAd {
    [self.loadingVC dismissViewControllerAnimated:YES completion:^{
        self.loadingVC = nil;
        // Call your original ad display method
        [self showStartupAd];
    }];
}

// Your original ad show method (unchanged)
-(void)showStartupAd{
    UIViewController *rootController = self.window.rootViewController;
    [IronSource showInterstitialWithViewController:rootController placement:0];
}

// Handle ad load failures to avoid leaving users stuck on the loading screen
- (void)onInterstitialLoadFailedWithError:(NSError *)error {
    // Clean up timer and loading screen
    if ([self.adLoadingTimer isValid]) {
        [self.adLoadingTimer invalidate];
        self.adLoadingTimer = nil;
    }
    [self.loadingVC dismissViewControllerAnimated:YES completion:^{
        self.loadingVC = nil;
        // Optionally show a message to users or skip the ad entirely
    }];
}

@end

3. Key Implementation Tips

  • IronSource Delegate Setup: Ensure your class conforms to IronSourceInterstitialDelegate and sets the delegate ([IronSource setInterstitialDelegate:self];) to receive load status updates.
  • Customization: Tweak the LoadingAdViewController to add logos, adjust colors, or change text to match your app's brand.
  • Memory Cleanup: We properly invalidate timers and dismiss view controllers to prevent memory leaks.
  • User Experience: The loading screen blocks interactions, so users can't accidentally navigate away while waiting for the ad.

内容的提问来源于stack exchange,提问作者user1695971

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 09:47:41