简体   繁体   中英

How to load data from web-service while displaying splash screen?

I want to load some data from web at the starting of my app launch. I have set a splash screen and in didFinishLaunchingWithOptions I have set the sleep(10). Now I want that in this time interval my app call the web service and load the data but I am not able to do this. Please help me out or give any suggestion to do it.

During sleep, the thread (in this case, the main thread) is unable to do anything.

I would recommend you simply show the splash screen, start loading the data and hide the splash screen once all the data has been loaded.

Big Question!

First of all, don't make the Main thread sleep, nothing would work there, so it is just wasting time.

Instead, Setup something like an Updating Page with constantly running UIScrollView, Which would disappear only when your data has been fetched.

Use the delegate for the webservice, through which you would call a function in AppDelegate to remove the Loader View and add HOMEPAGE, when data has been fetched.

Something like,

this is just an example...

- (void) webserviceDidFinishLoading  //write in appdelegate.m
{
      [self.activityIndicatorView removeFromSuperView];
      self.window.rootController = self.homeViewController;
}

Hope this helps! :)

hey mate see bellow code..

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
    {
        splashView = [[UIImageView alloc] initWithFrame:iphoneFrame];
        splashView.image = [UIImage imageNamed:@"Default"];
        [self.window addSubview:splashView];
        //// load you web-service here and get data. After 2 sec iphone rootview controller will display
        [self performSelector:@selector(loadViewIphone) withObject:nil afterDelay:2.0];
     }
}

-(void)loadViewIphone 
{
    [splashView removeFromSuperview];

    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    CATransition *animation = [CATransition animation];
    [animation setDelegate:self];   
    [animation setType:kCATransitionFade];
    [animation setDuration:0.5];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:
                                  kCAMediaTimingFunctionEaseInEaseOut]];
    [[self.window layer] addAnimation:animation forKey:kAnimationKey];
}

i hope this help you...

:)

Sleeping the main thread and showing the splash screen for long time is not a good idea. You can achieve the same thing by following a simple trick. I think in your case service is being called from the very first view controller after hiding splash screen. So you can create a modal view containing the same image like splash screen. And display like following :

SLSDummySplash *dummySplash = [self.storyboard instantiateViewControllerWithIdentifier:@"splashId"];
[self presentViewController:dummySplash animated:NO completion:nil];

When you are done with service calling / long loading event just dismiss the modal view.

Please see here: https://github.com/k06a/LaunchScreenViewController .

Display splash view controller right before the app's first view controller appears. When you are done loading data from web or initializing, dismiss the splash view controller.

Sleeping or performing a selector after a specific time are not the correct approaches because you never know how much to wait depending upon poor internet connectivity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM