【发布时间】:2013-10-20 16:46:36
【问题描述】:
我正在将所有本机通讯录联系人导入我的应用程序。目前,请求“允许访问联系人”权限的警报视图是在应用程序启动时完成的。
如何更改它以便在应用程序的其他地方询问权限?只需单击一个按钮,就像 Instagram 一样?
看这张图:
【问题讨论】:
标签: ios permissions contacts addressbook
我正在将所有本机通讯录联系人导入我的应用程序。目前,请求“允许访问联系人”权限的警报视图是在应用程序启动时完成的。
如何更改它以便在应用程序的其他地方询问权限?只需单击一个按钮,就像 Instagram 一样?
看这张图:
【问题讨论】:
标签: ios permissions contacts addressbook
您可以阅读文档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
}
}
【讨论】:
当您尝试检索联系人时应用请求许可。
只需在按钮触摸后调用 retriving,而不是在启动后调用。
【讨论】: