All Products
Search
Document Center

Mobile Platform as a Service:Add phone contacts

Last Updated:Nov 10, 2022

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

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

// API-DEMO page/API/contact/contact.json
{
   "defaultTitle": "Contact"
}
<!-- API-DEMO page/API/contact/contact.axml-->
<view class="page">

  <view class="page-description">Contact API</view>

  <view class="page-section">
    <view class="page-section-title">my.choosePhoneContact</view>
    <view class="page-section-demo">
      <button type="primary" onTap="choosePhoneContact">Arouse local directory</button>
    </view>
  </view>

  <view class="page-section">
    <view class="page-section-title">my.chooseAlipayContact</view>
    <view class="page-section-demo">
      <button type="primary" onTap="chooseAlipayContact">Arouse Alipay directory</button>
    </view>
  </view>

  <view class="page-section">
    <view class="page-section-title">my.chooseContact</view>
    <view class="page-section-demo">
      <button type="primary" onTap="chooseContact">Select contacts</button>
    </view>
  </view>

  <view class="page-section">
    <view class="page-section-title">my.addPhoneContact</view>
    <view class="page-section-demo">

      <view style="font-size:18px;margin-top:18px;margin-bottom:18px">
        <text style="font-size:18px;margin-top:18px;margin-bottom:18px">Basic information</text>
      </view>

      <view class="form-row">
        <view class="form-row-label">Nickname</view>
        <view class="form-row-content">
          <input id="nickName" onInput="onInput" class="input" value="Baking July" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Last name</view>
        <view class="form-row-content">
          <input id="lastName" onInput="onInput" class="input" value="Last" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Middle name</view>
        <view class="form-row-content">
          <input id="middleName" onInput="onInput" class="input" value="Middle" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Name</view>
        <view class="form-row-content">
          <input id="firstName" onInput="onInput" class="input" value="First" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Remarks</view>
        <view class="form-row-content">
          <input id="remark" onInput="onInput" class="input" value="This is remarks" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Mobile number</view>
        <view class="form-row-content">
          <input id="mobilePhoneNumber" onInput="onInput" class="input" value="13800000000" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Alipay account</view>
        <view class="form-row-content">
          <input id="alipayAccount" onInput="onInput" class="input" value="alipay@alipay.com" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">WeChat account</view>
        <view class="form-row-content">
          <input id="weChatNumber" onInput="onInput" class="input" value="liuhuo" />
        </view>
      </view>

      <view style="font-size:18px;margin-top:18px;margin-bottom:18px">
        <text style="font-size:18px;margin-top:18px;margin-bottom:18px">Address</text>
      </view>

      <view class="form-row">
        <view class="form-row-label">Country</view>
        <view class="form-row-content">
          <input id="addressCountry" onInput="onInput" class="input" value="US" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Province</view>
        <view class="form-row-content">
          <input id="addressState" onInput="onInput" class="input" value="California" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">City</view>
        <view class="form-row-content">
          <input id="addressCity" onInput="onInput" class="input" value="San Francisco" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Street</view>
        <view class="form-row-content">
          <input id="addressStreet" onInput="onInput" class="input" value="Mountain View" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Postcode</view>
        <view class="form-row-content">
          <input id="addressPostalCode" onInput="onInput" class="input" value="94016" />
        </view>
      </view>

      <view style="font-size:18px;margin-top:18px;margin-bottom:18px">
        <text style="font-size:18px;margin-top:18px;margin-bottom:18px">Work</text>
      </view>

      <view class="form-row">
        <view class="form-row-label">Company</view>
        <view class="form-row-content">
          <input id="organization" onInput="onInput" class="input" value="AntFin" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Title</view>
        <view class="form-row-content">
          <input id="title" onInput="onInput" class="input" value="Developer" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Work fax</view>
        <view class="form-row-content">
          <input id="workFaxNumber" onInput="onInput" class="input" value="11111111" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Work phone</view>
        <view class="form-row-content">
          <input id="workPhoneNumber" onInput="onInput" class="input" value="11111112" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Company phone</view>
        <view class="form-row-content">
          <input id="hostNumber" onInput="onInput" class="input" value="11111113" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Email</view>
        <view class="form-row-content">
          <input id="email" onInput="onInput" class="input" value="liuhuo01@alipay.com" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Website</view>
        <view class="form-row-content">
          <input id="url" onInput="onInput" class="input" value="www.alipay.com" />
        </view>
      </view>

      <view style="font-size:18px;margin-top:18px;margin-bottom:18px">
        <text style="font-size:18px;margin-top:18px;margin-bottom:18px">Work address</text>
      </view>

      <view class="form-row">
        <view class="form-row-label">Country</view>
        <view class="form-row-content">
          <input id="workAddressCountry" onInput="onInput" class="input" value="China" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Province</view>
        <view class="form-row-content">
          <input id="workAddressState" onInput="onInput" class="input" value="Zhejiang" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">City</view>
        <view class="form-row-content">
          <input id="workAddressCity" onInput="onInput" class="input" value="Hangzhou" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Street</view>
        <view class="form-row-content">
          <input id="workAddressStreet" onInput="onInput" class="input" value="Tianmushan Road" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Postcode</view>
        <view class="form-row-content">
          <input id="workAddressPostalCode" onInput="onInput" class="input" value="361005" />
        </view>
      </view>

      <view style="font-size:18px;margin-top:18px;margin-bottom:18px">
        <text style="font-size:18px;margin-top:18px;margin-bottom:18px">Residence</text>
      </view>

      <view class="form-row">
        <view class="form-row-label">Fax</view>
        <view class="form-row-content">
          <input id="homeFaxNumber" onInput="onInput" class="input" value="11111114" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Phone</view>
        <view class="form-row-content">
          <input id="homePhoneNumber" onInput="onInput" class="input" value="11111115" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Country</view>
        <view class="form-row-content">
          <input id="homeAddressCountry" onInput="onInput" class="input" value="Canada" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Province</view>
        <view class="form-row-content">
          <input id="homeAddressState" onInput="onInput" class="input" value="Ontario" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">City</view>
        <view class="form-row-content">
          <input id="homeAddressCity" onInput="onInput" class="input" value="Toronto" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Street</view>
        <view class="form-row-content">
          <input id="homeAddressStreet" onInput="onInput" class="input" value="No.234 Road" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Postcode</view>
        <view class="form-row-content">
          <input id="homeAddressPostalCode" onInput="onInput" class="input" value="123456" />
        </view>
      </view>

      <button type="primary" onTap="addPhoneContact">Add to Contacts</button>

    </view>
  </view>

