Firebase app check not working with iOS 16 - ios

It looks like for my app the firebase app check is not working with iOS16. I have configured the app attest and its been working for more than a year, it's still working with older iOS versions but not iOS 16.
This is the code that I initialise app check
import SwiftUI
import Firebase
import FirebaseAppCheck
#main
struct appointmeparterAppApp: App {
#Environment(\.scenePhase) var scenePhase
#UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
init() {
let providerFactory = YourAppCheckProviderFactory()
AppCheck.setAppCheckProviderFactory(providerFactory)
FirebaseApp.configure()
}
}
class YourAppCheckProviderFactory: NSObject, AppCheckProviderFactory {
func createProvider(with app: FirebaseApp) -> AppCheckProvider? {
return AppAttestProvider(app: app)
}
}
I am sure the problem is app check because if it's not enforced everything works as expected, and if I enforce it then i get permissions error. (this happens only on iOS 16)

import SwiftUI
import Firebase
import FirebaseCore
import FirebaseAppCheck
class AppDelegate: NSObject, UIApplicationDelegate {
let providerFactory = ACFTAppCheckProviderFactory()
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
AppCheck.setAppCheckProviderFactory(providerFactory)
FirebaseApp.configure()
return true
}
}
#main
struct ACFTApp: App {
#UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
class ACFTAppCheckProviderFactory: NSObject, AppCheckProviderFactory {
func createProvider(with app: FirebaseApp) -> AppCheckProvider? {
return AppAttestProvider(app: app)
}
}
Set the App attest env. to production. Ensure you add App Attest capability to your app target. Remove the -FIRDebugEnabled in your app scheme/run/arguments. Possibly ensure you add the debug token to your registered apps/manage tokens on FB.

Related

iOS universal links not entering in UIApplicationDelegate callback

I'm trying to use universal links to be detected in a callback in an app developed in SwiftUI. I think all the settings are ok, including the AASA file, capability in the app with applinks: join-domain.com. When I tap in the link https://join-domain.com/something in the notes app it will open my app so I assume is detecting the domain but it never enters in any callback of the appDelegate.
Inside the app I have the following:
import SwiftUI
#main
struct XApp: App {
#UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
and in AppDelegate:
class AppDelegate: UIResponder, UIApplicationDelegate, ObservableObject {
func application(_ application: UIApplication,
continue userActivity: NSUserActivity,
restorationHandler: #escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
print("did it entered here?")
}
}
I run through stackoverflow, nothing worked. In this one thread it mentions the 'Application Scene Manifest' and configurationForConnectingSceneSession. I have nothing of that.
Any ideas?

Using Firebase and SwiftUI

I am very new to both firebase and Xcode SwiftUI. I found many tutorial on the storyboard but could not find tutorials for SwiftUI. I downloaded firebase using pod and the instruction given to us by the firebase documents. I created an AppDelegate.swift and copied the following code:
import Foundation
import UIKit
import Firebase
import SwiftUI
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:
[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
return true
}
}
In the Firebase database, I created a user collection with an example value to test if it works.
So by using this link https://firebase.google.com/docs/firestore/quickstart?authuser=0#swift_3, I looked at the read data section and wrote the code in the ContentView.swift:
db.collection("users").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
print("\(document.documentID) => \(document.data())")
}
}
}
But It says that it has to be a view.
How can I get the data in the firebase database into my Swiftui app?

Running code when app is terminated. Xcode 12

I'm trying to run some code to refresh data and serve a local notification on iOS 14. I'm using Xcode 12 and all the help I find online is based on a structure having an appdelegate.swift swift file with several functions depending on the state of the app.
In Xcode 12 the appdelegate file isn't there anymore. How do I proceed, I don't know where to start. Does anybody know what to do?
Your issue is probably that you set up the project to use SwiftUI. You need to create an AppDelegate object that implements UIApplicationDelegate protocol:
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
print("Your code here")
return true
}
// Implement other methods that you require.
}
On your SwiftUI file, add this line to tell iOS who's gonna be the listener for the AppDelegate's methods:
#main
struct SomeApp: App {
#UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
You can then follow by checking Apple's documentation on how to use background app modes.

How to register plugins using GeneratedPluginRegistrant

How do i register all my plugins when presenting a view controller with implicit engine?
https://flutter.dev/docs/development/add-to-app/ios/add-flutter-screen#alternatively---create-a-flutterviewcontroller-with-an-implicit-flutterengine
When you create new project in Flutter it already ships with code the registers the plugin. Check if you have following;
Android
In your Android project find MainActivity.kt.
Important: Notice that FlutterActivity exist in both io.flutter.embedding.android and io.flutter.app but first one automatically registers all the plugins.
package com.example.myapp
import io.flutter.embedding.android.FlutterActivity
// do not import io.flutter.app.FlutterActivity instead
class MainActivity: FlutterActivity() {
}
iOS
In your iOS project find AppDelegate.swift
import UIKit
import Flutter
import FlutterPluginRegistrant
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
lazy var flutterEngine = FlutterEngine(name: "my flutter engine")
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self.flutterEngine)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
P.S. Make sure you update the Flutter version to get most of it.
iOS reference

Connecting to Firebase in IOS

Following various tutorials most of which seem to be from a couple years ago, I am trying to connect to Firebase from an IOS app and having trouble with how and where to import the libraries and, in turn, connect to the database.
In my app delegate, I am able to establish a connection--I think---inside the difFinishLaunching method (I say I think because Firebase does not recognize the app in the console but this seems to be a frequent occurrence so I'm ignoring that and just trying to get the app to build in Xcode for now) as follows:
import UIKit
import FirebaseAnalytics
import FirebaseDatabase
import FirebaseCore
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
let myDB = Database.database().reference().child("items")
}
return true
}
That seems okay in that it builds.
When I try to put a reference to the database in a viewcontroller, however, it throws an error on Database:
import UIKit
import FirebaseCore
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let ref = Database.database().reference(withPath: "items") //THIS LINE THROWS ERROR
What do I need to do to create a property for the Database in a view controller?
Edit: Looking at some other answers on SO, it seems I may have to create a singleton reference to the db connection. If so, where would I put this and how would I reference it?
Thanks for any suggestions
First, your AppDelegate should look something like this
import UIKit
import Firebase
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
return true
}
then, in your first viewController like this:
import UIKit
import Firebase
class ViewController: UIViewController {
var db: Firestore!
override func viewDidLoad() {
super.viewDidLoad()
self.db = Firestore.firestore()
//do something with Firestore
var ref: DocumentReference? = nil
ref = db.collection("users").addDocument(data: [
"first": "Ada",
"last": "Lovelace",
"born": 1815
]) { err in
if let err = err {
print("Error adding document: \(err)")
} else {
print("Document added with ID: \(ref!.documentID)")
}
}
}
}
Keep in mind I have set my Firestore rules to allow anyone to read/write, which is dangerous so please start working through the Authentication guide once you get this working.
Note that the above code pretty much came straight from the Getting Started guides Add Firebase To An App and Installation and setup

Resources