我試圖使用NSLog()
將所有手機的聯繫人打印到控制檯。目前這個代碼只是打印(null)
。從ABAddressBookRequestAccessWithCompletion()中獲取聯繫人數組給出無
.H
@property (nonatomic, strong) NSMutableArray *contactsObjects;
.M
@synthesize contactsObjects;
//lazy instantiation.
- (NSMutableArray *)contactsObjects
{
if(!contactsObjects)
{
contactsObjects = [[NSMutableArray alloc]init];
}
return contactsObjects;
}
- (void)viewWillAppear:(BOOL)animated
{
CFErrorRef error = nil;
// Request authorization to Address Book
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, &error);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
if (granted) {
// First time access has been granted, add all the user's contacts to array.
CFMutableArrayRef contactsObjects = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
} else {
// User denied access.
// Display an alert telling user that they must allow access to proceed to the "invites" page.
}
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add all the user's contacts to array.
CFMutableArrayRef contactsObjects = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
}
else {
// The user has previously denied access
// Send an alert telling user that they must allow access to proceed to the "invites" page.
}
NSLog(@"%@", contactsObjects);
}
我來到這裏兩個警告:
我不知道我我應該這樣做才能正確地將我的聯繫人的姓名和號碼打印到控制檯。
如何打印我的聯繫人姓名和電話號碼?
「viewWillAppear:」中有沒有關於陰影伊娃的警告?或者關於一個未聲明的變量指向setter的錯誤? –
我其實沒有。這兩個錯誤是「黃色!」錯誤,他們只是爲了我上面發佈的錯誤。 – Chisx
你在什麼版本的Xcode?你明確合成'contactsObjects'嗎? –