476rick
476rick

Reputation: 2795

Xamarin Forms request internet permission Android 6 from PCL

I'm building a cross-platform application with Xamarin.Forms. For the content I use an async call to a API which returns JSON. So I need the INTERNET permission.

Example of the code that contacts the API:

var data = await wc.GetStringAsync("http://website.for.content/content-api/Category/?format=json");

This line is in the Android Manifest:

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

All the code I wrote for the application is in a portable class library. So I don't have any platform specific code (yet).

How do I check and request internet permission from the portable class library?

Upvotes: 3

Views: 4085

Answers (3)

ahaliav fox
ahaliav fox

Reputation: 2247

I had this issue with camera permissions trying to use Xzing barcode scanner (but of course it can help in any other permissions) my solution was in my MainActivity : FormsAppCompatActivity OnCreate(Bundle bundle) adding this code:

  if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.M)
        {
            int mycode = 0;
            Android.Support.V4.App.ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.Camera }, mycode);
        }    

 public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
    {            

        ZXing.Net.Mobile.Forms.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }

Upvotes: 0

Zverev Evgeniy
Zverev Evgeniy

Reputation: 3719

Are you sure that what you need to check is the permission? Permission is a static, declarative thing that is required from user BEFORE the user installs the app. App permissions cannot be changed without updating the app and thus explicit acceptance from the user. So the logic behind permissions is that simple: if your app has

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

in its manifest and the app is running then you can be sure that the user had accepted the app requirements.

In the case you target your app for SDK 23 or higher (Android 6.0 or higher) and your app want a dangerous permission, you will need to request permissions in run-time:

// Assume thisActivity is the current activity int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_CALENDAR);

Anyway in runtime you might be willing to check is the availability of internet access which has nothing to do with the permission.

For that have a look here: How to check internet access on Android:

public boolean isOnline() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); return netInfo != null && netInfo.isConnectedOrConnecting(); }

Notice that the code is Android specific and cannot be put into a PCL. So you will need a PCL-declared interface and an Android specific implementation.

Upvotes: 1

SushiHangover
SushiHangover

Reputation: 74174

There are Normal and Dangerous permissions, Internet access is a normal permission and thus only needs to be defined in the application's manifest for installation purposes and is auto-granted at runtime.

Normal permissions cover areas where your app needs to access data or resources outside the app's sandbox, but where there's very little risk to the user's privacy or the operation of other apps. For example, permission to set the time zone is a normal permission. If an app declares that it needs a normal permission, the system automatically grants the permission to the app

Ref: https://developer.android.com/guide/topics/security/normal-permissions.html

Upvotes: 3

Related Questions