コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
namespace AppName.Services
{
//DependencyServiceから利用する
public interface IPermissionService
{
#region "パーミッションの確認"
//GPSのアクセスが許可されているか確認する
bool AllowedLocationPermission();
//電話のアクセスが許可されているか確認する
bool AllowedPhonePermission();
//連絡先のアクセスが許可されているか確認する
bool AllowedReadContactPermission();
//カレンダーのアクセスが許可されているか確認する
bool AllowedReadCalendarsPermission();
//カメラのアクセスが許可されているか確認する
bool AllowedCameraPermission();
//アルバムのアクセスが許可されているか確認する
bool AllowedReadPhotoLibraryPermission();
#endregion
}
}
using System;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Xamarin.Forms;
using AppName.Services;
using AppName.Droid.Services;
[assembly: Dependency(typeof(PermissionService))]
namespace AppName.Droid.Services
{
public class PermissionService: IPermissionService
{
#region "パーミッションの確認"
//GPSのアクセスが許可されているか確認する
public bool AllowedLocationPermission()
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
{
Context context = Forms.Context;
if (context.CheckSelfPermission(Android.Manifest.Permission.AccessFineLocation) == Permission.Granted)
{
return true;
}
}
else
{
return true;
}
return false;
}
//電話のアクセスが許可されているか確認する
public bool AllowedPhonePermission()
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
{
Context context = Forms.Context;
if (context.CheckSelfPermission(Android.Manifest.Permission.CallPhone) == Permission.Granted)
{
return true;
}
}
else
{
return true;
}
return false;
}
//連絡先のアクセスが許可されているか確認する
public bool AllowedReadContactPermission()
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
{
Context context = Forms.Context;
if (context.CheckSelfPermission(Android.Manifest.Permission.ReadContacts) == Permission.Granted)
{
return true;
}
}
else
{
return true;
}
return false;
}
//カレンダーのアクセスが許可されているか確認する
public bool AllowedReadCalendarsPermission()
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
{
Context context = Forms.Context;
if (context.CheckSelfPermission(Android.Manifest.Permission.ReadCalendar) == Permission.Granted)
{
return true;
}
}
else
{
return true;
}
return false;
}
//カメラのアクセスが許可されているか確認する
public bool AllowedCameraPermission()
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
{
Context context = Forms.Context;
if (context.CheckSelfPermission(Android.Manifest.Permission.Camera) == Permission.Granted)
{
return true;
}
}
else
{
return true;
}
return false;
}
//アルバムのアクセスが許可されているか確認する
public bool AllowedReadPhotoLibraryPermission()
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
{
Context context = Forms.Context;
if (context.CheckSelfPermission(Android.Manifest.Permission.ReadExternalStorage) == Permission.Granted)
{
return true;
}
}
else
{
return true;
}
return false;
}
#endregion
}
}
using UIKit;
using Xamarin.Forms;
using AppName.iOS.Services;
using AppName.Services;
[assembly: Dependency(typeof(PermissionService))]
namespace AppName.iOS.Services
{
public class PermissionService: IPermissionService
{
#region "パーミッションの確認"
//GPSのアクセスが許可されているか確認する
public bool AllowedLocationPermission()
{
if (CoreLocation.CLLocationManager.Status == CoreLocation.CLAuthorizationStatus.Authorized ||
CoreLocation.CLLocationManager.Status == CoreLocation.CLAuthorizationStatus.AuthorizedWhenInUse)
{
return true;
}
return false;
}
//電話のアクセスが許可されているか確認する
public bool AllowedPhonePermission()
{
//対象となるUsageDescriptionが存在しない
return true;
}
//連絡先のアクセスが許可されているか確認する
public bool AllowedReadContactPermission()
{
if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0))
{
if (Contacts.CNContactStore.GetAuthorizationStatus(Contacts.CNEntityType.Contacts) == Contacts.CNAuthorizationStatus.Authorized)
{
return true;
}
}
else
{
if (AddressBook.ABAddressBook.GetAuthorizationStatus() == AddressBook.ABAuthorizationStatus.Authorized)
{
return true;
}
}
return false;
}
//カレンダーのアクセスが許可されているか確認する
public bool AllowedReadCalendarsPermission()
{
if (EventKit.EKEventStore.GetAuthorizationStatus(EventKit.EKEntityType.Event) == EventKit.EKAuthorizationStatus.Authorized)
{
return true;
}
return false;
}
//カメラのアクセスが許可されているか確認する
public bool AllowedCameraPermission()
{
//ビデオの場合
//if (AVFoundation.AVCaptureDevice.GetAuthorizationStatus(AVFoundation.AVMediaType.Video) == AVAuthorizationStatus.Authorized)
//{
// return true;
//}
if (AssetsLibrary.ALAssetsLibrary.AuthorizationStatus == AssetsLibrary.ALAuthorizationStatus.Authorized)
{
return true;
}
return false;
}
//アルバムのアクセスが許可されているか確認する
public bool AllowedReadPhotoLibraryPermission()
{
if (Photos.PHPhotoLibrary.AuthorizationStatus == Photos.PHAuthorizationStatus.Authorized)
{
return true;
}
return false;
}
#endregion
}
}
async void OnConfirmClick(object sender, EventArgs e)
{
if (!DependencyService.Get<IPermissionService>().AllowedLocationPermission())
{
await DisplayAlert("GPSの許可", "アプリケーションにGPSの使用を許可してください。", "OK");
}
if (!DependencyService.Get<IPermissionService>().AllowedPhonePermission())
{
await DisplayAlert("電話の許可", "アプリケーションに電話の使用を許可してください。", "OK");
}
if (!DependencyService.Get<IPermissionService>().AllowedReadContactPermission())
{
await DisplayAlert("連絡帳の許可", "アプリケーションに連絡帳の使用を許可してください。", "OK");
}
if (!DependencyService.Get<IPermissionService>().AllowedReadCalendarsPermission())
{
await DisplayAlert("カレンダーの許可", "アプリケーションにカレンダーの使用を許可してください。", "OK");
}
if (!DependencyService.Get<IPermissionService>().AllowedCameraPermission())
{
await DisplayAlert("カメラの許可", "アプリケーションにカメラの使用を許可してください。", "OK");
}
if (!DependencyService.Get<IPermissionService>().AllowedReadPhotoLibraryPermission())
{
await DisplayAlert("アルバムの許可", "アプリケーションにアルバムの使用を許可してください。", "OK");
}
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。