Search code examples
objective-cxcodecocoaxcode4

Show activity indicator during application launch


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?


Solution

  • 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.