2013-10-12 19 views
12

Tôi đang nhập tất cả địa chỉ liên hệ trong sổ địa chỉ gốc vào ứng dụng của mình. Hiện tại, cảnh báo để yêu cầu quyền 'cho phép truy cập vào Danh bạ' được thực hiện khi khởi chạy ứng dụng.Yêu cầu quyền cho phép truy cập vào Danh bạ iOS

Làm cách nào để thay đổi nó để cho phép được yêu cầu ở nơi khác trong ứng dụng? Tại một nút bấm, giống như Instagram?

Xem ảnh này:

enter image description here

+0

Kiểm tra [Truy vấn yêu cầu theo lập trình cho liên hệ] (http://stackoverflow.com/questions/12648244/programmatically-request-access-to-contacts) –

+0

iOS 9 trở lên: http://stackoverflow.com/a/ 39374916/569789 – Zaraki

Trả lời

22

Bạn có thể đọc tài liệu HERE.

Dưới đây là một số mã để giúp bạn bắt đầu:

//First of all import the AddRessBookUI 
    #import <AddressBookUI/AddressBookUI.h> 

    // Request to authorise the app to use addressbook 
    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(nil, nil); 

    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) { 
    ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) { 
     if (granted) { 
      // If the app is authorized to access the first time then add the contact 
      [self _addContactToAddressBook]; 
     } else { 
      // Show an alert here if user denies access telling that the contact cannot be added because you didn't allow it to access the contacts 
     } 
    }); 
    } 
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) { 
    // If the user user has earlier provided the access, then add the contact 
    [self _addContactToAddressBook]; 
    } 
    else { 
    // If the user user has NOT earlier provided the access, create an alert to tell the user to go to Settings app and allow access 
    } 

Dưới đây là một vài hướng dẫn mà bạn có thể muốn xem mà bạn có thể tìm HEREHERE.

Hy vọng điều này sẽ hữu ích!

UPDATE: Bạn phải sử dụng didFinishLaunchingWithOptions để phát hiện lần đầu tiên ứng dụng của bạn khởi chạy:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"]) 
    { 
     // The app has already launched once 
    } 
    else 
    { 
     [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"]; 
     [[NSUserDefaults standardUserDefaults] synchronize]; 
     // This is the first time the app is launched 
    } 
} 
+0

Làm cách nào để dừng ứng dụng yêu cầu quyền truy cập khi khởi chạy ứng dụng? – Hassan

+0

@Hassan kiểm tra cập nhật của tôi về cách phát hiện lần đầu tiên ứng dụng của bạn khởi chạy. Trong phần còn lại, bạn có thể viết một số mã để không yêu cầu quyền truy cập danh bạ. – AJ112

+0

thực sự đã giúp tôi! cảm ơn guys –

0

App xin phép khi bạn cố gắng truy xuất danh bạ.

Chỉ cần gọi lại sau khi chạm nút, không phải sau khi khởi chạy.

+0

là có một phương pháp mà nó truy cập khi yêu cầu sự cho phép? – Hassan

Các vấn đề liên quan