How to run function whenever the app appears on screen in swift?












0















I have a function which I want to run whenever the app appears on screen.
I tried all these methods:



override func viewWillAppear() {
generateRandomNumber()

}
override func viewDidAppear() {
generateRandomNumber()
}
override func viewDidLoad() {
generateRandomNumber()
}


Although the function runs every time I run app, but if after running app I press home botton and come back to app (didn't terminated the app) then the function does not execute again.



Update: I tried using following code in my ViewController:



override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
}
@objc func applicationDidBecomeActive(notification: NSNotification) {
generateRandomNumber()
}


But it takes about a second to run. Is there a better way?










share|improve this question




















  • 2





    appwillEnterForeground in appdelegate.

    – Gagan_iOS
    Jan 20 at 14:32






  • 1





    Your updated code looks fine. I suspect when you say "it takes about a second to run" that you're measuring at the wrong point. Put a breakpoint in applicationDidBecomeActive, or add a print statement. I suspect it runs much sooner than you think and that you're probably not updating your views to match.

    – Rob Napier
    Jan 20 at 15:26











  • will it be even more faster if I use applicationWillEnterForeground?

    – ocean hancock
    Jan 20 at 15:28








  • 1





    That happens slightly before "active," but it should be nowhere near 1 second. It really has to do with whether you want system alerts, incoming phone calls and the like to trigger it. It's not about performance.

    – Rob Napier
    Jan 20 at 15:40













  • I was trying to just replace applicationDidBecomeActive with applicationWillEnterForeground in the updated code above but it didn't work. can you please show me how to use applicationWillEnterForeground in ViewController.swift?

    – ocean hancock
    Jan 20 at 15:46
















0















I have a function which I want to run whenever the app appears on screen.
I tried all these methods:



override func viewWillAppear() {
generateRandomNumber()

}
override func viewDidAppear() {
generateRandomNumber()
}
override func viewDidLoad() {
generateRandomNumber()
}


Although the function runs every time I run app, but if after running app I press home botton and come back to app (didn't terminated the app) then the function does not execute again.



Update: I tried using following code in my ViewController:



override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
}
@objc func applicationDidBecomeActive(notification: NSNotification) {
generateRandomNumber()
}


But it takes about a second to run. Is there a better way?










share|improve this question




















  • 2





    appwillEnterForeground in appdelegate.

    – Gagan_iOS
    Jan 20 at 14:32






  • 1





    Your updated code looks fine. I suspect when you say "it takes about a second to run" that you're measuring at the wrong point. Put a breakpoint in applicationDidBecomeActive, or add a print statement. I suspect it runs much sooner than you think and that you're probably not updating your views to match.

    – Rob Napier
    Jan 20 at 15:26











  • will it be even more faster if I use applicationWillEnterForeground?

    – ocean hancock
    Jan 20 at 15:28








  • 1





    That happens slightly before "active," but it should be nowhere near 1 second. It really has to do with whether you want system alerts, incoming phone calls and the like to trigger it. It's not about performance.

    – Rob Napier
    Jan 20 at 15:40













  • I was trying to just replace applicationDidBecomeActive with applicationWillEnterForeground in the updated code above but it didn't work. can you please show me how to use applicationWillEnterForeground in ViewController.swift?

    – ocean hancock
    Jan 20 at 15:46














0












0








0


2






I have a function which I want to run whenever the app appears on screen.
I tried all these methods:



override func viewWillAppear() {
generateRandomNumber()

}
override func viewDidAppear() {
generateRandomNumber()
}
override func viewDidLoad() {
generateRandomNumber()
}


Although the function runs every time I run app, but if after running app I press home botton and come back to app (didn't terminated the app) then the function does not execute again.



Update: I tried using following code in my ViewController:



override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
}
@objc func applicationDidBecomeActive(notification: NSNotification) {
generateRandomNumber()
}


But it takes about a second to run. Is there a better way?










