Skip to content

Commit d01e449

Browse files
authored
adding authorization for ios 15 (#89)
* adding authorization for ios 15 * removing log
1 parent 44fe314 commit d01e449

2 files changed

Lines changed: 52 additions & 35 deletions

File tree

apps/example/screens/SimpleTab.tsx

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,31 @@ 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+
await requestAuthorization(forIndividualOrChild);
44+
} else if (authorizationStatus === AuthorizationStatus.denied) {
45+
Alert.alert(
46+
"You didn't grant access",
47+
"Please go to settings and enable it",
48+
[
49+
{
50+
text: "Open settings",
51+
onPress: () => Linking.openSettings(),
52+
},
53+
{
54+
text: "Cancel",
55+
style: "cancel",
56+
},
57+
],
58+
);
59+
} else {
60+
await revokeAuthorization();
61+
}
62+
},
63+
[authorizationStatus],
64+
);
6265

6366
const [showCreateActivityPopup, setShowCreateActivityPopup] = useState(false);
6467

@@ -108,12 +111,21 @@ export function SimpleTab() {
108111
<Text style={{ flex: 1 }}>
109112
{authorizationStatusMap[authorizationStatus]}
110113
</Text>
111-
112-
<Button onPress={onPressRequestCallback} mode="contained">
113-
{authorizationStatus === AuthorizationStatus.approved
114-
? "Revoke authorization"
115-
: "Request authorization"}
116-
</Button>
114+
<View style={{ gap: 10 }}>
115+
<Button onPress={() => onPressRequestCallback()} mode="contained">
116+
{authorizationStatus === AuthorizationStatus.approved
117+
? "Revoke authorization"
118+
: "Request authorization"}
119+
</Button>
120+
<Button
121+
onPress={() => onPressRequestCallback("child")}
122+
mode="contained"
123+
>
124+
{authorizationStatus === AuthorizationStatus.approved
125+
? "Revoke child authorization"
126+
: "Request child authorization"}
127+
</Button>
128+
</View>
117129
</View>
118130
<Title>Activities</Title>
119131
{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)