Skip to content

Commit

Permalink
update readme and folder structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Duval committed Dec 13, 2021
1 parent 06b3531 commit ba3c4a9
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 153 deletions.
60 changes: 47 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,61 @@
# Unity_iOSCameraPermission
Requestes iOS camera permission with a callback method in Unity.
Request native iOS camera permissions and get a callback method in Unity.

# USAGE
The iOSCameraPermission static class will only function in iOS.
## Installation

Still the class will NOT crash the app on other platforms. It will just do nothing.
Download the latest `ios_camera_permission.unitypackage` file from the *Release* page, drag and drop it in your project folder and click *import*.

The string callbackName argument must match the name of the callback method you want to use.
## Prerequisites

The callback method must require a single string argument.
- You must set a Camera Usage Description in the Player Settings.

The string received will either be "true" or "false".

NOTE: You must set a Camera Usage Description in the Player Settings.
## Usage/Examples
If you want to ask camera permission and display the native iOS popup, call `AskPermission()`.

The method signature is `AskPermission(string gameObjectName, string callbackName)` where `gameObjectName ` is the name of the game object that will receive the callback via the Unity's build-in `SendMessage()` method. `callbackName` is the name of the method to call on the gameObject.

> Note: It is recommended to use `nameof(MyMethod)` in the `callbackName ` parameter to avoid errors when renaming or changing the callbacks.
```csharp
public void AskPermission()
{
iOSCameraPermission.AskPermission(gameObject.name, nameof(AskCallback));
}

void AskCallback(string permissionWasGranted)
{
// permissionWasGranted will either be "true" or "false".
}
```

If you just want to verify the current permission for your app (useful to avoid asking when the permission is already granted), you can call `VerifyPermision()`. The method signature and callbacks are the same as `AskPermission()`.

```csharp
public void VerifyPermission()
{
iOSCameraPermission.VerifyPermission(gameObject.name, nameof(VerifyCallback));
}

void VerifyCallback(string authorizationStatus)
{
// authorizationStatus will be one of the following:
// - Authorized
// - Denied
// - NotDetermined
// - Restricted
}
```

## Plugin directory structure

# Plugin directory structure
All .h and .m files must be in the follow location:

[ProjectName] > Assets > Plugins > iOS
`[ProjectName]/Assets/Plugins/iOS`

# Usage Sample
See Assets > Scenes > SampleScene.unity
## Usage Sample
See `Assets/iOSCameraPermission/Samples/SampleScene.unity`

SampleScene implements the SampleUsage class.
Each one of the two buttons in the scene has a `UsageSample` component and its `OnClick()` callback set to one of the two available methods.

Camera Usage Description is set to "Sample Camera Usage Description"

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,80 +1,80 @@
// Attach this script to the gameobject that will be clicked to call VerifyPermission().
// Ex. Attach to a UI.Button, and set OnClick to VerifyPermission();

using UnityEngine;
using UnityEngine.UI;

public class UsageSample : MonoBehaviour
{
[SerializeField] private Button _button;
[SerializeField] private Text _text;

public void AskPermission()
{
// Display some sort of thinking message to the user.
_text.text = "Asking camera permission...";

// Disable the button while verifying permission.
_button.interactable = false;

// Use native UI to request camera permission.
iOSCameraPermission.VerifyPermission(gameObject.name, nameof(AskCallback));
}

public void VerifyPermission()
{
iOSCameraPermission.VerifyPermission(gameObject.name, nameof(VerifyCallback));
}

void AskCallback(string permissionWasGranted)
{
Debug.Log("Callback.permissionWasGranted = " + permissionWasGranted);

if (permissionWasGranted == "true")
{
// You can now use the device camera.
_text.text = "You can now use the camera";
}
else
{
// You cannot use the device camera. You may want to display a message to the user
// about changing the camera permission in the Settings app.
_text.text = "Please active camera access in Settings.";

// You may want to re-enable the button to display the Settings message again.
_button.interactable = true;
}
}

void VerifyCallback(string authorizationStatus)
{
Debug.Log($"Authorization status: {authorizationStatus}");
_text.text = authorizationStatus;
}
}

