如何为地址簿获取系统生成的alertview

时间:2016-05-31 06:36:21

标签: ios objective-c

我正在使用我的应用程序中的地址簿,我只想获得book = FactoryGirl.create(:book)访问地址。如何显示系统生成警报以访问alertview

3 个答案:

答案 0 :(得分:0)

如果您想在要求使用其联系人或地址簿的权限时向用户显示自定义消息,而不是上面显示的默认消息,则需要将此字段添加到Info.plist文件中:

NSContactsUsageDescription

这是相同的苹果开发者链接: https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW14

答案 1 :(得分:0)

// Check the authorization status of your application for Address Book
-(void)checkAddressBookAccess
{
    switch (ABAddressBookGetAuthorizationStatus())
    {
            // Update our UI if the user has granted access to their Contacts
        case  kABAuthorizationStatusAuthorized:
            [self showAddressBook];
            break;
            // Prompt the user for access to Contacts if there is no definitive answer
        case  kABAuthorizationStatusNotDetermined :
            [self requestAddressBookAccess];
            break;
            // Display a message if the user has denied or restricted access to Contacts
        case  kABAuthorizationStatusDenied:
        case  kABAuthorizationStatusRestricted:
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning"
                                                            message:@"You have not granted access to your contacts or you have revoked it earlier."
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        }
            break;
        default:
            break;
    }
}

// Prompt the user for access to their Address Book data
-(void)requestAddressBookAccess
{
    TF_Message_View_Controller * __weak weakSelf = self;

    ABAddressBookRequestAccessWithCompletion(self.addressBook, ^(bool granted, CFErrorRef error)
                                             {
                                                 if (granted)
                                                 {
                                                     dispatch_async(dispatch_get_main_queue(), ^{
                                                         [weakSelf showAddressBook];

                                                     });
                                                 }
                                             });
}

答案 2 :(得分:0)

如果您想显示要求许可的警报,可以使用以下代码

此代码适用于Contacts框架,该框架仅适用于iOS 9.0 +

- (void)checkContactStoreAccess {
    self.contactsStrore = [[CNContactStore alloc] init];

    switch ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]) {
        case CNAuthorizationStatusAuthorized: {
            [self fetchContactsFromContactStore];
            break;
        }
        case CNAuthorizationStatusNotDetermined:{
            [self requestContactsAccess];
            break;
        }
        case CNAuthorizationStatusDenied:
        case CNAuthorizationStatusRestricted:{
            //show info that access denied for contact and redirect user to settings with 
            [UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
        }
    }
}
- (void)requestContactsAccess {
    typeof(self)  __weak weakSelf = self;
    [self.contactsStrore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
        [weakSelf fetchContactsFromContactStore];
    }];
}