1、申請授權
[objc]view plain
copy
?#import
@end
[self
requestauthorizationaddressbook];
return
yes;
} - (void)requestauthorizationaddressbook else
});
} }
@end
2、ios10之後需要在info.plist中配置nscontactsusagedescription
[objc]view plain
copy
?nscontactsusagedescription
請求訪問通訊錄
nscontactsusagedescription
請求訪問通訊錄
3、請求訪問並獲取通訊錄
3.1、匯入標頭檔案
#import
3.2、訪問並獲取
[objc]view plain
copy
?- (void)getaddressbookclick);
}else
}else
});
}else);
} }
- (nsmutablearray *)fetchcontactwithaddressbook:(abaddressbookref)addressbookelse
if([commandhelpcheckstringisnull:firstname])else
if ([commandhelpcheckstringisnull:lastname])else
abmutablemultivalueref phonenumref = abrecordcopyvalue(people, kabpersonphoneproperty);
nsstring *phonenumber = ((__bridge nsarray *)abmultivaluecopyarrayofallvalues(phonenumref)).lastobject;
//判斷手機號null
nsstring *phone;
if ([commandhelpcheckstringisnull:phonenumber]) else
//如果不加上面的判斷,這裡加入陣列的時候會出錯,不會判斷(null)這個東西,所以要先排除
[contactsaddobject:@];
} return contacts;
}else
}
- (void)getaddressbookclick);
}else
}else
});}else);
}}- (nsmutablearray *)fetchcontactwithaddressbook:(abaddressbookref)addressbookelse if([commandhelp checkstringisnull:firstname])else if ([commandhelp checkstringisnull:lastname])else
abmutablemultivalueref phonenumref = abrecordcopyvalue(people, kabpersonphoneproperty);
nsstring *phonenumber = ((__bridge nsarray *)abmultivaluecopyarrayofallvalues(phonenumref)).lastobject;
//判斷手機號null
nsstring *phone;
if ([commandhelp checkstringisnull:phonenumber]) else
//如果不加上面的判斷,這裡加入陣列的時候會出錯,不會判斷(null)這個東西,所以要先排除
[contacts addobject:@];
}return contacts;
}else
}
4、獲取資料示例
iOS 獲取手機通訊錄
首先匯入 addressbook.framework 獲取通訊錄資訊核心 如下 獲取通訊錄中每條資訊內容 abaddressbookref tmpaddressbook abaddressbookcreatewithoptions null,null dispatch semaphore t sem...
獲取手機通訊錄
在公司裡寫了一段上傳使用者通訊錄的 寫下來,以便以後查閱 獲取手機裡的通訊錄 獲取通訊錄 param context return 返回包含使用者名稱,與號碼的arraylist public arraylistgetcontact context context phones.close alla...
獲取手機通訊錄
手機聯絡人,通過乙個服務想歪提供資料 所有手機聯絡人的資料我們會在服務裡面查詢,然後再返回給activity 首先,我們建立出 contentresolver contentresolver 的建立時通過上下文來獲取的 contentresolver cr context.getcontentres...