Skip to content

Instantly share code, notes, and snippets.

@dyazincahya
Last active June 14, 2024 11:59
Show Gist options
  • Save dyazincahya/38c96572ace16e8cdeb011e7523c1e2d to your computer and use it in GitHub Desktop.
Save dyazincahya/38c96572ace16e8cdeb011e7523c1e2d to your computer and use it in GitHub Desktop.
Save Contact to Phonebook Utils for Nativescript 8 (core JS) (Android or IOS)

Dependency

You need Utils, isAndroid, isIOS from @nativescript/core. This util does not require contact permissions, because the way this util works is only sending data to the default contact app.

Usage

Import

import { saveToPhoneBook } from "~/util_phonebook";

Example

import { saveToPhoneBook } from "~/util_phonebook";

let contact = {
    name: "Kang Cahya",
    phoneNumber: "628123456789",
    email: "[email protected]",
    address: "123 Main St, Indonesia",
    company: "ABC Company",
    jobTitle: "Software Engineer",
    notes: "This is a sample note.",
    website: "https://kang-cahya.com",
  };
saveToPhoneBook(contact);
import {
Utils,
isAndroid,
isIOS,
} from "@nativescript/core";
export function saveToPhoneBook(contact) {
if (isAndroid) {
const uri = android.net.Uri.parse("content://contacts/people/");
const intent = new android.content.Intent(
android.content.Intent.ACTION_INSERT,
uri
);
intent.setType(android.provider.ContactsContract.Contacts.CONTENT_TYPE);
// Basic Information
intent.putExtra(android.provider.ContactsContract.Intents.Insert.NAME, contact.name);
intent.putExtra(android.provider.ContactsContract.Intents.Insert.PHONE, contact.phoneNumber);
intent.putExtra(android.provider.ContactsContract.Intents.Insert.EMAIL, contact.email);
// Address Information
intent.putExtra(android.provider.ContactsContract.Intents.Insert.POSTAL, contact.address);
// Organization
intent.putExtra(android.provider.ContactsContract.Intents.Insert.COMPANY, contact.company);
intent.putExtra(android.provider.ContactsContract.Intents.Insert.JOB_TITLE, contact.jobTitle);
// Additional Information
intent.putExtra(android.provider.ContactsContract.Intents.Insert.NOTES, contact.notes);
intent.putExtra(android.provider.ContactsContract.Intents.Insert.WEBSITE, contact.website);
Utils.android.getCurrentActivity().startActivity(intent);
} else if (isIOS) {
const url = `telprompt:${contact.phoneNumber}`;
Utils.ios.openURL(url);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment