Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdarwinpermissionplugin_contacts.mm
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <Contacts/Contacts.h>
7
8@interface QDarwinContactsPermissionHandler ()
9@property (nonatomic, retain) CNContactStore *contactStore;
10@end
11
12@implementation QDarwinContactsPermissionHandler
13- (Qt::PermissionStatus)checkPermission:(QPermission)permission
14{
15 Q_UNUSED(permission);
16 return [self currentStatus];
17}
18
19- (Qt::PermissionStatus)currentStatus
20{
21 const auto status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
22 switch (status) {
23 case CNAuthorizationStatusAuthorized:
24#if (defined(Q_OS_IOS) && QT_IOS_PLATFORM_SDK_EQUAL_OR_ABOVE(180000)) || defined(Q_OS_VISIONOS)
25 case CNAuthorizationStatusLimited:
26#endif
27 return Qt::PermissionStatus::Granted;
28 case CNAuthorizationStatusDenied:
29 case CNAuthorizationStatusRestricted:
30 return Qt::PermissionStatus::Denied;
31 case CNAuthorizationStatusNotDetermined:
32 return Qt::PermissionStatus::Undetermined;
33 }
34 qCWarning(lcPermissions) << "Unknown permission status" << status << "detected in"
35 << QT_STRINGIFY(QT_DARWIN_PERMISSION_PLUGIN);
36 return Qt::PermissionStatus::Denied;
37}
38
39- (QStringList)usageDescriptionsFor:(QPermission)permission
40{
41 Q_UNUSED(permission);
42 return { "NSContactsUsageDescription" };
43}
44
45- (void)requestPermission:(QPermission)permission withCallback:(PermissionCallback)callback
46{
47 if (!self.contactStore) {
48 // Note: Creating the CNContactStore results in warnings in the
49 // console about "Attempted to register account monitor for types
50 // client is not authorized to access", mentioning CardDAV, LDAP,
51 // and Exchange. This seems like a Contacts API bug.
52 self.contactStore = [[CNContactStore new] autorelease];
53 }
54
55 [self.contactStore requestAccessForEntityType:CNEntityTypeContacts
56 completionHandler:^(BOOL granted, NSError * _Nullable error) {
57 Q_UNUSED(granted); // We use status instead
58 // Permission denied will result in an error, which we don't
59 // want to report/log, so we ignore the error and just report
60 // the status.
61 Q_UNUSED(error);
62
63 callback([self currentStatus]);
64 }
65 ];
66}
67
68@end
69
70#include "moc_qdarwinpermissionplugin_p_p.cpp"
QList< QString > QStringList
Constructs a string list that contains the given string, str.