share|improve this question
















I have a function which I want to run whenever the app appears on screen.
I tried all these methods:



override func viewWillAppear() {
generateRandomNumber()

}
override func viewDidAppear() {
generateRandomNumber()
}
override func viewDidLoad() {
generateRandomNumber()
}


Although the function runs every time I run app, but if after running app I press home botton and come back to app (didn't terminated the app) then the function does not execute again.



Update: I tried using following code in my ViewController:



override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
}
@objc func applicationDidBecomeActive(notification: NSNotification) {
generateRandomNumber()
}


But it takes about a second to run. Is there a better way?







ios swift uiviewcontroller






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 20 at 16:52









rmaddy

242k27316380




242k27316380










asked Jan 20 at 14:22









ocean hancockocean hancock

607




607








  • 2





    appwillEnterForeground in appdelegate.

    – Gagan_iOS
    Jan 20 at 14:32






  • 1





    Your updated code looks fine. I suspect when you say "it takes about a second to run" that you're measuring at the wrong point. Put a breakpoint in applicationDidBecomeActive, or add a print statement. I suspect it runs much sooner than you think and that you're probably not updating your views to match.

    – Rob Napier
    Jan 20 at 15:26











  • will it be even more faster if I use applicationWillEnterForeground?

    – ocean hancock
    Jan 20 at 15:28








  • 1





    That happens slightly before "active," but it should be nowhere near 1 second. It really has to do with whether you want system alerts, incoming phone calls and the like to trigger it. It's not about performance.

    – Rob Napier
    Jan 20 at 15:40













  • I was trying to just replace applicationDidBecomeActive with applicationWillEnterForeground in the updated code above but it didn't work. can you please show me how to use applicationWillEnterForeground in ViewController.swift?

    – ocean hancock
    Jan 20 at 15:46














  • 2





    appwillEnterForeground in appdelegate.

    – Gagan_iOS
    Jan 20 at 14:32






  • 1





    Your updated code looks fine. I suspect when you say "it takes about a second to run" that you're measuring at the wrong point. Put a breakpoint in applicationDidBecomeActive, or add a print statement. I suspect it runs much sooner than you think and that you're probably not updating your views to match.

    – Rob Napier
    Jan 20 at 15:26











  • will it be even more faster if I use applicationWillEnterForeground?

    – ocean hancock
    Jan 20 at 15:28








  • 1





    That happens slightly before "active," but it should be nowhere near 1 second. It really has to do with whether you want system alerts, incoming phone calls and the like to trigger it. It's not about performance.

    – Rob Napier
    Jan 20 at 15:40













  • I was trying to just replace applicationDidBecomeActive with applicationWillEnterForeground in the updated code above but it didn't work. can you please show me how to use applicationWillEnterForeground in ViewController.swift?

    – ocean hancock
    Jan 20 at 15:46








2




2





appwillEnterForeground in appdelegate.

– Gagan_iOS
Jan 20 at 14:32





appwillEnterForeground in appdelegate.

– Gagan_iOS
Jan 20 at 14:32




1




1





Your updated code looks fine. I suspect when you say "it takes about a second to run" that you're measuring at the wrong point. Put a breakpoint in applicationDidBecomeActive, or add a print statement. I suspect it runs much sooner than you think and that you're probably not updating your views to match.

– Rob Napier
Jan 20 at 15:26





Your updated code looks fine. I suspect when you say "it takes about a second to run" that you're measuring at the wrong point. Put a breakpoint in applicationDidBecomeActive, or add a print statement. I suspect it runs much sooner than you think and that you're probably not updating your views to match.

– Rob Napier
Jan 20 at 15:26













will it be even more faster if I use applicationWillEnterForeground?

– ocean hancock
Jan 20 at 15:28







will it be even more faster if I use applicationWillEnterForeground?

– ocean hancock
Jan 20 at 15:28






1




1





