0

I updated my project to NativeScript 6 and also updated Android Studio. Now, on both emulators and real devices, I get permission errors when I invoke telephony():

JS: nativeException: java.lang.SecurityException: getLine1NumberForDisplay: Neither user 10089 nor current process has android.permission.READ_PHONE_STATE, android.permission.READ_SMS, or android.permission.READ_PHONE_NUMBERS

Adding the permissions to my own AndroidManifest.xml makes no difference. And, Google Play has a recent policy that restricts READ_SMS to apps that can act as the default text app.

The code in question is in the nativescript-telephony plugin, and I've added an issue to that repository, tho it has not been active for some time.

David
  • 578
  • 3
  • 16
  • 1
    Possible duplicate of [Android marshmallow request permission?](https://stackoverflow.com/questions/33666071/android-marshmallow-request-permission) – Johann67 Sep 03 '19 at 14:44
  • You may need to perform a clean build to get the updated Android manifest to be bundled into the app. – Jamie Birch Sep 03 '19 at 15:06
  • READ_SMS & READ_PHONE_STATE are runtime permissions, you should use nativescript-permisisons to acquire permissions before you access the apis. – Manoj Sep 03 '19 at 18:16

1 Answers1

1

Try to first install nativescript-contacts and nativescript-permissions plugin for get contact list with set permission for contacts read, edit and add permission.

Set permission in App_Resources->Android->src->main->Androidmanifest.xml File

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

contact.component.html:-

<ListView row="0" [items]="contactList" separatorColor="transparent">
            <ng-template let-contact="item" let-i="index">
                <GridLayout rows="*, auto" columns="auto, *, auto">
                    <Image col="0" *ngIf="contact && contact.photo" class="site-template-image" stretch="aspectFill" [src]="contact ? contact.photo : '~/app/images/avatar.jpg'"></Image>
                    <StackLayout col="0" class="site-template-first-icon" verticalAlignment="center">
                        <Label col="0" *ngIf="contact && contact.photo == null" horizontalAlignment="center" fontSize="24" color="#fff" [text]="contact && contact.name.displayname ? contact.name.displayname.charAt(0) : 'N/A'"></Label>
                    </StackLayout>

                    <StackLayout col="1" verticalAlignment="center">
                        <Label class="site-template-title" [text]="contact ? contact.name.displayname:'No data'"></Label>

                        <WrapLayout orientation="horizontal" *ngIf="contact && contact.phoneNumbers.length > 0">
                            <Label class="site-template-description" *ngFor="let phoneNumber of contact.phoneNumbers" text="{{phoneNumber.value + '  '}}"></Label>
                        </WrapLayout>
                    </StackLayout>
                    <StackLayout margin="0,16,0,35" row="1" colSpan="2" backgroundColor="lightGray" height="0.3"></StackLayout>
                </GridLayout>
            </ng-template>
</ListView>

contact.component.ts

import * as contacts from "nativescript-contacts";
import * as permissions from "nativescript-permissions";

declare var android;

@Component({
  selector: "Contact",
  moduleId: module.id,
  templateUrl: "./contact.component.html",
  styleUrls: ["./contact.component.css"]
})
export class HomeComponent implements OnInit {

  public contactList: any;

  constructor() { }

  ngOnInit(): void {
    // Init your component properties here.
    this.getContactList();
  }

  getContactList() {
    permissions.requestPermissions([android.Manifest.permission.GET_ACCOUNTS, android.Manifest.permission.READ_CONTACTS, android.Manifest.permission.WRITE_CONTACTS], "I need these permissions.")
        .then(() => {
            let contactFields = ["name", "phoneNumbers"];
            contacts.getAllContacts(contactFields).then(
                (data) => {
                    if (data.data.length > 0) {
                        this.contactList = data.data;

                    }
                }, (err) => {
                    console.log("Error: " + err);
                    this.isLoading = false;
                }
            );
        });
    }

}
FrontEnd-er
  • 661
  • 4
  • 13