I'm implementing the app restoration features.
But I found that viewDidLoad is called before didFinishLaunchingWithOptions gets called.
And I have to read some data from database here.
So it always throws exception on try! Realm().
Anyway, here's how I do it for now.
var realmConfigured = false
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
configureRealm()
}
func application(_ application: UIApplication, shouldRestoreApplicationState coder: NSCoder) -> Bool {
print("restoring")
configureRealm()
return true
}
func configureRealm(){
guard !realmConfigured else { return }
realmConfigured = true
let config = Realm.Configuration(...)
Realm.Configuration.defaultConfiguration = config
}
And it works fine so far.
Is there a better way to do this?
Related
Currently, this is my code-snippet, in handling tap action, of local notification.
The following code will launch a UIViewController, when user taps on the local notification.
import UIKit
extension UIApplication {
//
// https://stackoverflow.com/a/58031897/72437
//
var keyWindow: UIWindow? {
// Get connected scenes
UIApplication
.shared
.connectedScenes
.compactMap { $0 as? UIWindowScene }
.flatMap { $0.windows }
.first { $0.isKeyWindow }
}
}
#main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
return true
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
}
}
extension AppDelegate: UNUserNotificationCenterDelegate {
// This method is called when user clicked on the notification
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void)
{
let newViewController = NewViewController.instanceFromNib()
newViewController.postInit(response.notification.request.identifier)
UIApplication.shared.keyWindow?.rootViewController?.present(newViewController, animated: false, completion: nil)
completionHandler()
}
}
What I am puzzling is, should I install UNUserNotificationCenterDelegate during willFinishLaunchingWithOptions, or didFinishLaunchingWithOptions?
Install during willFinishLaunchingWithOptions
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
return true
}
Install during didFinishLaunchingWithOptions
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
return true
}
Based on https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate
You must assign your delegate object to the UNUserNotificationCenter
object before your app finishes launching. For example, in an iOS app,
you must assign it in the
application(:willFinishLaunchingWithOptions:) or
application(:didFinishLaunchingWithOptions:) method of your app
delegate. Assigning a delegate after the system calls these methods
might cause you to miss incoming notifications.
It seems like there is no difference between willFinishLaunchingWithOptions and didFinishLaunchingWithOptions
I was wondering, do you find any differences, between willFinishLaunchingWithOptions and didFinishLaunchingWithOptions, as far as UNUserNotificationCenterDelegate installation is concerned?
Thanks.
code not working in did enter in background method.
func applicationDidEnterBackground(_ application: UIApplication) {
UIScreen.main.brightness = CGFloat(0.5)
}
I tried to do this in the applicationWillResignActive and seems to achieve what you are looking for. Not sure if this is recommended behaviour though.
Here are my methods from AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIScreen.main.brightness = 1.0;
return true
}
func applicationWillResignActive(_ application: UIApplication) {
UIScreen.main.brightness = 0.5
}
I want to open my iOS app from and URL but when I put NSURL into fund application print() doesn't working.
please refer here
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var openUrl:NSURL? //This is used when to save state when App is not running before the url trigered
func application(_ application: UIApplication, openURL url: NSURL, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//print("Host: \(url.host!)")
//let url = url.standardized
//NotificationCenter.default.post(name: NSNotification.Name(rawValue: "HANDLEOPENURL"), object:url!)
print("bbb")
return true
}
}
There is no AppDelegate method with that signature, so its not getting called
func application(_ application: UIApplication, openURL url: NSURL, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
The correct method signature is:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool`
You are confusing it with this method
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool
EDIT
To respond to links from the web to iOS, use
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: #escaping ([Any]?) -> Void) -> Bool {
Recently converted a project to Swift 2.2 and running into issues that didn't exist prior. I've tried searching for a workaround but haven't found any similar posts.
Use of undeclared type 'UIApplicationLaunchOptionsKey'.
I can't tell what's wrong with this code in the AppDelegate.swift:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//UIToolbar.appearance().tintColor = UIColor.clearColor()
return true
}
Try changing the method signature to
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
return true
}
The signature is modified to func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool in swift 3
In Swift 3 the method is changed to:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
I have successfully implemented deep linking with Branck iOS SDK.
However on re-opening application shows following warning in console:
Attempting to load the view of a view controller while it is
deallocating is not allowed and may result in undefined behavior
(UIAlertController: 0x16b00800)
My AppDelegate with basic code implementation is:
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
// Branch initialization
let branch: Branch = Branch.getInstance()
branch.initSessionWithLaunchOptions(launchOptions, andRegisterDeepLinkHandler:
{ optParams, error in
if error == nil, let params = optParams
{
print("Branch params: ", params.description)
}
})
return true
}
// Respond to URI scheme links
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool
{
print("Branch url URI: ", url)
// pass the url to the handle deep link call
Branch.getInstance().handleDeepLink(url)
return true
}
// Respond to Universal Links
func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool
{
print("Branch universal link ")
Branch.getInstance().continueUserActivity(userActivity)
return true
}
}
How to remove above warning?
To remove warning AppDelegate must additionally implement application:willContinueUserActivityWithType function that will return true
func application(application: UIApplication, willContinueUserActivityWithType userActivityType: String) -> Bool
{
return true
}