Saeed Alasiry
Saeed Alasiry

Reputation: 342

consuming web service during splash screen in swift

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

Upvotes: 2

Views: 5373

Answers (2)

derdida
derdida

Reputation: 14904

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

Upvotes: 0

Navid Rezaei
Navid Rezaei

Reputation: 1041

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.

Upvotes: 5

Related Questions