diff --git a/.gitignore b/.gitignore index afe0c5b..ce3672b 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,5 @@ google-services.json .kotlin/ # KMP/Gradle build outputs **/build/ - +# iOS secrets +ios/Klottr/Klottr/Config.local.xcconfig diff --git a/ios/Klottr/Klottr.xcodeproj/project.pbxproj b/ios/Klottr/Klottr.xcodeproj/project.pbxproj index 7d23f59..6a09a3b 100644 --- a/ios/Klottr/Klottr.xcodeproj/project.pbxproj +++ b/ios/Klottr/Klottr.xcodeproj/project.pbxproj @@ -9,6 +9,8 @@ /* Begin PBXBuildFile section */ 412CF4A32E719E7D00EADB15 /* shared.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = 412CF4A22E719E7D00EADB15 /* shared.xcframework */; }; 412CF4B12E71B7C300EADB15 /* GoogleSignIn in Frameworks */ = {isa = PBXBuildFile; productRef = 412CF4B02E71B7C300EADB15 /* GoogleSignIn */; }; + 414EDCB22E8E6E730024FBE0 /* libsqlite3.0.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 414EDCB12E8E6E2B0024FBE0 /* libsqlite3.0.tbd */; }; + 414EDCB52E8E76260024FBE0 /* GoogleMaps in Frameworks */ = {isa = PBXBuildFile; productRef = 414EDCB42E8E76260024FBE0 /* GoogleMaps */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -33,6 +35,8 @@ 412CF4582E7037D800EADB15 /* KlottrTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = KlottrTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 412CF4622E7037D800EADB15 /* KlottrUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = KlottrUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 412CF4A22E719E7D00EADB15 /* shared.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = shared.xcframework; path = "/Users/olle/Documents/git-personal/klottr/shared/build/XCFrameworks/release/shared.xcframework"; sourceTree = ""; }; + 414EDCB02E8E6E140024FBE0 /* libsqlite3.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libsqlite3.tbd; path = usr/lib/libsqlite3.tbd; sourceTree = SDKROOT; }; + 414EDCB12E8E6E2B0024FBE0 /* libsqlite3.0.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libsqlite3.0.tbd; path = usr/lib/libsqlite3.0.tbd; sourceTree = SDKROOT; }; /* End PBXFileReference section */ /* Begin PBXFileSystemSynchronizedBuildFileExceptionSet section */ @@ -71,6 +75,8 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 414EDCB52E8E76260024FBE0 /* GoogleMaps in Frameworks */, + 414EDCB22E8E6E730024FBE0 /* libsqlite3.0.tbd in Frameworks */, 412CF4A32E719E7D00EADB15 /* shared.xcframework in Frameworks */, 412CF4B12E71B7C300EADB15 /* GoogleSignIn in Frameworks */, ); @@ -118,6 +124,8 @@ 412CF4AF2E71B7C300EADB15 /* Frameworks */ = { isa = PBXGroup; children = ( + 414EDCB12E8E6E2B0024FBE0 /* libsqlite3.0.tbd */, + 414EDCB02E8E6E140024FBE0 /* libsqlite3.tbd */, ); name = Frameworks; sourceTree = ""; @@ -143,6 +151,7 @@ name = Klottr; packageProductDependencies = ( 412CF4B02E71B7C300EADB15 /* GoogleSignIn */, + 414EDCB42E8E76260024FBE0 /* GoogleMaps */, ); productName = Klottr; productReference = 412CF4482E7037D700EADB15 /* Klottr.app */; @@ -228,6 +237,7 @@ minimizedProjectReferenceProxies = 1; packageReferences = ( 412CF4A62E71AF4C00EADB15 /* XCRemoteSwiftPackageReference "GoogleSignIn-iOS" */, + 414EDCB32E8E76260024FBE0 /* XCRemoteSwiftPackageReference "ios-maps-sdk" */, ); preferredProjectObjectVersion = 77; productRefGroup = 412CF4492E7037D700EADB15 /* Products */; @@ -424,6 +434,8 @@ }; 412CF46D2E7037D800EADB15 /* Debug */ = { isa = XCBuildConfiguration; + baseConfigurationReferenceAnchor = 412CF44A2E7037D700EADB15 /* Klottr */; + baseConfigurationReferenceRelativePath = Config.xcconfig; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; @@ -435,6 +447,7 @@ FRAMEWORK_SEARCH_PATHS = ""; GENERATE_INFOPLIST_FILE = NO; INFOPLIST_FILE = Klottr/Info.plist; + INFOPLIST_KEY_CFBundleDisplayName = Klottr; INFOPLIST_KEY_UIApplicationSceneManifest_Generation = NO; INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; INFOPLIST_KEY_UILaunchScreen_Generation = YES; @@ -455,6 +468,8 @@ }; 412CF46E2E7037D800EADB15 /* Release */ = { isa = XCBuildConfiguration; + baseConfigurationReferenceAnchor = 412CF44A2E7037D700EADB15 /* Klottr */; + baseConfigurationReferenceRelativePath = Config.xcconfig; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; @@ -466,6 +481,7 @@ FRAMEWORK_SEARCH_PATHS = ""; GENERATE_INFOPLIST_FILE = NO; INFOPLIST_FILE = Klottr/Info.plist; + INFOPLIST_KEY_CFBundleDisplayName = Klottr; INFOPLIST_KEY_UIApplicationSceneManifest_Generation = NO; INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; INFOPLIST_KEY_UILaunchScreen_Generation = YES; @@ -606,6 +622,14 @@ minimumVersion = 9.0.0; }; }; + 414EDCB32E8E76260024FBE0 /* XCRemoteSwiftPackageReference "ios-maps-sdk" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/googlemaps/ios-maps-sdk"; + requirement = { + kind = upToNextMajorVersion; + minimumVersion = 10.3.0; + }; + }; /* End XCRemoteSwiftPackageReference section */ /* Begin XCSwiftPackageProductDependency section */ @@ -614,6 +638,11 @@ package = 412CF4A62E71AF4C00EADB15 /* XCRemoteSwiftPackageReference "GoogleSignIn-iOS" */; productName = GoogleSignIn; }; + 414EDCB42E8E76260024FBE0 /* GoogleMaps */ = { + isa = XCSwiftPackageProductDependency; + package = 414EDCB32E8E76260024FBE0 /* XCRemoteSwiftPackageReference "ios-maps-sdk" */; + productName = GoogleMaps; + }; /* End XCSwiftPackageProductDependency section */ }; rootObject = 412CF4402E7037D600EADB15 /* Project object */; diff --git a/ios/Klottr/Klottr.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/ios/Klottr/Klottr.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 4a50188..ccd384b 100644 --- a/ios/Klottr/Klottr.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/ios/Klottr/Klottr.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "68aa00e3cba36db2e0a76f54dbc84d1f079d8aa9a19bfa9fce02d6286bd620b7", + "originHash" : "a121837d5658eb88623237b7d13ceb8badf03b05874b782ea35b621e17a6ec31", "pins" : [ { "identity" : "app-check", @@ -55,6 +55,15 @@ "version" : "5.0.0" } }, + { + "identity" : "ios-maps-sdk", + "kind" : "remoteSourceControl", + "location" : "https://github.com/googlemaps/ios-maps-sdk", + "state" : { + "revision" : "eae9fb0930f6f8ebcc5bfa6f7683da3cc3782cc3", + "version" : "10.3.0" + } + }, { "identity" : "promises", "kind" : "remoteSourceControl", diff --git a/ios/Klottr/Klottr.xcodeproj/project.xcworkspace/xcuserdata/olle.xcuserdatad/UserInterfaceState.xcuserstate b/ios/Klottr/Klottr.xcodeproj/project.xcworkspace/xcuserdata/olle.xcuserdatad/UserInterfaceState.xcuserstate index f3a7525..698b309 100644 Binary files a/ios/Klottr/Klottr.xcodeproj/project.xcworkspace/xcuserdata/olle.xcuserdatad/UserInterfaceState.xcuserstate and b/ios/Klottr/Klottr.xcodeproj/project.xcworkspace/xcuserdata/olle.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ios/Klottr/Klottr/AppDelegate.swift b/ios/Klottr/Klottr/AppDelegate.swift index 220b605..3eb75f8 100644 --- a/ios/Klottr/Klottr/AppDelegate.swift +++ b/ios/Klottr/Klottr/AppDelegate.swift @@ -9,6 +9,7 @@ // iosApp/AppDelegate.swift import UIKit import GoogleSignIn +import GoogleMaps import shared @main @@ -17,6 +18,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { + if let key = Bundle.main.object(forInfoDictionaryKey: "GMSApiKey") as? String, + !key.isEmpty { + GMSServices.provideAPIKey(key) + } else { + assertionFailure("Missing GMSApiKey. Check Config.local.xcconfig and Info.plist wiring.") + } PlatformAuthStorage.shared.install(delegate: KeychainStorage()) return true } diff --git a/ios/Klottr/Klottr/Config.xcconfig b/ios/Klottr/Klottr/Config.xcconfig new file mode 100644 index 0000000..3b0bc9b --- /dev/null +++ b/ios/Klottr/Klottr/Config.xcconfig @@ -0,0 +1 @@ +#include? "Config.local.xcconfig" diff --git a/ios/Klottr/Klottr/Info.plist b/ios/Klottr/Klottr/Info.plist index 3cc5314..027b04a 100644 --- a/ios/Klottr/Klottr/Info.plist +++ b/ios/Klottr/Klottr/Info.plist @@ -14,6 +14,8 @@ APPL CFBundleShortVersionString $(MARKETING_VERSION) + GMSApiKey + $(GMS_API_KEY) CFBundleURLTypes @@ -29,17 +31,13 @@ CFBundleVersion $(CURRENT_PROJECT_VERSION) - UILaunchScreen - - - - UIColorNameLaunchScreenBackground - - UIImageRespectsSafeAreaInsets + UIColorName + LaunchScreenBackground + UIImageRespectsSafeAreaInsets + - GIDClientID 508890452239-66lqrsbphlmqltb6aoe3c0d6dlgtjse8.apps.googleusercontent.com GIDServerClientID @@ -61,6 +59,8 @@ + NSLocationWhenInUseUsageDescription + Platstillstånd behövs för att hitta klotter och använda kartan UISupportedInterfaceOrientations UIInterfaceOrientationPortrait diff --git a/ios/Klottr/Klottr/SceneDelegate.swift b/ios/Klottr/Klottr/SceneDelegate.swift index 25da6aa..6543a08 100644 --- a/ios/Klottr/Klottr/SceneDelegate.swift +++ b/ios/Klottr/Klottr/SceneDelegate.swift @@ -8,31 +8,112 @@ import UIKit import shared +import UserNotifications +import CoreLocation class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? + + private lazy var locationManager: CLLocationManager = { + let m = CLLocationManager() + m.delegate = self + return m + }() func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { guard let windowScene = scene as? UIWindowScene else { return } - let root = MainKt.MainViewController() // Compose VC + var rootVC: UIViewController! // will be set below + + rootVC = MainKt.MainViewController( + onRequestPerms: { [weak self] in + self?.requestAppPermissions() + }, + onLogin: { [weak self] in + guard let presenting = rootVC else { return } + Task { + do { + try await GoogleAuthIOS.signIn(presenting: presenting) + } catch { + NSLog("Sign In Error: \(error)") + } + } + }, + onLogout: { + GoogleAuthIOS.signOut() + } + ) let window = UIWindow(windowScene: windowScene) - window.rootViewController = root + window.rootViewController = rootVC self.window = window window.makeKeyAndVisible() - // If you need to kick off sign-in at launch, do it here: DispatchQueue.main.async { Task { - do { - try await GoogleAuthIOS.signIn(presenting: root) - } catch { - NSLog("Sign In Error: \(error)") - } + await GoogleAuthIOS.restorePreviousSignIn() } } } + + + private func requestAppPermissions() { + requestAlertPermission { [weak self] granted in + NSLog("Alerts granted: \(granted)") + // Chain location after alerts, or call independently as needed: + self?.requestLocationPermission() + } + } + + private func requestAlertPermission(completion: @escaping (Bool) -> Void) { + UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in + if let error = error { NSLog("Alert permission error: \(error)") } + DispatchQueue.main.async { completion(granted) } + } + // If you also need push notifications, you'll later call: + // UIApplication.shared.registerForRemoteNotifications() + } + + private func requestLocationPermission() { + // Configure accuracy if needed + if #available(iOS 14.0, *) { + locationManager.desiredAccuracy = kCLLocationAccuracyBest + } + // Request foreground location permission. Use requestAlwaysAuthorization() only if you need background updates. + locationManager.requestWhenInUseAuthorization() + // Optionally start a one-off location update to prompt the system UI: + locationManager.requestLocation() + } +} + +// MARK: - CLLocationManagerDelegate +extension SceneDelegate: CLLocationManagerDelegate { + // iOS 14+ + func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) { + logAuthStatus(manager) + } + // iOS 13 and earlier + func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { + logAuthStatus(manager) + } + func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { + NSLog("Location: didUpdateLocations \(locations)") + } + func locationManager(_ manager: CLLocationManager, didFailWithError error: any Error) { + NSLog("Location: didFailWithError \(error.localizedDescription)") + } + + private func logAuthStatus(_ manager: CLLocationManager) { + let status = manager.authorizationStatus + switch status { + case .authorizedWhenInUse: NSLog("Location: authorizedWhenInUse") + case .authorizedAlways: NSLog("Location: authorizedAlways") + case .denied: NSLog("Location: denied") + case .restricted: NSLog("Location: restricted") + case .notDetermined: NSLog("Location: notDetermined") + @unknown default: NSLog("Location: unknown") + } + } } diff --git a/shared/src/iosMain/kotlin/app/klottr/Main.kt b/shared/src/iosMain/kotlin/app/klottr/Main.kt index c2f9f91..185fc55 100644 --- a/shared/src/iosMain/kotlin/app/klottr/Main.kt +++ b/shared/src/iosMain/kotlin/app/klottr/Main.kt @@ -3,5 +3,15 @@ package app.klottr import androidx.compose.ui.window.ComposeUIViewController import platform.UIKit.UIViewController -fun MainViewController(): UIViewController = - ComposeUIViewController { KlottrRoot() } +fun MainViewController( + onRequestPerms: () -> Unit = {}, + onLogin: () -> Unit = {}, + onLogout: () -> Unit = {}, +): UIViewController = + ComposeUIViewController { + KlottrAppShell( + onRequestPerms = onRequestPerms, + onLogin = onLogin, + onLogout = onLogout + ) + } diff --git a/shared/src/iosMain/kotlin/app/klottr/util/Klog.ios.kt b/shared/src/iosMain/kotlin/app/klottr/util/Klog.ios.kt index bae2319..ea14bb1 100644 --- a/shared/src/iosMain/kotlin/app/klottr/util/Klog.ios.kt +++ b/shared/src/iosMain/kotlin/app/klottr/util/Klog.ios.kt @@ -18,8 +18,11 @@ actual object KLog { private fun log(level: String, tag: String?, text: () -> String) { if (!enabled) return val tg = tagOrDefault(tag) - // %@ konverterar Kotlin String -> NSString - NSLog("[$level][%@] %@", tg, text()) + // Build string in kotlin + val msg = "[$level][$tg] ${text()}" + // Pass a single argument to avoid C-varargs issues + val safe = msg.replace("%", "%%") // prevent accidental format parsing + NSLog(safe) } actual fun d(tag: String?, msg: () -> String) = log("D", tag, msg) @@ -29,10 +32,10 @@ actual object KLog { actual fun e(tag: String?, t: Throwable?, msg: () -> String) { if (!enabled) return val tg = tagOrDefault(tag) - if (t != null) { - NSLog("[E][%@] %@ — %@", tg, msg(), t.stackTraceToString()) - } else { - NSLog("[E][%@] %@", tg, msg()) - } + val base = "[E][$tg] ${msg()}" + val full = if (t != null) "$base — ${t.stackTraceToString()}" else base + + val safe = full.replace("%", "%%") // prevent accidental format parsing + NSLog(safe) } }