// MIT License
//
// Copyright (c) 2021 Wonder Partner's
// www.wonder-partners.com
//
// Copyright (c) 2018 Cory Butler
// www.CoryButler.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Attach this script to the gameobject that will be clicked to call VerifyPermission().
// Ex. Attach to a UI.Button, and set OnClick to VerifyPermission();

using UnityEngine;
using UnityEngine.UI;

public class UsageSample : MonoBehaviour
{
[SerializeField] private Button _button;
[SerializeField] private Text _text;

public void AskPermission()
{
// Display some sort of thinking message to the user.
_text.text = "Asking camera permission...";

// Disable the button while verifying permission.
_button.interactable = false;

// Use native UI to request camera permission.
iOSCameraPermission.AskPermission(gameObject.name, nameof(AskCallback));
}

public void VerifyPermission()
{
iOSCameraPermission.VerifyPermission(gameObject.name, nameof(VerifyCallback));
}

void AskCallback(string permissionWasGranted)
{
Debug.Log("Callback.permissionWasGranted = " + permissionWasGranted);

if (permissionWasGranted == "true")
{
// You can now use the device camera.
_text.text = "You can now use the camera";
}
else
{
// You cannot use the device camera. You may want to display a message to the user
// about changing the camera permission in the Settings app.
_text.text = "Please active camera access in Settings.";

// You may want to re-enable the button to display the Settings message again.
_button.interactable = true;
}
}

void VerifyCallback(string authorizationStatus)
{
Debug.Log($"Authorization status: {authorizationStatus}");
_text.text = authorizationStatus;
}
}

// MIT License
//
// Copyright (c) 2021 Wonder Partner's
// www.wonder-partners.com
//
// Copyright (c) 2018 Cory Butler
// www.CoryButler.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
#if UNITY_IOS && !UNITY_EDITOR
using System.Runtime.InteropServices;
#endif

public static class iOSCameraPermission
{
#if UNITY_IOS && !UNITY_EDITOR
[DllImport("__Internal")]
extern static private void _verifyPermission(string gameObject, string callback);

[DllImport("__Internal")]
extern static private void _verifyPermission(string gameObject, string callback);
#endif

public static void VerifyPermission(string gameObjectName, string callbackName)
{
#if UNITY_IOS && !UNITY_EDITOR
_verifyPermission(gameObjectName, callbackName);
#endif
}

public static void AskPermission(string gameObjectName, string callbackName)
{
#if UNITY_IOS && !UNITY_EDITOR
_askPermission(gameObjectName, callbackName);
#endif
}
}

// MIT License
//
// Copyright (c) 2021 Wonder Partner's
// www.wonder-partners.com
//
// Copyright (c) 2018 Cory Butler
// www.CoryButler.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#if UNITY_IOS && !UNITY_EDITOR
using System.Runtime.InteropServices;
#endif

public static class iOSCameraPermission
{
#if UNITY_IOS && !UNITY_EDITOR
[DllImport("__Internal")]
extern static private void _verifyPermission(string gameObject, string callback);

[DllImport("__Internal")]
extern static private void _verifyPermission(string gameObject, string callback);
#endif

public static void VerifyPermission(string gameObjectName, string callbackName)
{
#if UNITY_IOS && !UNITY_EDITOR
_verifyPermission(gameObjectName, callbackName);
#endif
}

public static void AskPermission(string gameObjectName, string callbackName)
{
#if UNITY_IOS && !UNITY_EDITOR
_askPermission(gameObjectName, callbackName);
#endif
}
}

// MIT License
//
// Copyright (c) 2021 Wonder Partner's
// www.wonder-partners.com
//
// Copyright (c) 2018 Cory Butler
// www.CoryButler.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

0 comments on commit ba3c4a9

Please sign in to comment.