All Products
Search
Document Center

Add phone contacts

Last Updated: Feb 19, 2021

my.addPhoneContact()

Note:
  • This interface is supported since basic library version 1.10.0. Compatibility processing is required for earlier versions. See Mini Program base library to learn more.
  • This interface is only supported in mPaaS 10.1.60 and later versions.

This interface enables users to write the form into mobile contacts via creating contacts or adding to existing contacts.

Parameters

Parameter Type Required Description
photoFilePath String No Local file path of avatar.
nickName String No Nickname
lastName String No Surname
middleName String No Middle name
firstName String No First name
remark String No Remarks
mobilePhoneNumber String No Mobile phone number
alipayAccount String No Alipay account
addressCountry String No Country in contact address
addressState String No Province in contact address
addressCity String No City in contact address
addressStreet String No Street in contact address
addressPostalCode String No Postcode in contact address
organization String No Company
title String No Title
workFaxNumber String No Work fax number
workPhoneNumber String No Work phone number
hostNumber String No Company phone number
email String No Email
url String No Website
workAddressCountry String No Country in work address
workAddressState String No Province in work address
workAddressCity String No City in work address
workAddressStreet String No Street in work address
workAddressPostalCode String No Postcode in work address
homeFaxNumber String No Residential fax number
homePhoneNumber String No Residential phone number
homeAddressCountry String No Country in residential address
homeAddressState String No Province in residential address
homeAddressCity String No City in residential address
homeAddressStreet String No Street in residential address
homeAddressPostalCode String No Postcode in residential address
success Function No Callback function upon call success.
fail Function No Callback function upon call failure.
complete Function No Callback function upon call completion (to be executed upon either call success or failure).

The support for above fields of App contacts varies by mobile phone models. Some mobile phones may not support emoji or kaomoji. In such cases, the corresponding option is ignored.

Return value

Success: addPhoneContact response:{"success":true}

Error code

Error code Error message Description Solution
3 fail ${detail} Call failed. detail includes the detailed information. -
11 fail cancel The user canceled the operation. This is normal user interaction, and you don’t have to take any action.

