Function that detects an app uninstall/reinstall - swift

Been looking around to find a way to detect when an app is uninstalling/reinstalling. The thing is, I do not use NSUserDefaults, I use SwiftKeychainWrapper.
I need to clear that user's keychain when app uninstalls.
didFinishLaunchingWithOptions seems to call when app loads so no use there. Is there a way to detect a reinstall? I need to call this:
return KeychainWrapper.standard.removeObject(forKey: "myKey") // only when/if app is unsinstalled/reinstalling

Presumably you're using Keychain because you need to store sensitive information? If so then you could just store a boolean in UserDefaults and check if that exists. For example:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let freshInstall = !UserDefaults.standard.bool(forKey: "alreadyInstalled")
if freshInstall {
// delete your item from keychain
UserDefaults.standard.set(true, forKey: "alreadyInstalled")
}
return true
}
This way you still have the security of the Keychain, but the behaviour of UserDefaults on uninstall/reinstall.

for others searching for the way to clear the Keychain in the "// delete your item" part of the answer.....
Swift 3
let _ = KeychainWrapper.standard.removeAllKeys()

Related

Background Fetch working every Second in Swift 4.2

Behold. I am trying to create an IOS app with swift 4.2, which, according to my client, has to sound an alarm, but it can only be stopped with a QR code. Therefore, what I will need will be a timer that works in the background, checking the time every second, because something else, the user could stop it, without the QR code. The only thing I've found for that is the Background Fetch, but can it work with 1 second checks every time?
Does anyone have a better idea?
Cheers
This is my source code for now
App delegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.setMinimumBackgroundFetchInterval(2.0)
}
//ini----------------------------background task
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
// fetch data from internet now
let time:TimerNow = TimerNow();
let hours = preferences.string(forKey: "hours");
let minutes = preferences.string(forKey: "minutes");
if((hours == time.getHours())&&(minutes == time.getMinutes())){
lem.onPlayCommand();
}
completionHandler(.newData)
}
//fin ---------------------------background task
I think you should use local notifications instead. It will be fired when you want to notify about the elapsed time. You could cancel it whenever and with any condition you want.
IMO timer and background fetching is not suitable for that situation.

How to detect if an app was opened by a notification action in the ViewController

I am working on a notification app and was wondering if it is possible to detect if the app is opened from a notification action in ViewController.Swift instead of the AppDelegate.swift. How can I do this?
Krish, Whenever the app is launched the AppDelegate gets called(if the default Main class is not modified) and in the AppDelegate class you can check for the launch option in launch option delegate whether its opened through remote notification. This is the first action where you will catch the app opening event.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let remoteNotif = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary
if remoteNotif != nil {
let notifName = remoteNotif?["aps"] as! String
print("Notification: \(notifName )")
}
else {
print("Not remote")
}
}
Here is a complete and easy answer to that
After you handle the event on AppDelegate you can use an observer to let your ViewController of that event.
Normally you should redirect the user to a specific view, depending on the notification payload.

Firebase Notifications and Firebase Database features do not work at the same time in my app

I'm a new user to stack overflow but have been using the website anonymously for several months now. I've integrated Firebase into my iOS 10 (Swift 3) app, everything works fine but if I use Firebase Database, then I will not be able to use Firebase Notifications (meaning I won't receive my remote notifications that I have sent using Firebase).
Here is my code below:
override init() {
FIRApp.configure()
FIRDatabase.database().persistenceEnabled = true
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let types : UIUserNotificationType = [UIUserNotificationType.alert, UIUserNotificationType.badge, UIUserNotificationType.sound]
let settings = UIUserNotificationSettings(types: types, categories: nil)
application.registerForRemoteNotifications()
application.registerUserNotificationSettings(settings)
return true
}
Yes you can use both. It's documented pretty well on how to setup FIRDatabase: https://firebase.google.com/docs/database/ios/start
I've just built an app with both Messages and Notifications. Use CocaPods to manage and install all your dependancies.

Swift. Remember a facebook login

Okay so I have implemented a facebook login button:
loginButton.readPermissions = ["public_profile", "email", "user_friends"]
loginButton.center = (self.view?.center)!
loginButton.delegate = self
self.view?.addSubview(loginButton)
but every time I shut down the app completely and turn it on again I need to rejoin, so my question is how an I make the app remember that I've already logged in once?
try using this
Put this code in your appDelegate
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if((FBSDKAccessToken.currentAccessToken()) != nil){
//user is sign in
//Put here something what you want to do if user is sign in
}
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
save the email or facebook token which you get from the facebook response in your Userdefaults or coredata..
then check for the value first in the login screen and direct your app..

Swift: How to use handoff in the simplest way possible?

I am trying to use handoff to make an iphone app do something when the user does something in the watchkit app. In the watch app (in InterfaceController.swift), I have this code that gets run when the user taps a button:
self.updateUserActivity("com.test", userInfo: ["one":"two"], webpageURL: nil)
Then in the AppDelegate.swift file of the phone app, I have this code:
func application(application: UIApplication!,
continueUserActivity userActivity: NSUserActivity!,
restorationHandler: (([AnyObject]!) -> Void)!)
-> Bool {
let userInfo = userActivity.userInfo
println("testing: \(userInfo)")
return true
}
In theory this should print the userInfo (in this case ["one":"two"]) to the console, but I think I'm missing something here.