nativeContact.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * nativeContact,通过Native.js调用原生API
  3. * 选择通讯录电话
  4. */
  5. export const nativeContact = {
  6. /**
  7. * 通讯录模块
  8. */
  9. contacts: {
  10. getContact: (callBack) => {
  11. switch (plus.os.name) {
  12. case "iOS":
  13. if (plus.device.model === "iPhoneSimulator") {
  14. //模拟器
  15. nativeContact.contacts.ios.visitContacts((name, phoneNumber) => {
  16. callBack(name, phoneNumber);
  17. });
  18. } else {
  19. //真机
  20. nativeContact.contacts.ios.visitAddressBook((name, phoneNumber) => {
  21. callBack(name, phoneNumber);
  22. });
  23. }
  24. break;
  25. case "Android":
  26. // Android通过plus.contacts.getAddressBook可弹出通讯录授权提示框
  27. // nativeContact.contacts.android.visitContacts(function(name, phoneNumber) {
  28. // callBack(name, phoneNumber);
  29. // });
  30. plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, (addressbook) => {
  31. nativeContact.contacts.android.visitContacts((name, phoneNumber) => {
  32. callBack(name, phoneNumber);
  33. });
  34. }, (e) => {
  35. plus.nativeUI.alert("Get address book failed: " + e.message);
  36. });
  37. break;
  38. default:
  39. break;
  40. }
  41. },
  42. ios: { //供iOS系统调用
  43. /**
  44. * 访问通讯录,将获取的联系人信息通过callBack返回
  45. * 仅限模拟器使用(Native.js 的bug)
  46. * @param {Object} callBack回调
  47. */
  48. visitContacts: (callBack) => {
  49. const contactPickerVC = plus.ios.newObject("CNContactPickerViewController");
  50. //实现代理方法【- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact;】
  51. //同时生成遵守CNContactPickerDelegate协议的代理对象delegate
  52. const delegate = plus.ios.implements("CNContactPickerDelegate", {
  53. "contactPicker:didSelectContact:": (picker, contact) => {
  54. //姓名
  55. let name = "";
  56. //姓氏
  57. let familyName = contact.plusGetAttribute("familyName");
  58. //名字
  59. let givenName = contact.plusGetAttribute("givenName");
  60. name = familyName + givenName;
  61. //电话号码
  62. let phoneNo = "";
  63. let phoneNumbers = contact.plusGetAttribute("phoneNumbers");
  64. if (phoneNumbers.plusGetAttribute("count") > 0) {
  65. let phone = phoneNumbers.plusGetAttribute("firstObject");
  66. let phoneNumber = phone.plusGetAttribute("value");
  67. phoneNo = phoneNumber.plusGetAttribute("stringValue");
  68. }
  69. if (callBack) {
  70. callBack(name, phoneNo);
  71. }
  72. }
  73. });
  74. //给通讯录控制器contactPickerVC设置代理
  75. plus.ios.invoke(contactPickerVC, "setDelegate:", delegate);
  76. //获取当前UIWebView视图
  77. const currentWebview = plus.ios.currentWebview();
  78. //根据当前UIWebView视图获取当前控制器
  79. const currentVC = nativeContact.contacts.ios.getViewControllerByView(currentWebview);
  80. //由当前控制器present到通讯录控制器
  81. plus.ios.invoke(currentVC, "presentViewController:animated:completion:", contactPickerVC, true,
  82. null);
  83. },
  84. /**
  85. * 访问通讯录,将获取的联系人信息通过callBack返回
  86. * 仅限真机使用(Native.js 的bug)
  87. * @param {Object} callBack
  88. */
  89. visitAddressBook: (callBack) => {
  90. const peoplePickerNavController = plus.ios.newObject("ABPeoplePickerNavigationController");
  91. //实现代理方法【- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person;】
  92. //同时生成遵守ABPeoplePickerNavigationControllerDelegate协议的代理对象peoplePickerDelegate
  93. const peoplePickerDelegate = plus.ios.implements("ABPeoplePickerNavigationControllerDelegate", {
  94. "peoplePickerNavigationController:didSelectPerson:": (peoplePicker,
  95. person) => {
  96. //这里的peoplePicker竟然是CNContact实例对象,person是undefined
  97. console.log(JSON.stringify(peoplePicker));
  98. console.log(JSON.stringify(person));
  99. console.log(typeof person);
  100. //所以之前的代码不用改
  101. let contact = peoplePicker;
  102. //姓名
  103. let name = "";
  104. //姓氏
  105. let familyName = contact.plusGetAttribute("familyName");
  106. //名字
  107. let givenName = contact.plusGetAttribute("givenName");
  108. name = familyName + givenName;
  109. //电话号码
  110. let phoneNo = "";
  111. let phoneNumbers = contact.plusGetAttribute("phoneNumbers");
  112. if (phoneNumbers.plusGetAttribute("count") > 0) {
  113. let phone = phoneNumbers.plusGetAttribute("firstObject");
  114. let phoneNumber = phone.plusGetAttribute("value");
  115. phoneNo = phoneNumber.plusGetAttribute("stringValue");
  116. }
  117. if (callBack) {
  118. callBack(name, phoneNo);
  119. }
  120. }
  121. });
  122. //给通讯录控制器peoplePickerNavController设置代理
  123. plus.ios.invoke(peoplePickerNavController, "setPeoplePickerDelegate:", peoplePickerDelegate);
  124. //获取当前UIWebView视图
  125. const UIApplicationClass = plus.ios.importClass("UIApplication");
  126. const UIAppObj = UIApplicationClass.sharedApplication();
  127. const del = plus.ios.invoke(UIAppObj, "delegate");
  128. const appWindowObj = plus.ios.invoke(del, "window");
  129. const appRootController = plus.ios.invoke(appWindowObj, "rootViewController");
  130. //由当前控制器present到通讯录控制器
  131. plus.ios.invoke(appRootController, "presentViewController:animated:completion:",
  132. peoplePickerNavController, true,
  133. null);
  134. }
  135. },
  136. android: { //供android系统调用
  137. visitContacts: (callBack) => {
  138. plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, (book) => {
  139. const REQUESTCODE = 1000;
  140. const main = plus.android.runtimeMainActivity();
  141. const Intent = plus.android.importClass('android.content.Intent');
  142. const ContactsContract = plus.android.importClass(
  143. 'android.provider.ContactsContract');
  144. const intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
  145. main.onActivityResult = (requestCode, resultCode, data) => {
  146. if (REQUESTCODE == requestCode) {
  147. let phoneNumber = "";
  148. let resultString = "";
  149. let context = main;
  150. plus.android.importClass(data);
  151. let contactData = data.getData();
  152. let resolver = context.getContentResolver();
  153. plus.android.importClass(resolver);
  154. var cursor = resolver.query(contactData, null, null, null, null);
  155. plus.android.importClass(cursor);
  156. cursor.moveToFirst();
  157. //姓名
  158. let givenName = cursor.getString(cursor.getColumnIndex(ContactsContract
  159. .Contacts.DISPLAY_NAME)) || "";
  160. let contactId = cursor.getString(cursor.getColumnIndex(ContactsContract
  161. .Contacts._ID));
  162. let pCursor = resolver.query(ContactsContract.CommonDataKinds.Phone
  163. .CONTENT_URI, null, ContactsContract.CommonDataKinds
  164. .Phone.CONTACT_ID + " = " + contactId, null, null);
  165. if (pCursor.moveToNext()) {
  166. phoneNumber = pCursor.getString(pCursor.getColumnIndex(
  167. ContactsContract.CommonDataKinds.Phone.NUMBER));
  168. }
  169. if (callBack) {
  170. callBack(givenName, phoneNumber);
  171. }
  172. cursor.close();
  173. pCursor.close();
  174. }
  175. };
  176. main.startActivityForResult(intent, REQUESTCODE);
  177. });
  178. }
  179. }
  180. }
  181. };