Show activity indicator during application launch

10,165

Solution 1

I don't think this is possible from looking at the docs for UIApplicationDelegate

The earliest your app is called is in

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Which as the name suggests is when your app hasFinishedLaunching

The reason nothing happens when you show and hide the indicator is to do with threading. The user interface will only update at the end of your method where the net result of your changes will be that the indicator is hidden.

Solution 2

For all those who needed this, and i know there were many...
(Made on Xcode version 4.2.1)

So...

In the AppDelegate.h add this:

@property (nonatomic, strong) UIImageView *splashView;

In the AppDelegate.m add this:

On the top of the page of cours @synthesize splashView;
And then:

- (void) splashFade
{
    splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320, 480)];
    splashView.image = [UIImage imageNamed:@"Default.png"];
    [_window addSubview:splashView];
    [_window bringSubviewToFront:splashView];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.0];
    [UIView setAnimationDelay:2.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:_window cache:YES];
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(startupAnimationDone:finished:context:)];
    splashView.alpha = 0.0;
    [UIView commitAnimations];

    //Create and add the Activity Indicator to splashView
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    activityIndicator.alpha = 1.0;
    activityIndicator.center = CGPointMake(160, 360);
    activityIndicator.hidesWhenStopped = NO;
    [splashView addSubview:activityIndicator];
    [activityIndicator startAnimating];
}

- (void)startupAnimationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    [splashView removeFromSuperview];
}

The [UIView setAnimationDelay:2.5] is responsible for how long the splashView will be in front by the delay time you choose.

You can change the position of the indicator by changing the nubmers of x/y in:
activityIndicator.center = CGPointMake(160, 360);

At last, under the methode:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Just add this:

[self performSelector:@selector(splashFade) withObject:nil];

And there you go :)
Hope it helped.

Have a nice programming....

Solution 3

Another solution is to let the launchimage load and when your ViewController loads at the viewDidLoad, use overlay + indicator on top of your view with alpha below 1:

UIView *baseView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.view addSubview:baseView];
[baseView setBackgroundColor:[UIColor blackColor]];
baseView.userInteractionEnabled = NO;
baseView.alpha = 0.4;
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
indicator.center = self.view.center;
[self.view addSubview:indicator];
[indicator bringSubviewToFront:self.view];
[indicator startAnimating];

Now comes the fun part, use delegate to remove the indicator + overlay when all your background threads finish to load up and simple call a method via your delegate:

- (void) onFinishLoading{
        [indicator stopAnimating];
        [baseView removeFromSuperview];
}
  • In order to stopAnimation and removeFromSuperView you might want to make baseView and indicator property + @synthesize
Share:
10,165
Tanoro
Author by

Tanoro

Updated on June 04, 2022

Comments

  • Tanoro
    Tanoro about 2 years

    I am trying to add an activity indicator during startup. I did have a launch image, but I'd rather have just an indicator alone. I added the following to my app delegate, but the indicator doesn't appear.

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // *******Create activity indicator****
        UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(225, 115, 30, 30)];
        [activity setBackgroundColor:[UIColor clearColor]];
        [activity setActivityIndicatorViewStyle: UIActivityIndicatorViewStyleGray];
        [window addSubview: activity];
        [activity release];
    
        [activity startAnimating];
        activity.hidden = FALSE;
        // *******End activity indicator****
    
        MainViewController *viewController = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];
        self.mainViewController = viewController;
    
        [window addSubview:[mainViewController view]];
        mainViewController.view.frame = CGRectMake(0, 20, 320, 411);
    
        [window addSubview:[rootController view]];
        [window makeKeyAndVisible];
    #if !TARGET_IPHONE_SIMULATOR
        [application registerForRemoteNotificationTypes: 
         UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
    #endif
    
        application.applicationIconBadgeNumber = 0;
    
        // Hide indicator
        [viewController release];
        activity.hidden = TRUE;
    
        [activity stopAnimating];
        return YES;
    }
    

    I visited this question from back in 2009, but the solution didn't work for me. iPhone-SDK:Activity Indicator during Startup?

  • Tanoro
    Tanoro almost 13 years
    No, I'm keeping frameworks as narrow as possible. The loading time is roughly 2 seconds or less. My boss just wanted an indicator and aesthetics more than anything, so it's not important if it can't be done.
  • Ahmed Saeed
    Ahmed Saeed about 4 years
    How to use the above solution with Xcode version 11.4?