2017-09-08 25 views
1

如何檢查用戶將應用程序設置設置爲什麼?我想我應該能夠使用UserDefaults來做到這一點。我特別想檢查用戶是否允許應用訪問聯繫人。如果我知道鑰匙,我知道如何獲得設置。我希望能夠訪問UserDefaults爲應用程序提供的所有設置。我正在使用Swift創建一個iOS應用程序。使用Swift在iOS中使用userDefaults檢查應用程序設置

回答

1

您誤解了UserDefaults類的功能。它不知道用戶的設置。它用於您永久保存用戶的設置(從您的應用程序中)。

至於如何獲取這種信息。您必須使用每個API來檢查權限。例如,訪問GPS需要使用LocationManager API並檢查那裏的許可。

要檢查如果用戶已給聯繫人的訪問,你必須使用聯繫人API(link here

更具體地說this方法:

class func authorizationStatus(for entityType: CNEntityType) -> CNAuthorizationStatus 

編輯:

你真的應該讀關於UserDefaults類的用途。 (link here

0

您不能使用UserDefaults來檢查用戶是否允許應用程序訪問聯繫人。嘗試下面的代碼。

 let status = CNContactStore.authorizationStatus(for: CNEntityType.contacts) 

     switch (status) { 

     // Not determined or restricted (e.g.Parent control) 
     case CNAuthorizationStatus.notDetermined,CNAuthorizationStatus.restricted: 


     // Denied 
     case CNAuthorizationStatus.denied: 

     // Allowed 
     case CNAuthorizationStatus.authorized: 

     } 
+0

*「您不必使用UserDefaults」* - 您的意思是您不能使用UserDefaults。至少不適合這些設置。 – rmaddy

+0

是的,對於混淆抱歉。 –

相關問題