Skip to content

Commit

Permalink
Merge pull request #753 from Lee-sungheon/master
Browse files Browse the repository at this point in the history
Add limited contact permission for iOS 18
  • Loading branch information
morenoh149 authored Sep 30, 2024
2 parents 50c145e + d2e4a0c commit 2a789cb
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,9 @@ Contacts.checkPermission().then(permission => {
if (permission === 'authorized') {
// yay!
}
if (permission === 'limited') {
// ...
}
if (permission === 'denied') {
// x.x
}
Expand Down
10 changes: 3 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NativeModules } from "react-native";
import NativeContacts from "./src/NativeContacts";
import { Contact } from "./type";
import { Contact, PermissionType } from "./type";

const isTurboModuleEnabled = global.__turboModuleProxy != null;
const Contacts = isTurboModuleEnabled ? NativeContacts : NativeModules.Contacts;
Expand Down Expand Up @@ -73,15 +73,11 @@ async function getContactsByEmailAddress(
return Contacts.getContactsByEmailAddress(emailAddress);
}

async function checkPermission(): Promise<
"authorized" | "denied" | "undefined"
> {
async function checkPermission(): Promise<PermissionType> {
return Contacts.checkPermission();
}

async function requestPermission(): Promise<
"authorized" | "denied" | "undefined"
> {
async function requestPermission(): Promise<PermissionType> {
return Contacts.requestPermission();
}

Expand Down
Binary file modified ios/.DS_Store
Binary file not shown.
13 changes: 9 additions & 4 deletions ios/RCTContacts/RCTContacts.mm
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ - (NSDictionary *)constantsToExport
return @{
@"PERMISSION_DENIED": @"denied",
@"PERMISSION_AUTHORIZED": @"authorized",
@"PERMISSION_LIMITED": @"limited",
@"PERMISSION_UNDEFINED": @"undefined"
};
}
Expand All @@ -63,6 +64,8 @@ - (NSDictionary *)constantsToExport
resolve(@"denied");
} else if (authStatus == CNAuthorizationStatusAuthorized){
resolve(@"authorized");
} else if (authStatus == CNAuthorizationStatusLimited) {
resolve(@"limited");
} else {
resolve(@"undefined");
}
Expand Down Expand Up @@ -565,7 +568,7 @@ - (NSString *)getPathForDirectory:(int)directory
}
}];
}
else if( [CNContactStore authorizationStatusForEntityType:entityType]== CNAuthorizationStatusAuthorized)
else if( [CNContactStore authorizationStatusForEntityType:entityType]== CNAuthorizationStatusAuthorized || [CNContactStore authorizationStatusForEntityType:entityType] == CNAuthorizationStatusLimited)
{
resolve([self getFilePathForThumbnailImage:recordID addressBook:contactStore]);
}
Expand Down Expand Up @@ -603,7 +606,7 @@ -(NSString *) getFilePathForThumbnailImage:(NSString *)recordID
}
}];
}
else if( [CNContactStore authorizationStatusForEntityType:entityType]== CNAuthorizationStatusAuthorized)
else if( [CNContactStore authorizationStatusForEntityType:entityType]== CNAuthorizationStatusAuthorized || [CNContactStore authorizationStatusForEntityType:entityType] == CNAuthorizationStatusLimited))
{
resolve([self getContact:recordID addressBook:contactStore withThumbnails:false]);
}
Expand Down Expand Up @@ -1285,6 +1288,8 @@ - (void)checkPermission:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseReject
resolve(@"denied");
} else if (authStatus == CNAuthorizationStatusAuthorized){
resolve(@"authorized");
} else if (authStatus == CNAuthorizationStatusLimited) {
resolve(@"limited");
} else {
resolve(@"undefined");
}
Expand Down Expand Up @@ -1435,7 +1440,7 @@ - (void)getContactById:(nonnull NSString *)recordID resolve:(RCTPromiseResolveBl
}
}];
}
else if( [CNContactStore authorizationStatusForEntityType:entityType]== CNAuthorizationStatusAuthorized)
else if( [CNContactStore authorizationStatusForEntityType:entityType]== CNAuthorizationStatusAuthorized || [CNContactStore authorizationStatusForEntityType:entityType] == CNAuthorizationStatusLimited))
{
resolve([self getContact:recordID addressBook:contactStore withThumbnails:false]);
}
Expand Down Expand Up @@ -1485,7 +1490,7 @@ - (void)getPhotoForId:(nonnull NSString *)recordID resolve:(RCTPromiseResolveBlo
}
}];
}
else if( [CNContactStore authorizationStatusForEntityType:entityType]== CNAuthorizationStatusAuthorized)
else if( [CNContactStore authorizationStatusForEntityType:entityType]== CNAuthorizationStatusAuthorized || [CNContactStore authorizationStatusForEntityType:entityType] == CNAuthorizationStatusLimited))
{
resolve([self getFilePathForThumbnailImage:recordID addressBook:contactStore]);
}
Expand Down
6 changes: 3 additions & 3 deletions src/NativeContacts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TurboModule } from "react-native/Libraries/TurboModule/RCTExport";
import { TurboModuleRegistry } from "react-native";
import { Contact } from "../type";
import { Contact, PermissionType } from "../type";

export interface Spec extends TurboModule {
getAll: () => Promise<any>;
Expand All @@ -18,8 +18,8 @@ export interface Spec extends TurboModule {
getContactsMatchingString: (str: string) => Promise<Contact[]>;
getContactsByPhoneNumber: (phoneNumber: string) => Promise<Contact[]>;
getContactsByEmailAddress: (emailAddress: string) => Promise<Contact[]>;
checkPermission: () => Promise<string>;
requestPermission: () => Promise<string>;
checkPermission: () => Promise<PermissionType>;
requestPermission: () => Promise<PermissionType>;
writePhotoToPath: (contactId: string, file: string) => Promise<boolean>;
iosEnableNotesUsage: (enabled: boolean) => void;
}
Expand Down
2 changes: 2 additions & 0 deletions type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,5 @@ export interface Contact {
urlAddresses: UrlAddress[];
note: string;
}

export type PermissionType = 'authorized' | 'limited' | 'denied' | 'undefined'

0 comments on commit 2a789cb

Please sign in to comment.