Sample code

 
  1. // API-DEMO page/API/contact/contact.json
  2. {
  3. "defaultTitle": "Contact"
  4. }
 
  1. <!-- API-DEMO page/API/contact/contact.axml-->
  2. <view class="page">
  3. <view class="page-description">Contact API</view>
  4. <view class="page-section">
  5. <view class="page-section-title">my.choosePhoneContact</view>
  6. <view class="page-section-demo">
  7. <button type="primary" onTap="choosePhoneContact">Arouse local directory</button>
  8. </view>
  9. </view>
  10. <view class="page-section">
  11. <view class="page-section-title">my.chooseAlipayContact</view>
  12. <view class="page-section-demo">
  13. <button type="primary" onTap="chooseAlipayContact">Arouse Alipay directory</button>
  14. </view>
  15. </view>
  16. <view class="page-section">
  17. <view class="page-section-title">my.chooseContact</view>
  18. <view class="page-section-demo">
  19. <button type="primary" onTap="chooseContact">Select contacts</button>
  20. </view>
  21. </view>
  22. <view class="page-section">
  23. <view class="page-section-title">my.addPhoneContact</view>
  24. <view class="page-section-demo">
  25. <view style="font-size:18px;margin-top:18px;margin-bottom:18px">
  26. <text style="font-size:18px;margin-top:18px;margin-bottom:18px">Basic information</text>
  27. </view>
  28. <view class="form-row">
  29. <view class="form-row-label">Nickname</view>
  30. <view class="form-row-content">
  31. <input id="nickName" onInput="onInput" class="input" value="Baking July" />
  32. </view>
  33. </view>
  34. <view class="form-row">
  35. <view class="form-row-label">Last name</view>
  36. <view class="form-row-content">
  37. <input id="lastName" onInput="onInput" class="input" value="Last" />
  38. </view>
  39. </view>
  40. <view class="form-row">
  41. <view class="form-row-label">Middle name</view>
  42. <view class="form-row-content">
  43. <input id="middleName" onInput="onInput" class="input" value="Middle" />
  44. </view>
  45. </view>
  46. <view class="form-row">
  47. <view class="form-row-label">Name</view>
  48. <view class="form-row-content">
  49. <input id="firstName" onInput="onInput" class="input" value="First" />
  50. </view>
  51. </view>
  52. <view class="form-row">
  53. <view class="form-row-label">Remarks</view>
  54. <view class="form-row-content">
  55. <input id="remark" onInput="onInput" class="input" value="This is remarks" />
  56. </view>
  57. </view>
  58. <view class="form-row">
  59. <view class="form-row-label">Mobile number</view>
  60. <view class="form-row-content">
  61. <input id="mobilePhoneNumber" onInput="onInput" class="input" value="13800000000" />
  62. </view>
  63. </view>
  64. <view class="form-row">
  65. <view class="form-row-label">Alipay account</view>
  66. <view class="form-row-content">
  67. <input id="alipayAccount" onInput="onInput" class="input" value="alipay@alipay.com" />
  68. </view>
  69. </view>
  70. <view class="form-row">
  71. <view class="form-row-label">WeChat account</view>
  72. <view class="form-row-content">
  73. <input id="weChatNumber" onInput="onInput" class="input" value="liuhuo" />
  74. </view>
  75. </view>
  76. <view style="font-size:18px;margin-top:18px;margin-bottom:18px">
  77. <text style="font-size:18px;margin-top:18px;margin-bottom:18px">Address</text>
  78. </view>
  79. <view class="form-row">
  80. <view class="form-row-label">Country</view>
  81. <view class="form-row-content">
  82. <input id="addressCountry" onInput="onInput" class="input" value="US" />
  83. </view>
  84. </view>
  85. <view class="form-row">
  86. <view class="form-row-label">Province</view>
  87. <view class="form-row-content">
  88. <input id="addressState" onInput="onInput" class="input" value="California" />
  89. </view>
  90. </view>
  91. <view class="form-row">
  92. <view class="form-row-label">City</view>
  93. <view class="form-row-content">
  94. <input id="addressCity" onInput="onInput" class="input" value="San Francisco" />
  95. </view>
  96. </view>
  97. <view class="form-row">
  98. <view class="form-row-label">Street</view>
  99. <view class="form-row-content">
  100. <input id="addressStreet" onInput="onInput" class="input" value="Mountain View" />
  101. </view>
  102. </view>
  103. <view class="form-row">
  104. <view class="form-row-label">Postcode</view>
  105. <view class="form-row-content">
  106. <input id="addressPostalCode" onInput="onInput" class="input" value="94016" />
  107. </view>
  108. </view>
  109. <view style="font-size:18px;margin-top:18px;margin-bottom:18px">
  110. <text style="font-size:18px;margin-top:18px;margin-bottom:18px">Work</text>
  111. </view>
  112. <view class="form-row">
  113. <view class="form-row-label">Company</view>
  114. <view class="form-row-content">
  115. <input id="organization" onInput="onInput" class="input" value="AntFin" />
  116. </view>
  117. </view>
  118. <view class="form-row">
  119. <view class="form-row-label">Title</view>
  120. <view class="form-row-content">
  121. <input id="title" onInput="onInput" class="input" value="Developer" />
  122. </view>
  123. </view>
  124. <view class="form-row">
  125. <view class="form-row-label">Work fax</view>
  126. <view class="form-row-content">
  127. <input id="workFaxNumber" onInput="onInput" class="input" value="11111111" />
  128. </view>
  129. </view>
  130. <view class="form-row">
  131. <view class="form-row-label">Work phone</view>
  132. <view class="form-row-content">
  133. <input id="workPhoneNumber" onInput="onInput" class="input" value="11111112" />
  134. </view>
  135. </view>
  136. <view class="form-row">
  137. <view class="form-row-label">Company phone</view>
  138. <view class="form-row-content">
  139. <input id="hostNumber" onInput="onInput" class="input" value="11111113" />
  140. </view>
  141. </view>
  142. <view class="form-row">
  143. <view class="form-row-label">Email</view>
  144. <view class="form-row-content">
  145. <input id="email" onInput="onInput" class="input" value="liuhuo01@sina.com" />
  146. </view>
  147. </view>
  148. <view class="form-row">
  149. <view class="form-row-label">Website</view>
  150. <view class="form-row-content">
  151. <input id="url" onInput="onInput" class="input" value="www.alipay.com" />
  152. </view>
  153. </view>
  154. <view style="font-size:18px;margin-top:18px;margin-bottom:18px">
  155. <text style="font-size:18px;margin-top:18px;margin-bottom:18px">Work address</text>
  156. </view>
  157. <view class="form-row">
  158. <view class="form-row-label">Country</view>
  159. <view class="form-row-content">
  160. <input id="workAddressCountry" onInput="onInput" class="input" value="China" />
  161. </view>
  162. </view>
  163. <view class="form-row">
  164. <view class="form-row-label">Province</view>
  165. <view class="form-row-content">
  166. <input id="workAddressState" onInput="onInput" class="input" value="Zhejiang" />
  167. </view>
  168. </view>
  169. <view class="form-row">
  170. <view class="form-row-label">City</view>
  171. <view class="form-row-content">
  172. <input id="workAddressCity" onInput="onInput" class="input" value="Hangzhou" />
  173. </view>
  174. </view>
  175. <view class="form-row">
  176. <view class="form-row-label">Street</view>
  177. <view class="form-row-content">
  178. <input id="workAddressStreet" onInput="onInput" class="input" value="Tianmushan Road" />
  179. </view>
  180. </view>
  181. <view class="form-row">
  182. <view class="form-row-label">Postcode</view>
  183. <view class="form-row-content">
  184. <input id="workAddressPostalCode" onInput="onInput" class="input" value="361005" />
  185. </view>
  186. </view>
  187. <view style="font-size:18px;margin-top:18px;margin-bottom:18px">
  188. <text style="font-size:18px;margin-top:18px;margin-bottom:18px">Residence</text>
  189. </view>
  190. <view class="form-row">
  191. <view class="form-row-label">Fax</view>
  192. <view class="form-row-content">
  193. <input id="homeFaxNumber" onInput="onInput" class="input" value="11111114" />
  194. </view>
  195. </view>
  196. <view class="form-row">
  197. <view class="form-row-label">Phone</view>
  198. <view class="form-row-content">
  199. <input id="homePhoneNumber" onInput="onInput" class="input" value="11111115" />
  200. </view>
  201. </view>
  202. <view class="form-row">
  203. <view class="form-row-label">Country</view>
  204. <view class="form-row-content">
  205. <input id="homeAddressCountry" onInput="onInput" class="input" value="Canada" />
  206. </view>
  207. </view>
  208. <view class="form-row">
  209. <view class="form-row-label">Province</view>
  210. <view class="form-row-content">
  211. <input id="homeAddressState" onInput="onInput" class="input" value="Ontario" />
  212. </view>
  213. </view>
  214. <view class="form-row">
  215. <view class="form-row-label">City</view>
  216. <view class="form-row-content">
  217. <input id="homeAddressCity" onInput="onInput" class="input" value="Toronto" />
  218. </view>
  219. </view>
  220. <view class="form-row">
  221. <view class="form-row-label">Street</view>
  222. <view class="form-row-content">
  223. <input id="homeAddressStreet" onInput="onInput" class="input" value="No.234 Road" />
  224. </view>
  225. </view>
  226. <view class="form-row">
  227. <view class="form-row-label">Postcode</view>
  228. <view class="form-row-content">
  229. <input id="homeAddressPostalCode" onInput="onInput" class="input" value="123456" />
  230. </view>
  231. </view>
  232. <button type="primary" onTap="addPhoneContact">Add to Contacts</button>
  233. </view>
  234. </view>
  235. </view>
 
  1. // API-DEMO page/API/contact/contact.js
  2. Page({
  3. data:{
  4. "photoFilePath": "/sdcard/DCIM/Camera/a.jpg",
  5. "nickName": "Baking July",
  6. "lastName": "Last",
  7. "middleName": "Middle",
  8. "firstName": "First",
  9. "remark": "Remarks",
  10. "mobilePhoneNumber": "13800000000",
  11. "homePhoneNumber": "11111115",
  12. "workPhoneNumber": "11111112",
  13. "homeFaxNumber": "11111114",
  14. "workFaxNumber": "11111111",
  15. "hostNumber": "11111113",
  16. "weChatNumber": "liuhuo",
  17. "alipayAccount": "alipay@alipay.com",
  18. "addressCountry": "US",
  19. "addressState": "California",
  20. "addressCity": "San Francisco",
  21. "addressStreet": "Mountain View",
  22. "addressPostalCode": "94016",
  23. "workAddressCountry": "China",
  24. "workAddressState": "Zhejiang",
  25. "workAddressCity": "Hangzhou",
  26. "workAddressStreet": "Tianmushan Road",
  27. "workAddressPostalCode": "361005",
  28. "homeAddressCountry": "Canada",
  29. "homeAddressState": "Ontairo",
  30. "homeAddressCity": "Toronto",
  31. "homeAddressStreet": "No.234 Road",
  32. "homeAddressPostalCode": "123456",
  33. "organization": "AntFin",
  34. "title": "Developer",
  35. "email": "liuhuo01@sina.com",
  36. "url": "www.alipay.com",
  37. success: (res) => {
  38. my.alert({
  39. content: 'addPhoneContact response: ' + JSON.stringify(res)
  40. });
  41. },
  42. fail: (res) => {
  43. my.alert({
  44. content: 'addPhoneContact response: ' + JSON.stringify(res)
  45. });
  46. }
  47. },
  48. choosePhoneContact() {
  49. my.choosePhoneContact({
  50. success: (res) => {
  51. my.alert({
  52. content: 'choosePhoneContact response: ' + JSON.stringify(res)
  53. });
  54. },
  55. fail: (res) => {
  56. my.alert({
  57. content: 'choosePhoneContact response: ' + JSON.stringify(res)
  58. });
  59. },
  60. });
  61. },
  62. chooseAlipayContact() {
  63. my.chooseAlipayContact({
  64. count: 2,
  65. success: (res) => {
  66. my.alert({
  67. content: 'chooseAlipayContact response: ' + JSON.stringify(res)
  68. });
  69. },
  70. fail: (res) => {
  71. my.alert({
  72. content: 'chooseAlipayContact response: ' + JSON.stringify(res)
  73. });
  74. },
  75. });
  76. },
  77. chooseContact() {
  78. my.chooseContact({
  79. chooseType: 'multi', // multiple choices
  80. includeMe: true, // Include
  81. includeMobileContactMode: 'known',//Only include the two-way contacts, that is, the contacts who have each other's phone number in their directories
  82. multiChooseMax: 3, // Three contacts at most
  83. multiChooseMaxTips: 'Excceed the maximum number of contacts allowed',
  84. success: (res) => {
  85. my.alert({
  86. content: 'chooseContact : ' + JSON.stringify(res)
  87. });
  88. },
  89. fail: (res) => {
  90. my.alert({
  91. content: 'chooseContact : ' + JSON.stringify(res)
  92. });
  93. },
  94. });
  95. },
  96. onInput(e) {
  97. this.data[e.currentTarget.id] = e.detail.value;
  98. },
  99. addPhoneContact() {
  100. if (my.canIUse('addPhoneContact')) {
  101. my.addPhoneContact(this.data);
  102. } else {
  103. my.alert({
  104. title: 'Client version is too low',
  105. content: 'my.addPhoneContact() requires 10.1.32 or later version'
  106. });
  107. }
  108. }
  109. });