NathanaelA / nativescript-permissions

Wraps up the entire Android 6 permissions system in a easy to use plugin.
MIT License
46 stars 22 forks source link

Does this work with Angular? #15

Closed chief10 closed 8 years ago

chief10 commented 8 years ago

I have started a new project using the tns for an angular project, and I have copied your example code almost verbatim, but when I go to request the permission, it automatically fails without prompting the user at all. Here goes the code:

In app.component.ts:

import {Component, OnInit} from "@angular/core";
var permissions = require('nativescript-permissions');

declare var android: any;

@Component({
    selector: "my-app",
    templateUrl: "app.component.html",
})
export class AppComponent implements OnInit {
    ngOnInit() {
    }

    public counter: number = 16;

    public get message(): string {
        if (this.counter > 0) {
            return this.counter + " taps left";
        } else {
            return "Hoorraaay! \nYou are ready to start building!";
        }
    }

    public onTap() {
        this.counter--;
        this.handleRequestForPermissions();
    }

    public handleRequestForPermissions() {
        permissions.requestPermission(android.Manifest.permission.READ_CONTACTS, "I need these permissions because I'm cool")
            .then(function () {
                console.log("Woo Hoo, I have the power!");
            })
            .catch(function (e) {
                console.log("Uh oh, no permissions - plan B time!",JSON.stringify(e) );
            });
    }
}

And in my android manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="__PACKAGE__"
    android:versionCode="1"
    android:versionName="1.0">

    <supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"/>

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="23"/>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:name="com.tns.NativeScriptApplication"
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity
            android:name="com.tns.NativeScriptActivity"
            android:label="@string/title_activity_kimera"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:theme="@style/LaunchScreenTheme">

            <meta-data android:name="SET_THEME_ON_LAUNCH" android:resource="@style/AppTheme" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.tns.ErrorReportActivity"/>
    </application>
</manifest>

The error message:

JS: Uh oh, no permissions - plan B time! {"android.permission.READ_CONTACTS":false}

Am I missing something obvious here?

NathanaelA commented 8 years ago

Hmm, that is weird -- ng2 should not cause any issues with the permissions system. Is this Android 4.4, 5, 6, or 7?

chief10 commented 8 years ago

You are the shit. Didn't realize that my emulator was running version 7 -- idk why it would be. When I run this on my phone -- version 6 -- it works just fine.

Know what the problem could be with version 7?

Thanks again for helping me out with this. You rock.

NathanaelA commented 8 years ago

Interesting. I haven't had a chance to mess with 7; but my guess is they must have made some more permissions changes; or the compatibility layer that Google uses to allow 4.4 to work are well as 7 doesn't work properly in 7 yet... Guess I need to download a 7 emulator image. :grinning:

chief10 commented 8 years ago

Just noticed something interesting too. After I tried pulling that code into my actual application -- the code that is working and on android 6.0 -- I started getting the same error as before. Only after completely erasing the application off my phone and reinstalling it, was I able to get the prompt so show up. I wonder if at some point I implemented your plugin incorrectly and nativescript saved the value from that instance for future requests?

EDIT: Confirmed. After deleting my app in my emulator -- running Android 7 -- and reinstalling it, this module started working as expected. This seems like more of an issue with nativescript itself, rather than your plugin.

NathanaelA commented 8 years ago

Actually, not an issue with NativeScript -- the OS itself will save your setting -- this is by design; the idea is that the OS asks you and most the time will never re-ask you again either direction... So you need to go to Settings -> Security -> and then ether clear all saved app settings or clear the app itself (depends on OS)