ios(sign-in): add Google SignIn for ios app
Add KMP bridges to synch login state Use keychain for storing login state on ios Setup interop beween KMP and ios for sign in
This commit is contained in:
@@ -6,6 +6,11 @@
|
||||
objectVersion = 77;
|
||||
objects = {
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
412CF4A32E719E7D00EADB15 /* shared.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = 412CF4A22E719E7D00EADB15 /* shared.xcframework */; };
|
||||
412CF4B12E71B7C300EADB15 /* GoogleSignIn in Frameworks */ = {isa = PBXBuildFile; productRef = 412CF4B02E71B7C300EADB15 /* GoogleSignIn */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXContainerItemProxy section */
|
||||
412CF4592E7037D800EADB15 /* PBXContainerItemProxy */ = {
|
||||
isa = PBXContainerItemProxy;
|
||||
@@ -27,11 +32,25 @@
|
||||
412CF4482E7037D700EADB15 /* Klottr.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Klottr.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
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 = "<absolute>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFileSystemSynchronizedBuildFileExceptionSet section */
|
||||
412CF4B42E71C14D00EADB15 /* Exceptions for "Klottr" folder in "Klottr" target */ = {
|
||||
isa = PBXFileSystemSynchronizedBuildFileExceptionSet;
|
||||
membershipExceptions = (
|
||||
Info.plist,
|
||||
);
|
||||
target = 412CF4472E7037D700EADB15 /* Klottr */;
|
||||
};
|
||||
/* End PBXFileSystemSynchronizedBuildFileExceptionSet section */
|
||||
|
||||
/* Begin PBXFileSystemSynchronizedRootGroup section */
|
||||
412CF44A2E7037D700EADB15 /* Klottr */ = {
|
||||
isa = PBXFileSystemSynchronizedRootGroup;
|
||||
exceptions = (
|
||||
412CF4B42E71C14D00EADB15 /* Exceptions for "Klottr" folder in "Klottr" target */,
|
||||
);
|
||||
path = Klottr;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
@@ -52,6 +71,8 @@
|
||||
isa = PBXFrameworksBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
412CF4A32E719E7D00EADB15 /* shared.xcframework in Frameworks */,
|
||||
412CF4B12E71B7C300EADB15 /* GoogleSignIn in Frameworks */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
@@ -75,9 +96,11 @@
|
||||
412CF43F2E7037D600EADB15 = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
412CF4A22E719E7D00EADB15 /* shared.xcframework */,
|
||||
412CF44A2E7037D700EADB15 /* Klottr */,
|
||||
412CF45B2E7037D800EADB15 /* KlottrTests */,
|
||||
412CF4652E7037D800EADB15 /* KlottrUITests */,
|
||||
412CF4AF2E71B7C300EADB15 /* Frameworks */,
|
||||
412CF4492E7037D700EADB15 /* Products */,
|
||||
);
|
||||
sourceTree = "<group>";
|
||||
@@ -92,6 +115,13 @@
|
||||
name = Products;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
412CF4AF2E71B7C300EADB15 /* Frameworks */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
);
|
||||
name = Frameworks;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
/* End PBXGroup section */
|
||||
|
||||
/* Begin PBXNativeTarget section */
|
||||
@@ -112,6 +142,7 @@
|
||||
);
|
||||
name = Klottr;
|
||||
packageProductDependencies = (
|
||||
412CF4B02E71B7C300EADB15 /* GoogleSignIn */,
|
||||
);
|
||||
productName = Klottr;
|
||||
productReference = 412CF4482E7037D700EADB15 /* Klottr.app */;
|
||||
@@ -195,6 +226,9 @@
|
||||
);
|
||||
mainGroup = 412CF43F2E7037D600EADB15;
|
||||
minimizedProjectReferenceProxies = 1;
|
||||
packageReferences = (
|
||||
412CF4A62E71AF4C00EADB15 /* XCRemoteSwiftPackageReference "GoogleSignIn-iOS" */,
|
||||
);
|
||||
preferredProjectObjectVersion = 77;
|
||||
productRefGroup = 412CF4492E7037D700EADB15 /* Products */;
|
||||
projectDirPath = "";
|
||||
@@ -398,7 +432,9 @@
|
||||
DEVELOPMENT_ASSET_PATHS = "\"Klottr/Preview Content\"";
|
||||
DEVELOPMENT_TEAM = 9LY47TPGT7;
|
||||
ENABLE_PREVIEWS = YES;
|
||||
GENERATE_INFOPLIST_FILE = YES;
|
||||
FRAMEWORK_SEARCH_PATHS = "";
|
||||
GENERATE_INFOPLIST_FILE = NO;
|
||||
INFOPLIST_FILE = Klottr/Info.plist;
|
||||
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
|
||||
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
|
||||
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
|
||||
@@ -427,7 +463,9 @@
|
||||
DEVELOPMENT_ASSET_PATHS = "\"Klottr/Preview Content\"";
|
||||
DEVELOPMENT_TEAM = 9LY47TPGT7;
|
||||
ENABLE_PREVIEWS = YES;
|
||||
GENERATE_INFOPLIST_FILE = YES;
|
||||
FRAMEWORK_SEARCH_PATHS = "";
|
||||
GENERATE_INFOPLIST_FILE = NO;
|
||||
INFOPLIST_FILE = Klottr/Info.plist;
|
||||
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
|
||||
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
|
||||
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
|
||||
@@ -558,6 +596,25 @@
|
||||
defaultConfigurationName = Release;
|
||||
};
|
||||
/* End XCConfigurationList section */
|
||||
|
||||
/* Begin XCRemoteSwiftPackageReference section */
|
||||
412CF4A62E71AF4C00EADB15 /* XCRemoteSwiftPackageReference "GoogleSignIn-iOS" */ = {
|
||||
isa = XCRemoteSwiftPackageReference;
|
||||
repositoryURL = "https://github.com/google/GoogleSignIn-iOS";
|
||||
requirement = {
|
||||
kind = upToNextMajorVersion;
|
||||
minimumVersion = 9.0.0;
|
||||
};
|
||||
};
|
||||
/* End XCRemoteSwiftPackageReference section */
|
||||
|
||||
/* Begin XCSwiftPackageProductDependency section */
|
||||
412CF4B02E71B7C300EADB15 /* GoogleSignIn */ = {
|
||||
isa = XCSwiftPackageProductDependency;
|
||||
package = 412CF4A62E71AF4C00EADB15 /* XCRemoteSwiftPackageReference "GoogleSignIn-iOS" */;
|
||||
productName = GoogleSignIn;
|
||||
};
|
||||
/* End XCSwiftPackageProductDependency section */
|
||||
};
|
||||
rootObject = 412CF4402E7037D600EADB15 /* Project object */;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
{
|
||||
"originHash" : "68aa00e3cba36db2e0a76f54dbc84d1f079d8aa9a19bfa9fce02d6286bd620b7",
|
||||
"pins" : [
|
||||
{
|
||||
"identity" : "app-check",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/app-check.git",
|
||||
"state" : {
|
||||
"revision" : "61b85103a1aeed8218f17c794687781505fbbef5",
|
||||
"version" : "11.2.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "appauth-ios",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/openid/AppAuth-iOS.git",
|
||||
"state" : {
|
||||
"revision" : "145104f5ea9d58ae21b60add007c33c1cc0c948e",
|
||||
"version" : "2.0.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "googlesignin-ios",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/GoogleSignIn-iOS",
|
||||
"state" : {
|
||||
"revision" : "3996d908c7b3ce8a87d39c808f9a6b2a08fbe043",
|
||||
"version" : "9.0.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "googleutilities",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/GoogleUtilities.git",
|
||||
"state" : {
|
||||
"revision" : "60da361632d0de02786f709bdc0c4df340f7613e",
|
||||
"version" : "8.1.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "gtm-session-fetcher",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/gtm-session-fetcher.git",
|
||||
"state" : {
|
||||
"revision" : "a2ab612cb980066ee56d90d60d8462992c07f24b",
|
||||
"version" : "3.5.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "gtmappauth",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/GTMAppAuth.git",
|
||||
"state" : {
|
||||
"revision" : "56e0ccf09a6dd29dc7e68bdf729598240ca8aa16",
|
||||
"version" : "5.0.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "promises",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/promises.git",
|
||||
"state" : {
|
||||
"revision" : "540318ecedd63d883069ae7f1ed811a2df00b6ac",
|
||||
"version" : "2.4.0"
|
||||
}
|
||||
}
|
||||
],
|
||||
"version" : 3
|
||||
}
|
||||
26
ios/Klottr/Klottr/AppDelegate.swift
Normal file
26
ios/Klottr/Klottr/AppDelegate.swift
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// AppDelegate.swift
|
||||
// Klottr
|
||||
//
|
||||
// Created by Olof Hedman on 2025-09-10.
|
||||
//
|
||||
|
||||
|
||||
// iosApp/AppDelegate.swift
|
||||
import UIKit
|
||||
import GoogleSignIn
|
||||
import shared
|
||||
|
||||
class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||
func application(_ application: UIApplication,
|
||||
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
|
||||
PlatformAuthStorage.shared.install(delegate: KeychainStorage())
|
||||
return true
|
||||
}
|
||||
func application(_ app: UIApplication,
|
||||
open url: URL,
|
||||
options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
|
||||
if GIDSignIn.sharedInstance.handle(url) { return true }
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -5,20 +5,30 @@
|
||||
// Created by Olof Hedman on 2025-09-09.
|
||||
//
|
||||
|
||||
import shared
|
||||
import SwiftUI
|
||||
|
||||
struct ContentView: View {
|
||||
var sharedPing = SharedPing()
|
||||
var body: some View {
|
||||
VStack {
|
||||
Image(systemName: "globe")
|
||||
.imageScale(.large)
|
||||
.foregroundStyle(.tint)
|
||||
Text("Hello, world!")
|
||||
Text(sharedPing.ping())
|
||||
Button("Tap me") {
|
||||
var result = sharedPing.ping()
|
||||
NSLog("Ping result: \(result)")
|
||||
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#Preview {
|
||||
ContentView()
|
||||
}
|
||||
|
||||
63
ios/Klottr/Klottr/GoogleAuthError.swift
Normal file
63
ios/Klottr/Klottr/GoogleAuthError.swift
Normal file
@@ -0,0 +1,63 @@
|
||||
//
|
||||
// GoogleAuthError.swift
|
||||
// Klottr
|
||||
//
|
||||
// Created by Olof Hedman on 2025-09-10.
|
||||
//
|
||||
|
||||
|
||||
// iosApp/GoogleAuthIOS.swift
|
||||
import UIKit
|
||||
import GoogleSignIn
|
||||
import shared // Kotlin framework from :shared
|
||||
|
||||
enum GoogleAuthError: Error { case noPresenter }
|
||||
|
||||
enum GoogleAuthIOS {
|
||||
/// Optional: reads client IDs from Info.plist and sets configuration.
|
||||
static func configureFromPlist() {
|
||||
if let clientID = Bundle.main.object(forInfoDictionaryKey: "GIDClientID") as? String {
|
||||
let serverID = Bundle.main.object(forInfoDictionaryKey: "GIDServerClientID") as? String
|
||||
GIDSignIn.sharedInstance.configuration = GIDConfiguration(
|
||||
clientID: clientID,
|
||||
serverClientID: serverID
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Interactive sign-in. Call from a button handler.
|
||||
@MainActor
|
||||
static func signIn(presenting: UIViewController) async throws {
|
||||
let result = try await GIDSignIn.sharedInstance.signIn(withPresenting: presenting) // iOS
|
||||
let user = try await result.user.refreshTokensIfNeeded()
|
||||
let idToken = user.idToken?.tokenString
|
||||
let sub = user.userID
|
||||
let email = user.profile?.email
|
||||
AuthShared.shared.applyLogin(token: idToken, sub: sub, mail: email)
|
||||
}
|
||||
|
||||
/// Silent restore. Call on app start.
|
||||
@MainActor
|
||||
static func restorePreviousSignIn() async {
|
||||
do {
|
||||
if GIDSignIn.sharedInstance.hasPreviousSignIn() {
|
||||
let user = try await GIDSignIn.sharedInstance.restorePreviousSignIn()
|
||||
let refreshed = try await user.refreshTokensIfNeeded()
|
||||
AuthShared.shared.applyLogin(
|
||||
token: refreshed.idToken?.tokenString,
|
||||
sub: refreshed.userID,
|
||||
mail: refreshed.profile?.email
|
||||
)
|
||||
} else {
|
||||
AuthShared.shared.loadFromStorage() // falls back to saved ID token/email
|
||||
}
|
||||
} catch {
|
||||
AuthShared.shared.loadFromStorage()
|
||||
}
|
||||
}
|
||||
|
||||
static func signOut() {
|
||||
GIDSignIn.sharedInstance.signOut()
|
||||
AuthShared.shared.clearAll()
|
||||
}
|
||||
}
|
||||
32
ios/Klottr/Klottr/Info.plist
Normal file
32
ios/Klottr/Klottr/Info.plist
Normal file
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<!-- Standard stuff -->
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>$(PRODUCT_NAME)</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>APPL</string>
|
||||
|
||||
<!-- Google Sign-In config -->
|
||||
<key>GIDClientID</key>
|
||||
<string>YOUR_IOS_CLIENT_ID</string>
|
||||
<key>GIDServerClientID</key>
|
||||
<string>YOUR_WEB_CLIENT_ID</string>
|
||||
|
||||
<!-- URL scheme is required for the OAuth redirect -->
|
||||
<key>CFBundleURLTypes</key>
|
||||
<array>
|
||||
<dict>
|
||||
<key>CFBundleURLSchemes</key>
|
||||
<array>
|
||||
<string>YOUR_REVERSED_IOS_CLIENT_ID</string>
|
||||
</array>
|
||||
</dict>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
57
ios/Klottr/Klottr/KeychainStorage.swift
Normal file
57
ios/Klottr/Klottr/KeychainStorage.swift
Normal file
@@ -0,0 +1,57 @@
|
||||
//
|
||||
// KeychainStorage.swift
|
||||
// Klottr
|
||||
//
|
||||
// Created by Olof Hedman on 2025-09-10.
|
||||
//
|
||||
|
||||
|
||||
// iosApp/KeychainStorage.swift
|
||||
import Foundation
|
||||
import Security
|
||||
import shared // your XCFramework module name
|
||||
|
||||
final class KeychainStorage: NSObject, AuthStorageDelegate {
|
||||
private let service = "app.klottr.auth"
|
||||
|
||||
func get(key: String) -> String? {
|
||||
let q: [String: Any] = [
|
||||
kSecClass as String: kSecClassGenericPassword,
|
||||
kSecAttrService as String: service,
|
||||
kSecAttrAccount as String: key,
|
||||
kSecReturnData as String: true,
|
||||
kSecMatchLimit as String: kSecMatchLimitOne
|
||||
]
|
||||
var out: AnyObject?
|
||||
let status = SecItemCopyMatching(q as CFDictionary, &out)
|
||||
guard status == errSecSuccess, let data = out as? Data else { return nil }
|
||||
return String(data: data, encoding: .utf8)
|
||||
}
|
||||
|
||||
func set(key: String, value: String?) {
|
||||
let base: [String: Any] = [
|
||||
kSecClass as String: kSecClassGenericPassword,
|
||||
kSecAttrService as String: service,
|
||||
kSecAttrAccount as String: key
|
||||
]
|
||||
if value == nil {
|
||||
SecItemDelete(base as CFDictionary)
|
||||
return
|
||||
}
|
||||
let attrs = [kSecValueData as String: value!.data(using: .utf8)!]
|
||||
let status = SecItemUpdate(base as CFDictionary, attrs as CFDictionary)
|
||||
if status == errSecItemNotFound {
|
||||
var add = base
|
||||
add[kSecValueData as String] = value!.data(using: .utf8)!
|
||||
SecItemAdd(add as CFDictionary, nil)
|
||||
}
|
||||
}
|
||||
|
||||
func clear() {
|
||||
let q: [String: Any] = [
|
||||
kSecClass as String: kSecClassGenericPassword,
|
||||
kSecAttrService as String: service
|
||||
]
|
||||
SecItemDelete(q as CFDictionary)
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,9 @@ import SwiftUI
|
||||
|
||||
@main
|
||||
struct KlottrApp: App {
|
||||
// Make AppDelegate participate in the SwiftUI lifecycle
|
||||
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentView()
|
||||
|
||||
Reference in New Issue
Block a user