Skip to content

Commit 95fafdb

Browse files
committed
Fix CloudKit secret storage
1 parent 240997b commit 95fafdb

File tree

5 files changed

+60
-18
lines changed

5 files changed

+60
-18
lines changed

demo/ios/JuiceboxDemo.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@
501501
CLANG_ENABLE_MODULES = YES;
502502
CODE_SIGN_ENTITLEMENTS = JuiceboxDemo/Entitlements.plist;
503503
CURRENT_PROJECT_VERSION = 1;
504-
DEVELOPMENT_TEAM = 5HW5GVQ38N;
504+
DEVELOPMENT_TEAM = D5LDRT5BB3;
505505
ENABLE_BITCODE = NO;
506506
FRAMEWORK_SEARCH_PATHS = (
507507
"$(inherited)",
@@ -606,7 +606,7 @@
606606
CLANG_ENABLE_MODULES = YES;
607607
CODE_SIGN_ENTITLEMENTS = JuiceboxDemo/Entitlements.plist;
608608
CURRENT_PROJECT_VERSION = 1;
609-
DEVELOPMENT_TEAM = 5HW5GVQ38N;
609+
DEVELOPMENT_TEAM = D5LDRT5BB3;
610610
FRAMEWORK_SEARCH_PATHS = (
611611
"$(inherited)",
612612
"\"${PODS_ROOT}/hermes-engine/destroot/Library/Frameworks/universal\"",

demo/ios/JuiceboxDemo/Entitlements.plist

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5+
<key>aps-environment</key>
6+
<string>development</string>
7+
<key>com.apple.developer.icloud-container-identifiers</key>
8+
<array>
9+
<string>iCloud.xyz.juicebox.demo</string>
10+
</array>
511
<key>com.apple.developer.icloud-services</key>
612
<array>
713
<string>CloudKit</string>

demo/ios/SecretIdStorage.swift

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class SecretIdStorage: NSObject {
1919

2020
let container = CKContainer.default()
2121

22-
static let secretId = "SecretId"
23-
let secretIdRecordId = CKRecord.ID(recordName: secretId)
22+
static let recordType = "SecretId"
23+
static let recordId = CKRecord.ID(recordName: "xyz.juicebox.jbid")
2424

2525
@objc
2626
func register(
@@ -29,12 +29,20 @@ class SecretIdStorage: NSObject {
2929
reject: @escaping RCTPromiseRejectBlock
3030
) {
3131
Task {
32-
let record = CKRecord(recordType: Self.secretId, recordID: secretIdRecordId)
33-
record.setObject(secretId as NSString, forKey: Self.secretId)
34-
3532
do {
3633
try await checkAccountStatus()
34+
35+
let record: CKRecord
36+
37+
do {
38+
record = try await fetchExistingRecord()
39+
} catch AccountError.noRecord {
40+
record = CKRecord(recordType: Self.recordType, recordID: Self.recordId)
41+
}
42+
43+
record.setValue(secretId, forKey: Self.recordType)
3744
try await container.privateCloudDatabase.save(record)
45+
3846
resolve(())
3947
} catch {
4048
reject("\((error as NSError).code)", "Failed to save record: \(error)", error)
@@ -50,8 +58,8 @@ class SecretIdStorage: NSObject {
5058
Task {
5159
do {
5260
try await checkAccountStatus()
53-
let record = try await container.privateCloudDatabase.record(for: secretIdRecordId)
54-
guard let secretId = record.object(forKey: Self.secretId) as? String else {
61+
let record = try await fetchExistingRecord()
62+
guard let secretId = record.value(forKey: Self.recordType) as? String else {
5563
throw AccountError.noRecord
5664
}
5765
resolve(secretId)
@@ -61,7 +69,15 @@ class SecretIdStorage: NSObject {
6169
}
6270
}
6371

64-
func checkAccountStatus() async throws {
72+
private func fetchExistingRecord() async throws -> CKRecord {
73+
do {
74+
return try await container.privateCloudDatabase.record(for: Self.recordId)
75+
} catch CKError.unknownItem {
76+
throw AccountError.noRecord
77+
}
78+
}
79+
80+
private func checkAccountStatus() async throws {
6581
switch try await container.accountStatus() {
6682
case .couldNotDetermine, .noAccount, .temporarilyUnavailable:
6783
throw AccountError.notRegistered

demo/src/Screens/Secret.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import React, { useState, useEffect } from 'react';
2-
import { View, Text, TouchableOpacity, StyleSheet, Image } from 'react-native';
2+
import {
3+
View,
4+
Text,
5+
TouchableOpacity,
6+
StyleSheet,
7+
Image,
8+
Platform,
9+
} from 'react-native';
310
import AsyncStorage from '@react-native-async-storage/async-storage';
411
import { CommonActions } from '@react-navigation/native';
512

@@ -71,12 +78,12 @@ const styles = StyleSheet.create({
7178
backgroundColor: '#f7f7f7',
7279
borderRadius: 20,
7380
alignItems: 'center',
74-
maxWidth: 375,
81+
maxWidth: 360,
7582
},
7683
secretText: {
77-
fontSize: 16,
84+
fontSize: 15,
7885
fontWeight: '300',
79-
fontFamily: 'monospace',
86+
fontFamily: Platform.OS === 'ios' ? 'Courier New' : 'monospace',
8087
color: '#032f62',
8188
paddingVertical: 10,
8289
paddingHorizontal: 20,

demo/src/Screens/Setup.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,27 @@ const Setup = ({ navigation, route }) => {
9090

9191
if (secretId != null) return;
9292

93+
const isNotSignedInError = ({
94+
message,
95+
code,
96+
domain,
97+
}: {
98+
message: string;
99+
code: string;
100+
domain: string | undefined;
101+
}) => {
102+
if (message === 'google drive unavailable') return true;
103+
if (code === '0' && domain === 'Juicebox.SecretIdStorage.AccountError')
104+
return true;
105+
return false;
106+
};
107+
93108
const createSecretId = async () => {
94109
try {
95110
setSecretId(await SecretIdStorage.recover());
96111
} catch (e) {
97112
// @ts-ignore
98-
if (e.message === 'google drive unavailable') {
113+
if (isNotSignedInError(e)) {
99114
showNotSignedInError();
100115
} else {
101116
setSecretId(await JuiceboxSdk.randomSecretId());
@@ -109,9 +124,7 @@ const Setup = ({ navigation, route }) => {
109124
} catch (e) {
110125
showNotSignedInError(
111126
// @ts-ignore
112-
e.message !== 'google drive unavailable'
113-
? 'An existing account was not found.'
114-
: null
127+
!isNotSignedInError(e) ? 'An existing account was not found.' : null
115128
);
116129
}
117130
};

0 commit comments

Comments
 (0)