All the time if I open app I need to show Launch Screen with appname only, but if there is no internet then i need to show launch screen with extra image and button like this app launch screenImage. How is that possible?
For internet checking I am using Reachability
if I write below code in loginscreen then alert coming in loginscreen but how to show like above image in launch screen:
tried code in LoginScreen:
import UIKit
import SystemConfiguration
class LoginViewController: UIViewController, UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
internetCheck()
}
func internetCheck(){
if Reachability.isConnectedToNetwork(){
Constants.printLog(message: "Internet Connection Available!")
}else{
let alert = UIAlertController(title: "Alert", message: "NO internet", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
return
}
}
public class Reachability {
class func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
}
}
var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
return false
}
// Working for Cellular and WIFI
let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
let ret = (isReachable && !needsConnection)
return ret
}
}
}
Where shall I write code for launch screen? Is it possible to add extra image and button in launch screen if there is no internet or shall I create separate view control to show like above mentioned image. Please suggest and explain me the concept.
There is a trick for that kind of design. Obviously you CAN add views in your launchscreen BUT you CAN'T bind some codes to those views (i.e. UIButton, and whatnot).
So let's dive in to the trick.
Trick is: you can add a controller, right after the launch screen, meaning that controller is exactly the very first screen of your app. And then the UI design for that controller (let's name it RootViewController) will be EXACTLY the same with the design of your launch screen.
That will give the users an impression that they're still on the launch screen. And from that RootViewController, do your Reachability thing.
Hope this helps!