That happens slightly before "active," but it should be nowhere near 1 second. It really has to do with whether you want system alerts, incoming phone calls and the like to trigger it. It's not about performance.

– Rob Napier
Jan 20 at 15:40







That happens slightly before "active," but it should be nowhere near 1 second. It really has to do with whether you want system alerts, incoming phone calls and the like to trigger it. It's not about performance.

– Rob Napier
Jan 20 at 15:40















I was trying to just replace applicationDidBecomeActive with applicationWillEnterForeground in the updated code above but it didn't work. can you please show me how to use applicationWillEnterForeground in ViewController.swift?

– ocean hancock
Jan 20 at 15:46





I was trying to just replace applicationDidBecomeActive with applicationWillEnterForeground in the updated code above but it didn't work. can you please show me how to use applicationWillEnterForeground in ViewController.swift?

– ocean hancock
Jan 20 at 15:46












1 Answer
1






active

oldest

votes


















3














In your application delegate, you want to implement applicationDidBecomeActive. Note that this will also run when things like system alerts are dismissed. If you only want to run something when returning from the background, you want applicationWillEnterForeground. For full details, see "Managing State Transitions" in the UIApplicationDelegate documentation and Strategies for Handling App State Transitions in the App Programming Guide.



Your approach looks basically right; in order to use willEnterForeground rather than didBecomeActive, it's just a small change:



override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self,
selector: #selector(applicationWillEnterForeground),
name: UIApplication.willEnterForegroundNotification,
object: nil)
}

@objc func applicationWillEnterForeground(notification: Notification) {
generateRandomNumber()
}


The only thing to be careful with here is that this observer continues to be in force as long as the view controller exists, even if it's not currently on the screen (the most common case of that is when it's presented other view controllers). In some cases that's a problem, and you should call addObserver in viewWillAppear and removeObserver in viewDidDisappear instead of using viewDidLoad.






share|improve this answer





















  • 1





    I am new to swift and having a little difficulty understanding apple documentation. Can you please tell me when should I right my generateRandomNumber() function. generateRandomNumber() is located in ViewController.swift

    – ocean hancock
    Jan 20 at 14:36













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277406%2fhow-to-run-function-whenever-the-app-appears-on-screen-in-swift%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














In your application delegate, you want to implement applicationDidBecomeActive. Note that this will also run when things like system alerts are dismissed. If you only want to run something when returning from the background, you want applicationWillEnterForeground. For full details, see "Managing State Transitions" in the UIApplicationDelegate documentation and Strategies for Handling App State Transitions in the App Programming Guide.



Your approach looks basically right; in order to use willEnterForeground rather than didBecomeActive, it's just a small change:



override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self,
selector: #selector(applicationWillEnterForeground),
name: UIApplication.willEnterForegroundNotification,
object: nil)
}

@objc func applicationWillEnterForeground(notification: Notification) {
generateRandomNumber()
}


The only thing to be careful with here is that this observer continues to be in force as long as the view controller exists, even if it's not currently on the screen (the most common case of that is when it's presented other view controllers). In some cases that's a problem, and you should call addObserver in viewWillAppear and removeObserver in viewDidDisappear instead of using viewDidLoad.






share|improve this answer





















  • 1





    I am new to swift and having a little difficulty understanding apple documentation. Can you please tell me when should I right my generateRandomNumber() function. generateRandomNumber() is located in ViewController.swift

    – ocean hancock
    Jan 20 at 14:36


















3














In your application delegate, you want to implement applicationDidBecomeActive. Note that this will also run when things like system alerts are dismissed. If you only want to run something when returning from the background, you want applicationWillEnterForeground. For full details, see "Managing State Transitions" in the UIApplicationDelegate documentation and Strategies for Handling App State Transitions in the App Programming Guide.



Your approach looks basically right; in order to use willEnterForeground rather than didBecomeActive, it's just a small change:



