xcode 如何在显示启动画面时从 Web 服务加载数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12722111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load data from web-service while displaying splash screen?
提问by Saurabh
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.
我想在我的应用程序启动时从网络加载一些数据。我已经设置了启动画面,并且在 didFinishLaunchingWithOptions 中我已经设置了 sleep(10)。现在我希望在这个时间间隔内,我的应用程序调用 Web 服务并加载数据,但我无法做到这一点。请帮助我或提出任何建议。
回答by Pieter Jongsma
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.
我建议您只显示启动画面,开始加载数据并在所有数据加载后隐藏启动画面。
回答by mayuur
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.
相反,设置类似于不断运行 UIScrollView 的更新页面,只有当您的数据被获取时才会消失。
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.
使用 Web 服务的委托,通过它您将调用 AppDelegate 中的函数以在获取数据时删除加载器视图并添加主页。
Something like,
就像是,
this is just an example...
这只是一个例子...
- (void) webserviceDidFinishLoading //write in appdelegate.m
{
[self.activityIndicatorView removeFromSuperView];
self.window.rootController = self.homeViewController;
}
Hope this helps! :)
希望这可以帮助!:)
回答by Paras Joshi
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...
我希望这能帮助你...
:)
:)
回答by Saleh Masum
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.
完成服务调用/长加载事件后,只需关闭模态视图。
回答by K.K
Please see here: https://github.com/k06a/LaunchScreenViewController.
请参阅此处: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.
在应用程序的第一个视图控制器出现之前显示初始视图控制器。从 Web 加载数据或初始化完成后,关闭启动视图控制器。
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.
在特定时间后睡觉或执行选择器不是正确的方法,因为您永远不知道根据糟糕的互联网连接需要等待多长时间。