my.addPhoneContact

Version requirements: Basic library 1.10.0 or higher version. If the version is low, suggest Compatible treatment

This form enables the user to write the form into phone contacts via create contacts or add to existing contacts.

Sample Code

copy
// API-DEMO page/API/contact/contact.json
{
   "defaultTitle": "Contact"
}
copy
<!-- 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.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">First 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 the remarks" />
        </view>
      </view>

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

      <view style="font-size:18px;margin-top:18px;margin-bottom:18px">
        <text style="font-size:18px;margin-top:18px;margin-bottom:18px">Contact 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="address country" />
        </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="address state" />
        </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="address city" />
        </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="address street" />
        </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="organization" />
        </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@miniprogram.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.miniprogram.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">Company 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="work country" />
        </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="work state" />
        </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="work city" />
        </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="work street" />
        </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="111111" />
        </view>
      </view>

      <view style="font-size:18px;margin-top:18px;margin-bottom:18px">
        <text style="font-size:18px;margin-top:18px;margin-bottom:18px">Home</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="home country" />
        </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="home state" />
        </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="home city" />
        </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="home street" />
        </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 phone contact</button>

    </view>
  </view>	
</view>
copy
// 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": "This is remarks",
      "mobilePhoneNumber": "13800000000",
      "homePhoneNumber": "11111115",
      "workPhoneNumber": "11111112",
      "homeFaxNumber": "11111114",
      "workFaxNumber": "11111111",
      "hostNumber": "11111113",
      "addressCountry": "address country",
      "addressState": "address state",
      "addressCity": "address city",
      "addressStreet": "address street",
      "addressPostalCode": "94016",
      "workAddressCountry": "work country",
      "workAddressState": "work state",
      "workAddressCity": "work city",
      "workAddressStreet": "work street",
      "workAddressPostalCode": "111111",
      "homeAddressCountry": "home country",
      "homeAddressState": "home state",
      "homeAddressCity": "home city",
      "homeAddressStreet": "home street",
      "homeAddressPostalCode": "123456",
      "organization": "organization",
      "title": "Developer",
      "email": "liuhuo01@miniprogram.com",
      "url": "www.miniprogram.com",
      success: (res) => {
        my.alert({
          content: 'addPhoneContact response: ' + JSON.stringify(res)
        });
      },
      fail: (res) => {
        my.alert({
          content: 'addPhoneContact response: ' + 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 too low',
        content: 'my.addPhoneContact() needs higher version'
      });
    }
  }
});

Parameters

Object type with the following attributes:

PropertyType

Required

Description
photoFilePathStringNoLocal file path of avatar.
nickNameStringNoNickname.
lastNameStringNoSurname.
middleNameStringNoMiddle name.
firstNameStringNoFirst name.
remarkStringNoRemarks.
mobilePhoneNumberStringNoCell number.
addressCountryStringNoCountry in contact address.
addressStateStringNoProvince in contact address.
addressCityStringNoCity in contact address.
addressStreetStringNoStreet in contact address.
addressPostalCodeStringNoPostcode in contact address.
organizationStringNoCompany.
titleStringNoTitle.
workFaxNumberStringNoWork fax.
workPhoneNumberStringNoWork phone number.
hostNumberStringNoCompany phone number.
emailStringNoEmail.
urlStringNoWebsite.
workAddressCountryStringNoCountry in work address.
workAddressStateStringNoProvince in work address.
workAddressCityStringNoCity in work address.
workAddressStreetStringNoStreet in work address.
workAddressPostalCodeStringNoPostcode in work address.
homeFaxNumberStringNoHome fax.
homePhoneNumberStringNoHome phone.
homeAddressCountryStringNoCountry in home address.
homeAddressStateStringNoProvince in home address.
homeAddressCityStringNoCity in home address.
homeAddressStreetStringNoStreet in home address.
homeAddressPostalCodeStringNoPostcode in home address.
successFunctionNoCallback function upon call success.
failFunctionNoCallback function upon call failure.
completeFunctionNoCallback function upon call completion (to be executed upon either call success or failure).

Depending on the support of contact App built in different ROM, the above fields may not support emoji or kaomoji. In such cases, this option is ignored.

Return Value

Success:

success = true

Failure:

ErrorErrorMessageDescription
11fail cancelThe user cancels the operation.
3fail ${detail}Call failure, detail includes the detailed information.