Local notification pull into background function triggering - swift

when I terminate my app into twice tap on home button then how can I call my function in background service on swift 4.
because I work on local push notification , after API calling then getting data and that's data put into notification.

I am not entirely sure this is what you mean, but if you want to call a function/do something right before your app enters the background(what happens when someone double clicks the home button) you can use the default applicationWillResignActive(_:) or applicationDidEnterBackground(_:) function in the AppDelegate class. See the apple docs for more information about app states and functions.
If you want to execute code when the app terminates, use applicationWillTerminate(_:).
So in AppDelegate.swift(I deliberately left the original Xcode default comment in for clarification):
class AppDelegate: UIResponder, UIApplicationDelegate {
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
yourGoToBackgroundFunction()
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
yourSaveDataFunction()
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
yourSaveDataFunction()
}
}

Related

Quit code for watchOS

In Xcode there is a place to set actions when the interface controller is dismissed in:
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
However, my watchOS app needs to perform an action when the app quits completely i.e. the home screen is visible. Not when the watch is simply lowered and the screen is dimmed because the app is still running as a workout app and is performing actions.
Is there a way to do this?
In ExtensionDelegate, you'll find the method applicationWillResignActive.
Sent when the application is about to move from active to inactive
state. This can occur for certain types of temporary interruptions
(such as an incoming phone call or SMS message) or when the user quits
the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, etc.
You can use this method to do what you need.

How and where do I execute a function after the user closes an app?

Can I include a function that runs once if the user closes the app? If so, where should I put that function because I don't know which view controller the user will be closing the app from. What's the solution?
In AppDelegate.swift
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

Stripe Integrating Issues

I installed 'Stripe' in my project. However, when I follow the directions on Stripe's website to integrate and 'import Stripe' in my AppDelegate, it says 'no module' and doesn't work/comes as an error. Am I doing something wrong?
Stripe's Library is in Obj-C you will need to add a Bridging Header to be able to import it to a swift project
Extensive guide :- https://www.appcoda.com/ios-stripe-payment-integration/
Also : - https://stackoverflow.com/a/30635582/6297658 might be helpful
Try this:
update Podfile
use_frameworks!
target '**{YOUR_PROJECT_NAME}**' do
pod 'Stripe'
end
delete Podfile.lock and Pods. After install pod
your sample code AppDelegate.swift
import UIKit
import Stripe
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
STPPaymentConfiguration.sharedConfiguration().publishableKey = "pk_test_6pRNASCoBOKtIshFeQd4XMUh"
// do any other necessary launch configuration
return true
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}

Swift: Run a code once a while whenever app is closed

I've an app that has to run a code once a while whenever app isn't running. Is this possible? if yes Any help will be appreciated
Your app can run in the background several minutes.
I would suggest to override one of these methods to achieve your goal:
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

Application going in background -- Home button tapped when application is running

What is the sequence of methods that gets called when we tap on the home button on iPhone, i.e. when an application is going in background.
Basically, I want to make a server call before my app goes in background. I am writing my server call code in applicationWillResignActive:, but sometimes it executes and sometimes it doesn't.
I need a solution for this.
From the app delegate file itself:
- (void)applicationDidEnterBackground:(UIApplication *)application {
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
Superclass implementation saves changes in the application's managed object context before the application terminates.
*/
[super applicationDidEnterBackground:application];
}
Try using this method.....