0

I have a LaunchImage asset for displaying a launch screen. My problem is after launch image disappearing, the webview of my initial view controller starts to load contents.

So, how to display my launch image until my webview load ends?

My configuration:

enter image description here

enter image description here

Sayakiss
  • 6,878
  • 8
  • 61
  • 107
  • http://stackoverflow.com/questions/5618163/displaying-splash-screen-for-longer-than-default-seconds. check this one. – Quang Hà Feb 20 '17 at 09:21

4 Answers4

2

You can't modify the Launch Screen timer or anything related to "how long it should show". It is not in your control.

1: Add delegate method to your UIWebView webViewDidFinishLoad(_:)

2: Show a Copy of your "Launch Screen" as an UIImageView or a custom UIView. And hide it when the delegate method fires of as finished loading.

Note In apps that run in iOS 8 and later, use the WKWebView class instead of using UIWebView. Additionally, consider setting the WKPreferences property javaScriptEnabled to false if you render files that are not supposed to run JavaScript.

For WKWebView delegate:

webView(_:didFinish:)

  • Thanks. But I got another question with your method: how to convert my launch image resource to a proper image resource which fits different screen size? is it possible to display a launch image resource with an image view? – Sayakiss Feb 20 '17 at 09:27
  • @Sayakiss It is better if you create a custom UIView, add the same image as your launchscreen, and add a UIActivityIndicator to show that the view is loading, otherwise the user might think it has crashed or is stuck. –  Feb 20 '17 at 09:29
  • `add the same image as your launchscreen`: You know, an image resource comes with only 3 images(1x,2x,3x), but there are more than 10 images in my launch image resource, how can I select the proper image for different screen size? – Sayakiss Feb 20 '17 at 09:33
  • @Sayakiss Your 10 launchscreen images have different width/height, but I guess the images is not full size width and height? Maybe you have a background color to fill the background and your image is only centered, then you only need 1x-2x-3x. Thats the only way to do it and the only supported method. Otherwise for a **custom method** you must do calculations on screenwidth and height and programmatically add the correct PNG file depending on your calculated height and width of the screen, i don't recommend this is very ugly code. You should use 1x2x3x as designed if possible. –  Feb 20 '17 at 09:36
  • @Sayakiss Also note my answer, switch to WKWebView , UIWebView has alot of bugs and memory problems, and is very slow compared to WKWebView. –  Feb 20 '17 at 09:38
  • Thanks for your advice, I will check it out. As for `WKWebView`, I have tried `WKWebView` but failed with some issues. I will open another SO post for those issues and then I will comment here to notice you... – Sayakiss Feb 20 '17 at 09:44
  • I just wonder, how could you figure out it's `UIWebView` rather `WKWebView`? I don't mention it's `UIWebView` in my question... – Sayakiss Feb 20 '17 at 09:49
  • @Sayakiss Usually when people write "webview" in the question, they indicate the use of UIWebView, otherwise when using WKWebView, people tend to specifically write it :) Was just a guess. –  Feb 20 '17 at 09:50
0

You do not have control about the launch appearence time. However you can create your own UIImageView covering the screen with the same launch image and remove it once the UIWebView is done loading

giorashc
  • 13,691
  • 3
  • 35
  • 71
  • Thanks. But I got another question with your method: how to convert my launch image resource to a proper image resource which fits different screen size? is it possible to display a launch image resource with an image view? – Sayakiss Feb 20 '17 at 09:25
  • If its an image file or xib you can use it exactly as it is being used in the launch screen. The launch screen is just a view displaying an image. so just mimic the same. Regarding screen sizes if the launch image fits them all then it will fit if you do it manually. – giorashc Feb 20 '17 at 09:31
  • `If it's an image file or xib`: it's a `LaunchImage` resource(see the picture in my question) – Sayakiss Feb 20 '17 at 09:34
0

You should try delegate methods of UIWebView :-

- (void)webViewDidStartLoad:(UIWebView *)webView
- (void)webViewDidFinishLoad:(UIWebView *)webView

And use UIImageView (which contains your launch image), keep showing it till your UIWebView gets loaded completely.

Vineet Singh
  • 4,009
  • 1
  • 28
  • 39
0

If you don't want to create a separate UIImageView "similar" to your launch screen, you can instantiate a view controller as exactly the launch screen and use that.

let sb = UIStoryboard(name: "Launch Screen", bundle: nil) // the storyboard of your launch screen
let vc = sb.instantiateViewController(withIdentifier: "launchScreen") // the storyboard id (remember to set it first!)

// present the vc
navigationController?.present(vc, animated: false, completion: nil)

// when you're done loading
vc.dismiss(animated: false, completion: nil)

However, note that this is not a very good practice, since you should let the user now that the app is actually doing something. Consider showing some kind of loading spinner or similar.