SwiftUI: where to initialize frameworks? - ios

With UIKit and UIApplicationDelegate logic, I used to initialize all the frameworks and stuff in the good old applicationDidFinishLaunching... like this:
#UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Parse.initialize(with: ParseClientConfiguration() {
$0.applicationId = PARSE_APP_ID
// ...
})
FirebaseApp.configure()
// etc...
}
}
I'm now learning how to use SwiftUI, but since there is no more UIApplicationDelegate, and I've never really used UISceneDelegate before, I don't really know where I should do this.
Thank you for your help.

Related

Is there a purely SwiftUI way to call registerForRemoteNotifications()?

my app has a struct that implements App, and this struct uses a custom AppDelegate. This is my entire AppDelegate:
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
application.registerForRemoteNotifications()
return true
}
}
I would like to get rid of this AppDelegate and call application.registerForRemoteNotifications() in the App implementation. Is this currently possible with SwiftUI 2.0 or later?

Confusion connecting iOS portion of Flutter App to Firebase

I'm attempting to connect the iOS portion of my Flutter app to Firebase. And as I go through the steps on Firebase - "Add Firebase to your iOS app" - I hit a step that says "Add the initialization code below to your main AppDelegate class" (Swift version):
import UIKit
import Firebase
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?)
-> Bool {
FirebaseApp.configure()
return true
}
}
But my AppDelegate class already has this code:
import UIKit
import Flutter
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Not sure what to do. Do I replace the existing code with the code provided by Firebase or do I reconcile the two somehow?
In the given (predefined) AppDelegate class, there are 2 things you need to do additionally.
They are
import Firebase // <-- 1st add
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
FirebaseApp.configure() // <-- 2nd add
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Merge both code together:
import UIKit
import Flutter
import Firebase
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
FirebaseApp.configure()
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}

UIApplication delegate must be used from main thread only

I have a standard AppDelegate file in my Xcode project, I'm experimenting with Firebase and as soon as I imported it & added
FirebaseApp.configure()
I was flagged with purple warnings: Click me
The whole AppDelegate looks as follows
import UIKit
import Firebase
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
return true
}
These warnings never appeared whilst using Xcode 8, as a novice I am unsure on how to fix this, I have read that adding a DispatchQueue.main.async can fix it but I'm not sure where to add this.
Thanks!

Declaring variable outside the class vs inside

Hey I am trying to initialise a realm object in my app outside the app delegate.
Now, Since a lot of file in my app are accessing the realm file, i decide to declare it globally.
Now, by doing that I got an app rejection, reasoning crash due to this object. My code is as follows
AppDelegate.swift
import UIKit
import RealmSwift
var uiRealm = try! Realm()
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
return true
}
In the above code the object is initialised as the app starts and any other methods loads.
Is it possible that I just declare an object or type Realm and the initialise it under didFinishLaunching as follows:
import UIKit
import RealmSwift
var uiRealm : Realm!
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// I initialise it here
uiRealm = try! Realm()
return true
}
Will it make a difference if its declare in such form. I am a little timid about this.
Any help is highly appreciated.
P.S: I already tried declaring the realm variable in app delegate and the calling it in other classes by UIapplication.shared.delegatemethod, and it always crashes.

GADMobileAds.configure(withApplicationID: "AppID here") not working

I am trying to implement a banner ad in my app and have run into a roadblock.
This is my code:
import UIKit
import Firebase
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FIRApp.configure()
GADMobileAds.configure(withApplicationID: "APPID HERE")
return true
}
This is my error:
Use of unresolved identifier GADMobileAds
I have been watching a few videos now and can't seem to find out where I have gone wrong, any help would be appreciated.

Resources