google-ar / arcore-ios-sdk

ARCore SDK for iOS

Home Page:https://developers.google.com/ar/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Creating GARSession fails first time, succeeds after delay, manual ARCore installation

nrj opened this issue · comments

Update: I can now create GARSessions, however it requires adding a delay. See second comment.

Hello, I'm attempting to add ARCore and ARCoreGeospatial to an iOS application and, because of reasons out of my control, I cannot use Cocoapods or SPM. Here is what I've tried so far:

I've created an empty project and added (and linked) the following xcframeworks:
Screen Shot 2022-12-19 at 12 41 27

Then I've added $(inherited) -ObjC options as other linker flags.

I've configured a new Firebase app and added the following to my app delegate:

import UIKit
import FirebaseCore
import ARCoreGARSession

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var garSession: GARSession!

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        FirebaseApp.configure()
        garSession = try! GARSession(apiKey: "my-api-key", bundleIdentifier: nil)
        return true
    }
}

Everything compiles and installs fine, however the try ! line where I create the garSession raises an exception with error code -1 which from what I can see means my device is not supported, which is not the case for my iPhone 13 Pro.

'try!' expression unexpectedly raised an error: Error Domain=GARSession Code=-1 "(null)"

Additionally I've tested the exact same thing with a Cocoapods and SPM installation and it runs fine.

Any tips on how to properly add ARCore to my project without using Cocoapods?

Xcode 14.1
iOS 16.0.2

I've found something strange. If I attempt to create the GARSession a second time after a delay, it will succeed. Additionally, after it succeeds once, it will succeed all subsequent times on all future runs, until the app is uninstalled.

Here is some code demonstrating the problem:

import UIKit
import FirebaseCore
import ARCoreGARSession

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var garSession: GARSession?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()

        do {
            // first attempt to create a `GARSession` always fails and returns nil
            print("first attempt creating GARSession")
            garSession = try GARSession(apiKey: "my-api-key", bundleIdentifier: nil)
        } catch {
            // GARSession Code=-1 is thrown here
            print("error: \(error)")
        }
        print("after first attempt, garSession = \(garSession)")

        if garSession == nil {
            print("failed to create garSession, delaying 5 seconds...")
            // adding a second creation attempt after delay will succeed
            DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
                print("second attempt creating GARSession")
                // this succeeds, and after it does all future attempts will succeed until the app is uninstalled
                self.garSession = try? GARSession(apiKey: "my-api-key", bundleIdentifier: nil)
                print("after second attempt, garSession = \(self.garSession)")
            }
        }

        return true
    }
}

Tested on:
Xcode 14.1
ARCore-1.35.0
iPhone 14 Pro running iOS 16.1.1
iPhone 13 Pro running iOS 16.0.2

Please refrain from opening GitHub issues if you are not using a supported integration method (either CocoaPods or Swift Package Manager).