Skip to content

Commit b2c7b8c

Browse files
committed
adding authorization for ios 15
1 parent 50b83ec commit b2c7b8c

2 files changed

Lines changed: 57 additions & 35 deletions

File tree

apps/example/screens/SimpleTab.tsx

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,36 @@ export function SimpleTab() {
3737

3838
const [activities, refreshActivities] = useActivities();
3939

40-
const onPressRequestCallback = useCallback(async () => {
41-
if (authorizationStatus === AuthorizationStatus.notDetermined) {
42-
await requestAuthorization();
43-
} else if (authorizationStatus === AuthorizationStatus.denied) {
44-
Alert.alert(
45-
"You didn't grant access",
46-
"Please go to settings and enable it",
47-
[
48-
{
49-
text: "Open settings",
50-
onPress: () => Linking.openSettings(),
51-
},
52-
{
53-
text: "Cancel",
54-
style: "cancel",
55-
},
56-
],
57-
);
58-
} else {
59-
await revokeAuthorization();
60-
}
61-
}, [authorizationStatus]);
40+
const onPressRequestCallback = useCallback(
41+
async (forIndividualOrChild: "individual" | "child" = "individual") => {
42+
if (authorizationStatus === AuthorizationStatus.notDetermined) {
43+
try {
44+
await requestAuthorization(forIndividualOrChild);
45+
} catch (e) {
46+
Alert.alert("Error requesting authorization", (e as Error).message);
47+
console.log("Error requesting authorization", e);
48+
}
49+
} else if (authorizationStatus === AuthorizationStatus.denied) {
50+
Alert.alert(
51+
"You didn't grant access",
52+
"Please go to settings and enable it",
53+
[
54+
{
55+
text: "Open settings",
56+
onPress: () => Linking.openSettings(),
57+
},
58+
{
59+
text: "Cancel",
60+
style: "cancel",
61+
},
62+
],
63+
);
64+
} else {
65+
await revokeAuthorization();
66+
}
67+
},
68+
[authorizationStatus],
69+
);
6270

6371
const [showCreateActivityPopup, setShowCreateActivityPopup] = useState(false);
6472

@@ -108,12 +116,21 @@ export function SimpleTab() {
108116
<Text style={{ flex: 1 }}>
109117
{authorizationStatusMap[authorizationStatus]}
110118
</Text>
111-
112-
<Button onPress={onPressRequestCallback} mode="contained">
113-
{authorizationStatus === AuthorizationStatus.approved
114-
? "Revoke authorization"
115-
: "Request authorization"}
116-
</Button>
119+
<View style={{ gap: 10 }}>
120+
<Button onPress={() => onPressRequestCallback()} mode="contained">
121+
{authorizationStatus === AuthorizationStatus.approved
122+
? "Revoke authorization"
123+
: "Request authorization"}
124+
</Button>
125+
<Button
126+
onPress={() => onPressRequestCallback("child")}
127+
mode="contained"
128+
>
129+
{authorizationStatus === AuthorizationStatus.approved
130+
? "Revoke child authorization"
131+
: "Request child authorization"}
132+
</Button>
133+
</View>
117134
</View>
118135
<Title>Activities</Title>
119136
{activities.map((activity) => (

packages/react-native-device-activity/ios/ReactNativeDeviceActivityModule.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -570,13 +570,18 @@ public class ReactNativeDeviceActivityModule: Module {
570570
try await ac.requestAuthorization(
571571
for: forIndividualOrChild == "child" ? .child : .individual)
572572
} else {
573-
let errorMessage = "iOS 16.0 or later is required to request authorization."
574-
logger.log("⚠️ \(errorMessage)")
575-
throw NSError(
576-
domain: "FamilyControls",
577-
code: 9999,
578-
userInfo: [NSLocalizedDescriptionKey: errorMessage]
579-
)
573+
// Deprecated iOS 15 API - uses completion handler
574+
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
575+
ac.requestAuthorization { result in
576+
switch result {
577+
case .success:
578+
continuation.resume()
579+
case .failure(let error):
580+
logger.log("❌ Failed to request authorization: \(error.localizedDescription, privacy: .public)")
581+
continuation.resume(throwing: error)
582+
}
583+
}
584+
}
580585
}
581586
}
582587

0 commit comments

Comments
 (0)