2

I am new in ios swift ,

how can I fetch data from webservice during splash screen time ,

In other words I want to make splash screen still appear until the app finish fetching data.

I tried put NSURLConnection.sendAsynchronousRequest function in appdelegate but splash screen not wait sendAsynchronousRequest to finish

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
Saeed Alasiry
  • 322
  • 5
  • 23

2 Answers2

5

You can create a Splash Screen view controller in your Storyboard and mark it as the initial view controller. Assign a custom view controller to it, such as "SplashViewController.swift". This view controller has usual methods, such as viewDidAppear, where you could have your network connections. After you are done, you can use segues to direct the app to the desired view controller.

Navid Rezaei
  • 1,003
  • 11
  • 22
0

I dont think thats a good idea. For example:

I have a very bad internet connection, and your app shows me the splash screen for seconds. So i normally think that app will crash, or something else. A better approach would be to do that in your MainViewController, and show the User a Spinner/Load Screen or something like that.

A good way to make async requests in swift is to use the (great) extension Alamofire. Especially when youre new to iOS Swift.

https://github.com/Alamofire/Alamofire

Then, in your ViewDidLoad make your request (in this case still as background Thread)

let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {

// load here spinner / load screen 
            
// alamofire request
Alamofire.request(.POST, "http://www.test.com/service", parameters: ["foo": "bar"]).responseJSON { (req, res, json, error) in
  ....
}
aturan23
  • 4,798
  • 4
  • 28
  • 52
derdida
  • 14,784
  • 16
  • 90
  • 139
  • Thank you for your idea , but I want to fetch some data during splash screen. – Saeed Alasiry Jul 31 '15 at 11:58
  • And an the Thread NEVER wait for an AsynchronousRequest, thats why you make it Asychronous (so that the UI not hang until the Request finishes) - this is why your splashscreen will not wait for your response. – derdida Jul 31 '15 at 12:13
  • And please explain why you want to make an Synchronous (in your case) request during Splash Screen. You want to show the User on the first VieController some contents from your service? – derdida Jul 31 '15 at 12:15
  • I want to check some data first and then according to result I want to redirect user to specific view controller – Saeed Alasiry Aug 01 '15 at 08:41
  • And why not do that in your MainViewController? Show the user a loading Screen, and then load (with your response) your next ViewController. I dont see any advantages when youre doing that in your SplashScreen (only seeing a hanging/lagging app) – derdida Aug 01 '15 at 11:11