</view>
// API-DEMO page/API/contact/contact.js
Page({
  data:{
      "photoFilePath": "/sdcard/DCIM/Camera/a.jpg",
      "nickName": "Baking July",
      "lastName": "Last",
      "middleName": "Middle",
      "firstName": "First",
      "remark": "Remarks",
      "mobilePhoneNumber": "13800000000",
      "homePhoneNumber": "11111115",
      "workPhoneNumber": "11111112",
      "homeFaxNumber": "11111114",
      "workFaxNumber": "11111111",
      "hostNumber": "11111113",
      "weChatNumber": "liuhuo",
      "alipayAccount": "alipay@alipay.com",
      "addressCountry": "US",
      "addressState": "California",
      "addressCity": "San Francisco",
      "addressStreet": "Mountain View",
      "addressPostalCode": "94016",
      "workAddressCountry": "China",
      "workAddressState": "Zhejiang",
      "workAddressCity": "Hangzhou",
      "workAddressStreet": "Tianmushan Road",
      "workAddressPostalCode": "361005",
      "homeAddressCountry": "Canada",
      "homeAddressState": "Ontairo",
      "homeAddressCity": "Toronto",
      "homeAddressStreet": "No.234 Road",
      "homeAddressPostalCode": "123456",
      "organization": "AntFin",
      "title": "Developer",
      "email": "liuhuo01@alipay.com",
      "url": "www.alipay.com",
      success: (res) => {
        my.alert({
          content: 'addPhoneContact response: ' + JSON.stringify(res)
        });
      },
      fail: (res) => {
        my.alert({
          content: 'addPhoneContact response: ' + JSON.stringify(res)
        });
      }
  },
  choosePhoneContact() {
    my.choosePhoneContact({
      success: (res) => {
        my.alert({
          content: 'choosePhoneContact response: ' + JSON.stringify(res)
        });
      },
      fail: (res) => {
        my.alert({
          content: 'choosePhoneContact response: ' + JSON.stringify(res)
        });
      },
    });
  },
  chooseAlipayContact() {
    my.chooseAlipayContact({
      count: 2,
      success: (res) => {
        my.alert({
          content: 'chooseAlipayContact response: ' + JSON.stringify(res)
        });
      },
      fail: (res) => {
        my.alert({
          content: 'chooseAlipayContact response: ' + JSON.stringify(res)
        });
      },
    });
  },
  chooseContact() {
    my.chooseContact({
      chooseType: 'multi', // multiple choices
      includeMe: true,     // Include 
      includeMobileContactMode: 'known',//Only include the two-way contacts, that is, the contacts who have each other's phone number in their directories
      multiChooseMax: 3,  // Three contacts at most
      multiChooseMaxTips: 'Exceed the maximum number of contacts allowed',
      success: (res) => {
        my.alert({
          content: 'chooseContact : ' + JSON.stringify(res)
        });
      },
      fail: (res) => {
        my.alert({
          content: 'chooseContact : ' + JSON.stringify(res)
        });
      },
    });
  },
  onInput(e) {
    this.data[e.currentTarget.id] = e.detail.value;
  },
  addPhoneContact() {
    if (my.canIUse('addPhoneContact')) {
      my.addPhoneContact(this.data);
    } else {
      my.alert({ 
        title: 'Client version is too low',
        content: 'my.addPhoneContact() requires 10.1.32 or later version'
      });
    }
  }
});