我正在将所有原生地址簿联系人导入我的应用程序。目前,要求获得允许访问联系人的权限的提醒视图'是在应用程序启动时完成的。
如何更改它以便在应用程序的其他位置询问权限?点击按钮就像Instagram一样?
看到这张图片:
答案 0 :(得分:22)
您可以阅读文档HERE。
以下是一些可以帮助您入门的代码:
//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
}
以下是您可能希望看到的另外两个教程,您可以找到HERE和HERE。
希望这有帮助!
更新:您必须使用didFinishLaunchingWithOptions检测您的应用首次启动:
- (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
}
}
答案 1 :(得分:0)
应用程序在您尝试检索联系人时请求权限。
只需在按钮触摸后调用检索,而不是在启动后调用。