[ SWIFT ] - 通訊錄 AddressBook 屬性介紹
【通訊錄存取權限介紹】
由ABAddressbookGetAuthorizationStatus查詢通訊錄的存取權限。
存取權限分為:
1.kABAddressbookAuthorizationStatusNotDetermined :表示使用者尚未選擇同意或不同意。
※當遇到使用者尚未決定時,可以再呼叫ABAddressbookRequestAccessWithCompletion來再次詢問是否同意存取。
2.kABAddressbookAuthorizationStatusRestricted: 代表權限受限於某些限制。
3.kABAddressbookAuthorizationStatusDenied 代表使用者點選了不同意。
4.kABAddressbookAuthorizationStatusAuthorized 代表使用者點選同意 。
【程式範例-通訊錄存取權限介紹】
switch ABAddressBookGetAuthorizationStatus(){
case .Authorized:
println("Already authorized") createAddressBook()
case .Denied:
println("You are denied access to address book")
case .NotDetermined:
createAddressBook()
if let theBook: ABAddressBookRef = addressBook{ ABAddressBookRequestAccessWithCompletion(theBook,
{(granted: Bool, error: CFError!) in
if granted{
println("Access is granted")
} else {
println("Access is not granted")
}
})
}
case .Restricted:
println("Access is restricted")
default:
println("Unhandled")
}
【通訊錄相關屬性】
//以下為取得通訊錄相關屬性介紹由ABAddressbookGetAuthorizationStatus查詢通訊錄的存取權限。
存取權限分為:
1.kABAddressbookAuthorizationStatusNotDetermined :表示使用者尚未選擇同意或不同意。
※當遇到使用者尚未決定時,可以再呼叫ABAddressbookRequestAccessWithCompletion來再次詢問是否同意存取。
2.kABAddressbookAuthorizationStatusRestricted: 代表權限受限於某些限制。
3.kABAddressbookAuthorizationStatusDenied 代表使用者點選了不同意。
4.kABAddressbookAuthorizationStatusAuthorized 代表使用者點選同意 。
【程式範例-通訊錄存取權限介紹】
switch ABAddressBookGetAuthorizationStatus(){
case .Authorized:
println("Already authorized") createAddressBook()
case .Denied:
println("You are denied access to address book")
case .NotDetermined:
createAddressBook()
if let theBook: ABAddressBookRef = addressBook{ ABAddressBookRequestAccessWithCompletion(theBook,
{(granted: Bool, error: CFError!) in
if granted{
println("Access is granted")
} else {
println("Access is not granted")
}
})
}
case .Restricted:
println("Access is restricted")
default:
println("Unhandled")
}
【通訊錄相關屬性】
kABPersonFirstNameProperty,名字
kABPersonLastNameProperty,姓氏
kABPersonMiddleNameProperty,中間名
kABPersonPrefixProperty,讀取前碼
kABPersonSuffixProperty,讀取後碼
kABPersonNicknameProperty,匿稱
kABPersonFirstNamePhoneticProperty,名子拼音或音標
kABPersonLastNamePhoneticProperty,姓氏拼音或音標
q kABPersonMiddleNamePhoneticProperty,中間名的漢語拼音或音標
kABPersonOrganizationProperty,組織名稱
kABPersonJobTitleProperty,頭銜
kABPersonDepartmentProperty,部門
kABPersonNoteProperty,備注
【程式範例-通訊錄相關屬性】
//建立通訊錄
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef results = ABAddressBookCopyArrayOfAllPeo
for(int i = 0; i < CFArrayGetCount(results); i++)
{
ABRecordRef person = CFArrayGetValueAtIndex(
//1.讀取firstname
NSString *personName = (NSString*)ABRecordCopyValue(
if(personName != nil)
textView.text = [textView.text stringByAppendingFormat:@"\n姓名
//2.讀取lastname
NSString *lastname = (NSString*)ABRecordCopyValue(
if(lastname != nil)
textView.text = [textView.text stringByAppendingFormat:@"%@\
//3.讀取middlename
NSString *middlename = (NSString*)ABRecordCopyValue(
if(middlename != nil)
textView.text = [textView.text stringByAppendingFormat:@"%@\
//4.讀取prefix首碼
NSString *prefix = (NSString*)ABRecordCopyValue(
if(prefix != nil)
textView.text = [textView.text stringByAppendingFormat:@"%@\
//5.讀取suffix尾碼
NSString *suffix = (NSString*)ABRecordCopyValue(
if(suffix != nil)
textView.text = [textView.text stringByAppendingFormat:@"%@\
//6.讀取nickname匿稱
NSString *nickname = (NSString*)ABRecordCopyValue(
if(nickname != nil)
textView.text = [textView.text stringByAppendingFormat:@"%@\
//7.讀取firstname拼音音標
NSString *firstnamePhonetic = (NSString*)ABRecordCopyValue(
if(firstnamePhonetic != nil)
textView.text = [textView.text stringByAppendingFormat:@"%@\
//8.讀取lastname拼音音標
NSString *lastnamePhonetic = (NSString*)ABRecordCopyValue(
if(lastnamePhonetic != nil)
textView.text = [textView.text stringByAppendingFormat:@"%@\
//9.讀取middlename拼音音標
NSString *middlenamePhonetic = (NSString*)ABRecordCopyValue(
if(middlenamePhonetic != nil)
textView.text = [textView.text stringByAppendingFormat:@"%@\
//10.讀取organization公司
NSString *organization = (NSString*)ABRecordCopyValue(
if(organization != nil)
textView.text = [textView.text stringByAppendingFormat:@"%@\
//11.讀取jobtitle工作
NSString *jobtitle = (NSString*)ABRecordCopyValue(
if(jobtitle != nil)
textView.text = [textView.text stringByAppendingFormat:@"%@\
//12.讀取department部門
NSString *department = (NSString*)ABRecordCopyValue(
if(department != nil)
textView.text = [textView.text stringByAppendingFormat:@"%@\
//13.讀取birthday生日
NSDate *birthday = (NSDate*)ABRecordCopyValue(
if(birthday != nil)
textView.text = [textView.text stringByAppendingFormat:@"%@\
//14.讀取note備忘錄
NSString *note = (NSString*)ABRecordCopyValue(
if(note != nil)
textView.text = [textView.text stringByAppendingFormat:@"%@\
//15.第一次添加該條記錄的時間
NSString *firstknow = (NSString*)ABRecordCopyValue(
NSLog(@"第一次添加該條記錄的時間%@
//16.最後一次修改該條記錄的時間
NSString *lastknow = (NSString*)ABRecordCopyValue(
NSLog(@"最後一次修改該條記錄的時間%
//17.獲取email多值
ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty);
int emailcount = ABMultiValueGetCount(email);
for (int x = 0; x < emailcount; x++)
{
//獲取email Label
NSString* emailLabel = (NSString*)
//獲取email值
NSString* emailContent = (NSString*)
textView.text = [textView.text stringByAppendingFormat:@"%@:%
}
//18.讀取地址多值
ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);
int count = ABMultiValueGetCount(address);
for(int j = 0; j < count; j++)
{
//獲取地址Label
NSString* addressLabel = (NSString*)
textView.text = [textView.text stringByAppendingFormat:@"%@\
//獲取該label下的位址6屬性
NSDictionary* personaddress =(NSDictionary*) ABMultiValueCopyValueAtIndex(
NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey];
if(country != nil)
textView.text = [textView.text stringByAppendingFormat:@"國家:%
NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey];
if(city != nil)
textView.text = [textView.text stringByAppendingFormat:@"城市:%
NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey];
if(state != nil)
textView.text = [textView.text stringByAppendingFormat:@"省:%@
NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey];
if(street != nil)
textView.text = [textView.text stringByAppendingFormat:@"街道:%
NSString* zip = [personaddress valueForKey:(NSString *)kABPersonAddressZIPKey];
if(zip != nil)
textView.text = [textView.text stringByAppendingFormat:@"郵編:%
NSString* coutntrycode = [personaddress valueForKey:(NSString *)
if(coutntrycode != nil)
textView.text = [textView.text stringByAppendingFormat:@"國家編號
}
//19.獲取dates多值
ABMultiValueRef dates = ABRecordCopyValue(person, kABPersonDateProperty);
int datescount = ABMultiValueGetCount(dates);
for (int y = 0; y < datescount; y++)
{
//獲取dates Label
NSString* datesLabel = (NSString*)
//獲取dates值
NSString* datesContent = (NSString*)
textView.text = [textView.text stringByAppendingFormat:@"%@:%
}
//獲取kind值
CFNumberRef recordType = ABRecordCopyValue(person, kABPersonKindProperty);
if (recordType == kABPersonKindOrganization) {
// it's a company
NSLog(@"it's a company\n");
} else {
// it's a person, resource, or room
NSLog(@"it's a person, resource, or room\n");
}
//獲取IM多值
ABMultiValueRef instantMessage = ABRecordCopyValue(person, kABPersonInstantMessagePropert
for (int l = 1; l < ABMultiValueGetCount(
{
//獲取IM Label
NSString* instantMessageLabel = (NSString*)
textView.text = [textView.text stringByAppendingFormat:@"%@\
//獲取該label下的2屬性
NSDictionary* instantMessageContent =(NSDictionary*) ABMultiValueCopyValueAtIndex(
NSString* username = [instantMessageContent valueForKey:(NSString *)
if(username != nil)
textView.text = [textView.text stringByAppendingFormat:@"
NSString* service
if(service != nil)
textView.text = [textView.text stringByAppendingFormat:@"
【一次存取通訊錄全部內容方式】
通訊錄內容只有2種存取方式,(1)ABAddressBookCopyArrayOfAllPeople:一次將全部聯絡人資料取出(2)ABAddressBookCopyPeopleWithNmae:透過人名查詢聯絡人資料。
※上述
ABAddressBookCopyArrayOfAllPeople和ABAddressBookCopyPeopleWithNmae回傳值為CFArrayRef格式,NASrray無法直接使用,因此需透過(a)CFArrayGetCount(b)CFArrayGetValueAtIndex進行處理。
【新增通訊錄聯絡人】
Step1:ABPersonCreate
Step2:ABRecordSetValue
Step3:ABAddressBookAddRecord
【新增通訊錄群組】
Step1:ABGroupCreate
Step2:ABRecordSetValue
Step3:ABAddressBookAddRecord
※利用ABAddressBookHasUnsavedChanges檢查目前通訊錄是否未存檔
【新增通訊錄群組成員】
留言
張貼留言
您好:
歡迎到訪我的Blog,這裡有我的生活經驗、美好的回憶和程式開發經驗分享~
目前努力學習Swift中,希望你會喜歡Swift!
如果可以也請你留言給我一個鼓勵喔!
謝謝