override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self,
selector: #selector(applicationWillEnterForeground),
name: UIApplication.willEnterForegroundNotification,
object: nil)
}

@objc func applicationWillEnterForeground(notification: Notification) {
generateRandomNumber()
}


The only thing to be careful with here is that this observer continues to be in force as long as the view controller exists, even if it's not currently on the screen (the most common case of that is when it's presented other view controllers). In some cases that's a problem, and you should call addObserver in viewWillAppear and removeObserver in viewDidDisappear instead of using viewDidLoad.






share|improve this answer





















  • 1





    I am new to swift and having a little difficulty understanding apple documentation. Can you please tell me when should I right my generateRandomNumber() function. generateRandomNumber() is located in ViewController.swift

    – ocean hancock
    Jan 20 at 14:36
















3












3








3







In your application delegate, you want to implement applicationDidBecomeActive. Note that this will also run when things like system alerts are dismissed. If you only want to run something when returning from the background, you want applicationWillEnterForeground. For full details, see "Managing State Transitions" in the UIApplicationDelegate documentation and Strategies for Handling App State Transitions in the App Programming Guide.



Your approach looks basically right; in order to use willEnterForeground rather than didBecomeActive, it's just a small change:



override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self,
selector: #selector(applicationWillEnterForeground),
name: UIApplication.willEnterForegroundNotification,
object: nil)
}

@objc func applicationWillEnterForeground(notification: Notification) {
generateRandomNumber()
}


The only thing to be careful with here is that this observer continues to be in force as long as the view controller exists, even if it's not currently on the screen (the most common case of that is when it's presented other view controllers). In some cases that's a problem, and you should call addObserver in viewWillAppear and removeObserver in viewDidDisappear instead of using viewDidLoad.






share|improve this answer















In your application delegate, you want to implement applicationDidBecomeActive. Note that this will also run when things like system alerts are dismissed. If you only want to run something when returning from the background, you want applicationWillEnterForeground. For full details, see "Managing State Transitions" in the UIApplicationDelegate documentation and Strategies for Handling App State Transitions in the App Programming Guide.



Your approach looks basically right; in order to use willEnterForeground rather than didBecomeActive, it's just a small change:



override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self,
selector: #selector(applicationWillEnterForeground),
name: UIApplication.willEnterForegroundNotification,
object: nil)
}

@objc func applicationWillEnterForeground(notification: Notification) {
generateRandomNumber()
}


The only thing to be careful with here is that this observer continues to be in force as long as the view controller exists, even if it's not currently on the screen (the most common case of that is when it's presented other view controllers). In some cases that's a problem, and you should call addObserver in viewWillAppear and removeObserver in viewDidDisappear instead of using viewDidLoad.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 20 at 16:25

























answered Jan 20 at 14:25









Rob NapierRob Napier

202k28298424




202k28298424








  • 1





    I am new to swift and having a little difficulty understanding apple documentation. Can you please tell me when should I right my generateRandomNumber() function. generateRandomNumber() is located in ViewController.swift

    – ocean hancock
    Jan 20 at 14:36
















  • 1





    I am new to swift and having a little difficulty understanding apple documentation. Can you please tell me when should I right my generateRandomNumber() function. generateRandomNumber() is located in ViewController.swift

    – ocean hancock
    Jan 20 at 14:36










1




1





I am new to swift and having a little difficulty understanding apple documentation. Can you please tell me when should I right my generateRandomNumber() function. generateRandomNumber() is located in ViewController.swift

– ocean hancock
Jan 20 at 14:36







I am new to swift and having a little difficulty understanding apple documentation. Can you please tell me when should I right my generateRandomNumber() function. generateRandomNumber() is located in ViewController.swift

– ocean hancock
Jan 20 at 14:36






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277406%2fhow-to-run-function-whenever-the-app-appears-on-screen-in-swift%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Liquibase includeAll doesn't find base path

How to use setInterval in EJS file?

Petrus Granier-Deferre