iOS - Calling SceneDelegate method from ViewController - ios

I tried following code to call Scene Delegate func from a VC and getting error
Thread 1: signal SIGABRT
let sceneDelegate = UIApplication.shared.delegate as! SceneDelegate
sceneDelegate.initializeFirstViewController()
Is there any way i can call SceneDelegate func from VC?

UIApplication.shared.delegate is AppDelegate not SceneDelegate You need
if let scene = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate {
// to do
}

Related

App.Delegate from ViewController to TabController Swift 5

My code does not work in Swift 5
var myTabBar = self.storyboard?.instantiateViewController(withIdentifier: "myTabBar") as! UITabBarController
var app = UIApplication.shared.delegate as! AppDelegate
app.window?.rootViewController = myTabBar
Value of type AppDelegate has no member window
Because in new Xcode window is moved to SceneDelegate
I'm meeting you more than half-way here, because we really need more details on what's going on and what you're trying to achieve.
But the error is telling you that the AppDelegate doesn't have a variable called window. You probably want the UIApplication object itself, and not its delegate.
Maybe try replacing
var app = UIApplication.shared.delegate as! AppDelegate
app.window?.rootViewController = myTabBar
with
var app = UIApplication.shared
app.keyWindow?.rootViewController = myTabBar

UIApplication Instance fail

if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
// FAIL
}
But If I do UIApplication.shared.delegate then I'm getting Optional<UIApplicationDelegate>
Please help.
AppDelegate: https://gist.github.com/n1tesh/8f069e3b4eb9d843e691f5d463b81017
with UIApplication.shared.delegate you are getting
Optional<UIApplicationDelegate> is actually protocol, and with this line
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
you are checking that some class named AppDelegate is confirming that protocol. If your if condition fails that means that your AppDelegate is not confirm UIApplicationDelegate protocol methods. and not related but also confirm #UIApplicationMain is there in top of AppDelegate class

How do I refactor my code to call AppDelegate on the main thread?

I recently started migrating my project from Swift3/Xcode8 to Swift4/Xcode9. My app crashes at runtime because the main thread sanitizer allows access to UIApplication.shared.delegate only on the main thread, resulting in a crash at startup. I have the following code, which worked fine in Swift 3 -
static var appDelegate: AppDelegate {
return UIApplication.shared.delegate as! AppDelegate;
}
Other classes in my code make access to appDelegate. I need to figure out a way to return UIApplication.shared.delegate from the main thread.
Note: Using an DispatchQueue.main.async{} block from wherever access is made to appDelegate is not an option. Need to use it only within the static var appDelegate declaration only.
Looking for a clever workaround.
Relevant crash message:
Main Thread Checker: UI API called on a background thread: -[UIApplication delegate]
PID: 1094, TID: 30824, Thread name: (none), Queue name: NSOperationQueue 0x60400043c540 (QOS: UNSPECIFIED), QoS: 0
Solved by using Dispatch Groups.
static var realDelegate: AppDelegate?;
static var appDelegate: AppDelegate {
if Thread.isMainThread{
return UIApplication.shared.delegate as! AppDelegate;
}
let dg = DispatchGroup();
dg.enter()
DispatchQueue.main.async{
realDelegate = UIApplication.shared.delegate as? AppDelegate;
dg.leave();
}
dg.wait();
return realDelegate!;
}
and call it somewhere else for
let appDelegate = AppDelegate(). realDelegate!
I use the following:
static var shared: AppDelegate? {
if Thread.isMainThread {
return UIApplication.shared.delegate as? AppDelegate
}
var appDelegate: AppDelegate?
DispatchQueue.main.sync {
appDelegate = UIApplication.shared.delegate as? AppDelegate
}
return appDelegate
}

Swift: getting AppDelegate instance as a class func results in Thread 1: EXC_BAD_INSTRUCTION

I want to get my AppDelegate reference from a class func in my AppDelegate. Why is this throwing a Thread 1: EXC_BAD_INSTRUCTION?
class func getDelegate() -> AppDelegate {
return UIApplication.sharedApplication().delegate as! AppDelegate
}
I have also tried to move this to another utility class and as a regular func, but getting the same crash.
Is there a way I can access the AppDelegate as a class func instead of having to write
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
in every class?
Declare class function to get appDelegate in AppDelegate class as
class func getDelegate() -> AppDelegate {
return UIApplication.sharedApplication().delegate as! AppDelegate
}
To access and use appDelegate in other class, call
let delegate = AppDelegate.getDelegate()
delegate.printHello()
Create a class as shown below[in a new class file or in existing class file outside the previous class. No need to put it inside AppDelete.swift file]
class Delegate
{
static var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
}
and you can use it as Delegate.appDelegate in any class.

manageObjectContext failure

Simple app starts with a table view in a navigation controller with an add button.
My problem is here:
func saveName(name: String) {
//1
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext!
I have issue:
'AppDelegate' does not have a member named 'managedObjectContext'
Thanks.

Resources