How to initialise Facebook SDK in iOS 14(SwiftUI) Widget Extension? - swift

How do I initialisation Facebook SDK in iOS 14(SwiftUI) widget extension? Where do I need to call below method?
ApplicationDelegate.shared.application( application, didFinishLaunchingWithOptions: launchOptions)
My end goal is to call GraphRequest from my widget. And according to documentation, to call graph request we need to initialise Facebook SDK first.
Thanks in advance!

There is a solution at HackingWithSwift
https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-an-appdelegate-to-a-swiftui-app
It uses a AppDelegate in the main view

only a minor note:
If you are writing app from scratch and think You will need Appdelegate logic, you can select at start:
where You get:
#main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
}

Related

App closes on startup on Flutter (iOS) with Firebase with out giving any error

The application is working fine on android, however, when I am opening the application on iOS, it crashes instantly without giving any error.
I solved this problem just by moving below line at the top of the function.
FirebaseApp.configure()
Put the above line as the first instruction in the didFinishLaunchingWithOptions method. Like below.
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}

Failed to get FirebaseApp instance even though I used FirebaseApp.configure()

I'm trying to use firebase firestore but keep getting this error:
Thread 1: "Failed to get FirebaseApp instance. Please call FirebaseApp.configure() before using Firestore"
This happens even though I do call FirebaseApp.configure() here:
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
FirebaseApp.configure()
return true
}
}
And I have been using firebase authentication with no problems. Anyone know how to fix this?

AppTransparencyTracking not working in Swift UI

Since SwiftUI doesnt have a Appdelegate file, I tried adding it through App.swift file.
However, it still doesnt work. What am i missing ?
imported the libraries
import AppTrackingTransparency
import AdSupport
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {func requestIDFA() {
ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in
// Tracking authorization completed. Start loading ads here.
// loadAd()
})
}
Then called the appdelegate under #main
#main
struct MyApp: App {
#UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
If you look at the didFinishLaunchingWithOptions function. You have a call function inside another function so the requestIDFA never calls.
Put your requestIDFA() function out side the didFinishLaunchingWithOptions and call inside the didFinishLaunchingWithOptions.
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
requestIDFA()
return true
}
func requestIDFA() {
ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in
// Tracking authorization completed. Start loading ads here.
// loadAd()
})
}
}
Note: Make sure you have an added key to your Info.plist.
<key>NSUserTrackingUsageDescription</key>
<string>Your reason, why you want to track the user</string>

Cannot add iOS support for versions previous to 13 in Xcode 11

I've already followed this tutorial in both ways, by implementing the #available directives and by removing code/files, but still a black screen displays. Unless I'm missing something I think I've done both things properly.
This is the current status in the way of removing files/code:
SceneDelegate.swift deleted.
AppDelegate.swift like this:
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
}
And finally Info.plist like this:
What am I missing so the screen still displays black due to the program not supporting versions previous to iOS 13?
Add this code in your AppDelegate file in didFinishLaunchingWithOptions method
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
if #available(iOS 13.0, *) {
} else {
let rootVC = //Initialize root controller
self.window?.rootViewController = rootVC
self.window?.makeKeyAndVisible()
}
return true
}
I guide you step by step.
1st remove SceneDelegate file from the project.
Add var window: UIWindow? to AppDelegate.
Remove below func from AppDelegate.
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
Remove Application Scene Manifest key from Info.plist file.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let navigation = UINavigationController(rootViewController: ViewController())
let frame = UIScreen.main.bounds
window = UIWindow(frame: frame)
window!.rootViewController = navigation window!.makeKeyAndVisible()
return true
}
Extra
Add below delegate methods in AppDelegate file, below didFinishLaunchingWithOptions
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:.
}

BackgroundTask iOS 13 Swift with Scenes

I am trying to implement the new iOS 13 background tasks functionality with a new app that is utilizing Scenes (also new). There are not a lot of examples, but the ones I have found were not using SceneDelegates, instead all the logic was contained in the AppDelegate. This causes issues since the event for the app going into the background is now raised in the SceneDelegate, not the AppDelegate.
This method does not fire in AppDelegate when using scenes
func applicationDidEnterBackground(_ application: UIApplication) {
}
This method does fire in SceneDelegate
func sceneDidEnterBackground(_ scene: UIScene) {
}
I am not able to find a corresponding SceneDelegate method for the following method in AppDelegate where all the examples show BGTaskScheduler registering tasks
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
BGTaskScheduler.shared.register(forTaskWithIdentifier: "UNIQUE_ID", using: DispatchQueue.global()) { task in
refreshMethod(task: task as! BGAppRefreshTask)
}
}
My question is whether there is some event in the SceneDelegate where I can register the task and if not what is the recommended pattern to follow for scene based apps.
AppDelegate.didFinishLaunchingWithOptions is still called at app launch, even when you have a SceneDelegate. So you should continue to register your background tasks in that method.