In my iPhone app need to display a splash screen (displays company logo and some info) for 2-3 seconds before load the first page.
Also the app needs to decide which page should load as the first page in here (according the 'initial setup completion' level by the user).
I am using 'Swift' as the programing language and 'Universal StoryBoard' to design interfaces...
I have seleted the Main.storyboard as the Launch Screen File. In the ViewController class have implemented following logic
override func viewDidAppear(animated: Bool) {
NSLog("Before sleep...")
sleep(2)
NSLog("After sleep...")
self.controllNavigation()
}
func controllNavigation() -> Void {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var nextViewController
if (Condition1)
{
nextViewController = storyBoard.instantiateViewControllerWithIdentifier("MainMenu") as! MainMenuViewController
}
else
{
nextViewController = storyBoard.instantiateViewControllerWithIdentifier("UserSetup") as! UserSetupViewController
}
self.presentViewController(nextViewController, animated: true, completion: nil)
}
All works ok but While waiting with sleep(2), refresh page sort of a thing happens. I am not sure if this is the best way to do. Like to hear ideas. Thanks
Use the delay in app delegate instead of viewController's viewDidAppear.
Use as:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
NSLog("Before Sleep");
sleep(2);
NSLog("After sleep");
return true
}
Doing this will allow your splash screen to stay till 2 seconds and then you can use following in your view controller:
override func viewDidAppear(animated: Bool) {
self.controllNavigation()
}
Hope this helps :)
I came across similar answers which mentioned sleep(), however it doesn't seem appropriate to use as it blocks the main thread.
I came up with this solution if you are using the default LaunchScreen.storyboard in Xcode 7.2:
func showLaunchScreen() {
let launchScreen = UIStoryboard(name: "LaunchScreen", bundle: nil)
let launchView = launchScreen.instantiateInitialViewController()
self.view.addSubview(launchView!.view)
let timeDelay = dispatch_time(DISPATCH_TIME_NOW, Int64(2 * Double(NSEC_PER_SEC)))
dispatch_after(timeDelay, dispatch_get_main_queue()) {
UIView.animateWithDuration(0.5, animations: { _ in
launchView?.view.alpha = 0.0
}) { _ in
launchView!.view.removeFromSuperview()
}
}
This will show the launch screen for another 2 seconds, then fade it out over 0.5 seconds. You can call you other function to load the 'default' VC in the completion handler.
I have solved this issue with this:
On my project I set the desired storyboard of splash (in my case Splash.storyboard) as Default Screen -> Targets/ (your target)/ Info/ Launch Screen Interface File Base Name.
Field on Project Settings
When you've done that, you can insert your logic on method "func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {} " placed on AppDelegate.swift .
If you want to make clean code, you can insert all your navigation related methods on another Class, and call them from AppDelegate.swift .
With this solution, the splash is visible only the required time, and when it finish, you can navigate to the screen you need.
Swift 4 Update 100% working
Just write one line of code
Thread.sleep(forTimeInterval: 3.0)
in the method of didfinishLauching.... in appdelegate class.
Example
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Thread.sleep(forTimeInterval: 3.0)
// Override point for customization after application launch.
return true
}
Related
I'm implementing 3D touch quick actions in my app and I have the following problem:
When the app is already running and so the quick action is initiated through perform Action For Shortcut Item, it works perfectly fine. However, when the app is killed and then launched through a quick action (so didFinishLaunchingWithOptions) it does not take me to the desired view controller, but rather to the home screen of the app.
Here is my code:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//... other stuff I'm doing
if let shortcutItem = launchOptions?[UIApplication.LaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {
shortcutItemToProcess = shortcutItem
}
return true
NOTE: I've read previous SO answers where they said that I need to return false in didFinishLaunchingWithOptions when the app was launched through a quick action, so that performAction won't get called. I need to always return true however in my didFinishLaunching method because of the other things I'm handling there. I tried however to return false just to see if that causes the problem and the app still behaved the same way.
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: #escaping (Bool) -> Void) {
shortcutItemToProcess = shortcutItem
}
Here is how I present the view controller:
func applicationDidBecomeActive(_ application: UIApplication) {
if let shortcutItem = shortcutItemToProcess {
if shortcutItem.type == "com.myName.MyApp.myQuickAction" {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myViewController = storyboard.instantiateViewController(withIdentifier: "myViewController") as! MyViewController
if let navVC = window?.rootViewController as! UINavigationController? {
navVC.pushViewController(myViewController, animated: true)
}
}
So this works fine when app is already running, but it lands me on the home page of my app when the app is killed. What am I doing wrong and how can I solve this?
I have put an image in imageView in LaunchStoreyboard. How can I delay the time of image programmatically?
Here is the Launch Screen Guideline from Apple.
Here is code for Launch Screen View controller:
import UIKit
class LaunshViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.delay(0.4)
}
func delay(_ delay:Double, closure:#escaping ()->()) {
let when = DispatchTime.now() + delay
DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
}
}
As of today there is no predefine method from Apple to hold launch screen. Here are some Approaches which are not optimum but works
Approach #1
Create a Separate ViewController which has Launch logo & create a timer or perform some operation (Like Database/Loads some essential network call) depends on your app type this way you can ready with data before hand & hold the launch screen as well :)
Approach #2 Not Optimum
Use Sleep code which holds up the app for a while.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Thread.sleep(forTimeInterval: 3.0)
// Override point for customization after application launch.
return true
}
Would not recommending setting the entire application in a waiting state.
If the application needs to do more work before finishing the watchdog could kill the application for taking too long time to start up.
Instead you could do something like this to delay the launch screen.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = UIStoryboard(name: "LaunchScreen", bundle: nil).instantiateInitialViewController()
window?.makeKeyAndVisible()
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
}
return true
}
Swift 4.x
It is Not a good practice to put your application to sleep!
Booting your App should be as fast as possible, so the Launch screen delay is something you do not want to use.
But, instead of sleeping you can run a loop during which the receiver processes data from all attached input sources:
This will prolong the launch-screen's visibility time.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
RunLoop.current.run(until: NSDate(timeIntervalSinceNow:1) as Date)
return true
}
Swift 5.x, iOS 13.x.x
Modifying the following function in the AppDelegate class does not work in Swift 5.x/iOS 13.x.x.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
Instead, you will have to modify the scene function in SceneDelegate class as following. It will delay the LaunchSceen for 3 seconds.
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
window?.rootViewController = UIStoryboard(name: "LaunchScreen", bundle: nil).instantiateInitialViewController()
window?.makeKeyAndVisible()
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
}
guard let _ = (scene as? UIWindowScene) else { return }
}
The window variable should already be there in SceneDelegate class like the following.
var window: UIWindow?
Definitely your app should not be put to sleep as it may be killed by the OS for being unresponsive for so long.
If you're using a static image for your launch screen, what works for me is to use the image in the LaunchScreen.storyboard, and then when your main controller launches, modally present a VC with the same image as the background in the ViewDidAppear of your main controller (with animated set to false).
You can then use your logic to know when to dismiss the launch screen (dismiss method in the VC with animated set to false).
The transition from the actual LaunchScreen to my VC presenting the same screen looks to me imperceptible.
PS: the ViewDidAppear method might be called more than once, in which case you need to use logic to not present the VC with the launch screen a second time.
Create a ViewController and use NSTimer to detect the delay time. and when the timer ends push the first UIViewcontroller.
In ViewDidLoad method..
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(fireMethod) userInfo:nil repeats:NO];
-(void)fireMethod
{
// push view controller here..
}
Put one line of code in AppDelegate Class;
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
Thread.sleep(forTimeInterval: 3.0)
return true
}
SwiftUI
For SwiftUI, you can put a very similar code to the accepted answer into ContentView.onAppearing:
struct ContentView: View {
var body: some View {
Text("Hello")
.onAppear {
Thread.sleep(forTimeInterval: 3.0)
}
}
}
Putting a thread to sleep is not a good idea.
I would suggest you go to SceneDelegate's "willConnectTo" function and paste this piece of code and you are good to go.
window?.rootViewController = UIStoryboard(name: "LaunchScreen", bundle: nil).instantiateInitialViewController()
window?.makeKeyAndVisible()
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
}
guard let _ = (scene as? UIWindowScene) else { return }
I have two views
ViewController-> Main view
LoginVC -> LogIn View
My initial view is ViewController which contains buttons and some text.
What i want to achive
Perform a segue that will transfer the view to Login if the user is not yet log in.
What i have done
Inside my ViewController I did a check if USER_ID is nil then if its a nil then i will perform a segue.
override func viewWillAppear(animated: Bool) {
if Globals.USER_ID == nil{
self.performSegueWithIdentifier("goto_login", sender: nil)
// dispatch_async(dispatch_get_main_queue(), {
// self.performSegueWithIdentifier("goto_login", sender: nil)
// })
}
}
What is my problem
My Problem is whether I use viewWillAppear or viewDidLoad or viewDidAppear to transfer view from ViewController to LoginVC.ViewController will be visible in a single second before the the login screen appears and i want to get rid of it.Can someone help me solve this issue.
I have same functionality in my App. I achieved it by checking for userID in AppDelegate.swift inside function func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
So, delete the segue and your modified code will look like as follows:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
if Globals.USER_ID == nil{
let loginScreen = storyBoard.instantiateViewControllerWithIdentifier("LoginVC")
self.window?.rootViewController = loginScreen
}else{
let mainScreen = storyBoard.instantiateViewControllerWithIdentifier("ViewController")
self.window?.rootViewController = mainScreen
}
return true
}
This way you will not need any of the method like viewDidLoad(), viewDidAppear() or viewWillAppear()to be used for this purpose.
Just as an addition to this answer - Use the instance of AppDelegate again after logout happens. For example you should do following in logout method:
func logout() {
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
Globals.USER_ID = nil
appDelegate.window?.rootViewController = loginScreen
}
}
This way you can clear the userID and navigate back to login screen after the user logouts.
I'm implementing some 3D touch quick actions for my iOS 9 app in swift, and I have a curious issue. When my app is in the background and I launch with the quick action, everything goes as planned. When my app is totally dead (i.e. I killed it from the multitasking menu), and I launch with the quick action, the app crashes. I'm having trouble debugging this as once I kill the app, the debug session in Xcode gets detached. Is there a way for me to connect to the app to debug like normal, or is there something in my code that would be causing it? Thanks in advance.
Code:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
var launchedFromShortCut = false
//Check for ShortCutItem
if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem
{
launchedFromShortCut = true
self.handleShortCutItem(shortcutItem)
}
return !launchedFromShortCut
}
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void)
{
self.handleShortCutItem(shortcutItem)
}
func handleShortCutItem(shortcutItem: UIApplicationShortcutItem)
{
//Get type string from shortcutItem
if let shortcutType = ShortcutType.init(rawValue: shortcutItem.type)
{
//Get root navigation viewcontroller and its first controller
let rootNavigationViewController = window!.rootViewController as? UINavigationController
if let rootViewController = rootNavigationViewController?.viewControllers.first as! LaunchViewController?
{
//Pop to root view controller so that approperiete segue can be performed
rootNavigationViewController?.popToRootViewControllerAnimated(false)
switch shortcutType
{
case .Compose:
rootViewController.shouldCompose()
break
}
}
}
}
Thanks!
In Xcode, open Product -> Schemes -> Edit Schemes
In your Run Scheme, change the Launch setting to 'Wait for executable to be launched'
Now, if you turn on debugging and run your app, Xcode will wait for you to launch your app from the home screen so you are able to test launching it using a 3D Touch Shortcut Item.
I finally got this working. Here's what my AppDelegate.swift file ended up as;
class AppDelegate: UIResponder, UIApplicationDelegate {
// Properties
var window: UIWindow?
var launchedShortcutItem: UIApplicationShortcutItem?
func applicationDidBecomeActive(application: UIApplication) {
guard let shortcut = launchedShortcutItem else { return }
handleShortcut(shortcut)
launchedShortcutItem = nil
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
var shouldPerformAdditionalDelegateHandling = true
// If a shortcut was launched, display its information and take the appropriate action
if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem {
launchedShortcutItem = shortcutItem
// This will block "performActionForShortcutItem:completionHandler" from being called.
shouldPerformAdditionalDelegateHandling = false
}
return shouldPerformAdditionalDelegateHandling
}
func handleShortcut( shortcutItem:UIApplicationShortcutItem ) -> Bool {
// Construct an alert using the details of the shortcut used to open the application.
let alertController = UIAlertController(title: "Shortcut Handled", message: "\"\(shortcutItem.localizedTitle)\"", preferredStyle: .Alert)
let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(okAction)
// Display an alert indicating the shortcut selected from the home screen.
window!.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
return handled
}
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
completionHandler(handleShortcut(shortcutItem))
}
Much of this was taken from Apple's sample code for UIApplicationShortcuts, and while I'm having my app launch an alert to prove that it is recognizing the proper shortcut was chosen, this could be adapted to your code to pop the view controller.
I think the func applicationDidBecomeActive was the critical part that I was missing, and removing the self.handleShortCut(shortcutItem) from didFinishLaunchingWithOptions (otherwise it was calling handleShortCut twice, it seemed).
For Swift 4.2
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
var isLaunchedFromQuickAction = false
if let shortcutItem = launchOptions?[UIApplication.LaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {
isLaunchedFromQuickAction = true
handleQuickAction(shortcutItem: shortcutItem)
}
return isLaunchedFromQuickAction
}
I tried all the above, and it didn't solve the problem
than I tried handling the shortcut after delay in handleShortcut method:
self.performSelector("action1", withObject: self, afterDelay: 0.5)
and added a method for every action, and it worked like a charm
Replace your didfinishlaunching method with this one.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
if let shortcutItem =
launchOptions?[UIApplicationLaunchOptionsShortcutItemKey]
as? UIApplicationShortcutItem {
handleShortcut(shortcutItem)
return false
}
return true
}
XCode 11.6, Swift 5
We can attach a process at runtime. XCode will wait until the process is started and will get attached to it when an App is launched manually.
XCode -> Debug -> Attach to process by PID or Name -> ("Enter the name of an app in the pop-up")
Directions:
Make sure an application is freshly installed on a device or a simulator.
Kill the application.
In XCode attach the name of the process as mentioned above.
Open the application through the desired shortcut
P.S: If you are using SceneDelegate a shortcutItem can be found in
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
switch connectionOptions.shortcutItem?.localizedTitle {
case "Search":
break
case "DoSomething":
break
default:
break
}
}
Happy Debugging :)
I'm working on an iOS app using Xcode 6 and Swift. I'm working with storyboard, so I don't have to instantiate a NavigationController in AppDelegate.
But in my applicationDidFinishLaunching, I instantiate a controller to control Philips Hue
var window: UIWindow?
var phController:PHController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.phController = PHController()
return true
}
In this PHController I have a heartbeat function, which is checking all 10 seconds if the connection to the Philips Hue Bridge is still alive.
If not (and this could happen all the time, INDEPENDENT from where the user is currently in the app), I would like to pop a view controller (SearchForNewBridgeViewController).
The question is:
How can I pop/present modally a ViewController from PHController class instantiated when the app did finish launching?
My idea is to instantiate the PHController with the navigationController:
self.phController = PHController(self.navigationController)
But my project is as I mentioned Storyboard based, so I don't have a navigationConntroller in my AppDelegate
Ok I found a solution :)
Solution:
Instantiate my controller and give AppDelegate as argument:
var window: UIWindow?
var phController:PHController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.phController = PHController(delegate:self)
return true
}
Initializer of PHController:
var delegate:AppDelegate?
init(delegate:AppDelegate) {
self.delegate = delegate
...
}
Present SearchForNewBridgeViewController modally from PHController:
func showSearchForNewBridge() {
let storyboard = UIStoryboard(name: "Main", bundle: nil);
var searchForNewBridgeViewController = storyboard.instantiateViewControllerWithIdentifier("searchForNewBridge") as SearchForNewBridgeViewController
var navigationController = self.delegate!.window!.rootViewController as UINavigationController
navigationController.presentViewController(searchForNewBridgeViewController, animated: true, completion: nil)
}
It's working perfectly for me