Splash screen while receiving web service data

Asked

Viewed 227 times

0

Hello! I have a following problem: I need a splash screen that stays on the screen while the app receives the data from the web service to popular a BD sqlite on iPhone. I tried to use the Xcode image Launch, but the image is only on the screen for 2 seconds, and then the screen turns black.

Has anyone been there? Thank you!!

2 answers

2


At the time of application:didFinishLaunchingWithOptions: in Appdelegate, your app is ready to display the root controller, code-defined in that method itself or in IB if you’re using storyboard.

If you make the call to the webservice synchronously, within application:didFinishLaunchingWithOptions:, your application will get stuck on the splash screen, but this is not a good practice, block the user interface.

So in your root controller, while waiting for the data to be returned from the service, do the following:

IN THE CODE:

#import "ViewController.h"

@interface ViewController ()
// crie uma propriedade para poder acessa-la no retorno dos dados do serviço.
@property (strong, nonatomic) UIView *splashScreenView;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Cria a splash view
    self.splashScreenView = [[UIView alloc] initWithFrame:self.view.bounds];
   // define o background
    self.splashScreenView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"splash-screen"]];
    // cria o spinner
    UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [loading startAnimating];
    // adiciona o spinner na splash view
    [self.splashScreenView addSubview:loading];
    // posiciona o spinner no centro da splash view.
    loading.center = self.splashScreenView.center;
    // adiciona a splash view na view do controller
    [self.view addSubview:self.splashScreenView];
}

- (void)meusDadosDoWebserviceRetornaAqui
{
    // quando receber seus dados, remova a splash view da view do controller.
    [self.splashScreenView removeFromSuperview];
}

Of course, you can do all this with a few milling machines, like an animation and such... Besides this View can also be done in IB, with a NIB file.

That’s up to you!

And the result is this below:

inserir a descrição da imagem aqui

  • Yes this will help a lot!! I will be testing and already bring the result!! I had already come up with something like this, but I was having a hard time implementing it, your example code fell like a glove!!

  • your code works!! That’s good, but it doesn’t show up. Because what happens: The method that starts the download of the contents is in viewDidload of Viewcontroller. m So when the app starts running, it enters this method and only comes out when it finishes downloading everything. And it shows nothing on the screen, not even using the code you passed. The idea would be to divide this into two processes, the splash being the main thread, and the download in the background in another thread. @Rodrigo Salles

1

Cannot control splash to make it last longer on screen.

I suggest you create a new viewcontroller that shows the splash screen image (default.png) in the method application:didFinishLaunchingWithOptions: of AppDelegate and if you want, place a loading message or a UIActivityIndicator to give feedback to the user that something is happening behind.

  • Thanks for the guidance! But you would have a snippet of code showing how I could do?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.