Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: CoryButler/Unity_iOSCameraPermission
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: wonder-partners/Unity_iOSCameraPermission
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 6 commits
  • 23 files changed
  • 1 contributor

Commits on Dec 13, 2021

  1. update Unity version to 2019.4.30f1, add .gitignore

    Samuel Duval committed Dec 13, 2021
    Copy the full SHA
    837dab1 View commit details
  2. refactor methods verifyPermission becomes askPermission. add verifyPe…

    …rmission, add new licence, add new examples
    Samuel Duval committed Dec 13, 2021
    Copy the full SHA
    cd482ca View commit details
  3. update MIT licence

    Samuel Duval committed Dec 13, 2021
    Copy the full SHA
    06b3531 View commit details
  4. update readme and folder structure

    Samuel Duval committed Dec 13, 2021
    Copy the full SHA
    ba3c4a9 View commit details
  5. change name to avoid violating naming rules, add asmdef file for comp…

    …atibility with custom packages
    Samuel Duval committed Dec 13, 2021
    Copy the full SHA
    1d1a0ee View commit details
  6. fix asmdef and dll method name

    Samuel Duval committed Dec 13, 2021
    Copy the full SHA
    93cb998 View commit details
Showing with 1,110 additions and 220 deletions.
  1. +1 −0 LICENSE
  2. +47 −13 README.md
  3. +75 −0 iOSCameraPermissionSampleProject/.gitignore
  4. +15 −3 iOSCameraPermissionSampleProject/Assets/Plugins/iOS/PermissionProvider.m
  5. +4 −0 iOSCameraPermissionSampleProject/Assets/Plugins/iOS/PermissionProviderHelper.h
  6. +32 −7 iOSCameraPermissionSampleProject/Assets/Plugins/iOS/PermissionProviderHelper.m
  7. +1 −3 iOSCameraPermissionSampleProject/Assets/{Scenes.meta → iOSCameraPermission.meta}
  8. +1 −3 iOSCameraPermissionSampleProject/Assets/{Scripts.meta → iOSCameraPermission/Samples.meta}
  9. +342 −77 iOSCameraPermissionSampleProject/Assets/{Scenes → iOSCameraPermission/Samples}/SampleScene.unity
  10. 0 ...ameraPermissionSampleProject/Assets/{Scenes → iOSCameraPermission/Samples}/SampleScene.unity.meta
  11. +80 −71 iOSCameraPermissionSampleProject/Assets/{Scripts → iOSCameraPermission/Samples}/UsageSample.cs
  12. 0 iOSCameraPermissionSampleProject/Assets/{Scripts → iOSCameraPermission/Samples}/UsageSample.cs.meta
  13. +13 −0 iOSCameraPermissionSampleProject/Assets/iOSCameraPermission/Unity.IOSCameraPermission.asmdef
  14. +7 −0 iOSCameraPermissionSampleProject/Assets/iOSCameraPermission/Unity.IOSCameraPermission.asmdef.meta
  15. +54 −42 iOSCameraPermissionSampleProject/Assets/{Scripts → iOSCameraPermission}/iOSCameraPermission.cs
  16. 0 iOSCameraPermissionSampleProject/Assets/{Scripts → iOSCameraPermission}/iOSCameraPermission.cs.meta
  17. +43 −0 iOSCameraPermissionSampleProject/Packages/manifest.json
  18. +326 −0 iOSCameraPermissionSampleProject/Packages/packages-lock.json
  19. +38 −0 iOSCameraPermissionSampleProject/ProjectSettings/PackageManagerSettings.asset
  20. +7 −0 iOSCameraPermissionSampleProject/ProjectSettings/PresetManager.asset
  21. +2 −1 iOSCameraPermissionSampleProject/ProjectSettings/ProjectVersion.txt
  22. +12 −0 iOSCameraPermissionSampleProject/ProjectSettings/VFXManager.asset
  23. +10 −0 iOSCameraPermissionSampleProject/ProjectSettings/XRSettings.asset
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
MIT License

Copyright (c) 2021 Wonder Partner's
Copyright (c) 2018 Cory Butler

Permission is hereby granted, free of charge, to any person obtaining a copy
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"
75 changes: 75 additions & 0 deletions iOSCameraPermissionSampleProject/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# This .gitignore file should be placed at the root of your Unity project directory
#
# Get latest from https://github.com/github/gitignore/blob/main/Unity.gitignore
#
/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/[Ll]ogs/
/[Uu]ser[Ss]ettings/

# MemoryCaptures can get excessive in size.
# They also could contain extremely sensitive data
/[Mm]emoryCaptures/

# Recordings can get excessive in size
/[Rr]ecordings/

# Uncomment this line if you wish to ignore the asset store tools plugin
# /[Aa]ssets/AssetStoreTools*

# Autogenerated Jetbrains Rider plugin
/[Aa]ssets/Plugins/Editor/JetBrains*

# Visual Studio cache directory
.vs/

# Gradle cache directory
.gradle/

# Autogenerated VS/MD/Consulo solution and project files
ExportedObj/
.consulo/
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
*.svd
*.pdb
*.mdb
*.opendb
*.VC.db

# Unity3D generated meta files
*.pidb.meta
*.pdb.meta
*.mdb.meta

# Unity3D generated file on crash reports
sysinfo.txt

# Builds
*.apk
*.aab
*.unitypackage
*.app

# Crashlytics generated file
crashlytics-build.properties

# Packed Addressables
/[Aa]ssets/[Aa]ddressable[Aa]ssets[Dd]ata/*/*.bin*

# Temporary auto-generated Android Assets
/[Aa]ssets/[Ss]treamingAssets/aa.meta
/[Aa]ssets/[Ss]treamingAssets/aa/*

# VS Code
/.vscode
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
#import "PermissionProviderHelper.h"

extern void _verifyPermission(const char* gameObject, const char* callback)
{
NSString *NSGameObject = [[NSString alloc] initWithUTF8String:gameObject];
NSString *NSCallback = [[NSString alloc] initWithUTF8String:callback];
{
NSString *NSGameObject = [[NSString alloc] initWithUTF8String:gameObject];
NSString *NSCallback = [[NSString alloc] initWithUTF8String:callback];

PermissionProviderHelper* permissionProviderHelper = [[PermissionProviderHelper alloc] init];
[permissionProviderHelper verifyPermission:NSGameObject withCallback:NSCallback];
}

extern void _askPermission(const char* gameObject, const char* callback)
{
NSString *NSGameObject = [[NSString alloc] initWithUTF8String:gameObject];
NSString *NSCallback = [[NSString alloc] initWithUTF8String:callback];

PermissionProviderHelper* permissionProviderHelper = [[PermissionProviderHelper alloc] init];
[permissionProviderHelper askPermission:NSGameObject withCallback:NSCallback];
}

// MIT License
//
// Copyright (c) 2021 Wonder Partner's
// www.wonder-partners.com
//
// Copyright (c) 2018 Cory Butler
// www.CoryButler.com
//
Original file line number Diff line number Diff line change
@@ -5,11 +5,15 @@
@interface PermissionProviderHelper : NSObject {}

- (void) verifyPermission:(NSString *)gameObject withCallback:(NSString *)callback;
- (void) askPermission:(NSString *)gameObject withCallback:(NSString *)callback;

@end

// MIT License
//
// Copyright (c) 2021 Wonder Partner's
// www.wonder-partners.com
//
// Copyright (c) 2018 Cory Butler
// www.CoryButler.com
//
Original file line number Diff line number Diff line change
@@ -1,30 +1,55 @@
#import "PermissionProviderHelper.h"
#import <AVFoundation/AVFoundation.h>

@implementation PermissionProviderHelper
- (void) verifyPermission:(NSString *)NSGameObject withCallback:(NSString *)NSCallback
- (void) askPermission:(NSString *)NSGameObject withCallback:(NSString *)NSCallback
{
// if (iOS >= 7) ask for camera access;
if ([AVCaptureDevice respondsToSelector:@selector(requestAccessForMediaType:completionHandler:)]) {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {

if (granted == YES) { UnitySendMessage(([NSGameObject cStringUsingEncoding:NSUTF8StringEncoding]),
([NSCallback cStringUsingEncoding:NSUTF8StringEncoding]), "true"); }
if (granted == YES) { UnitySendMessage(([NSGameObject cStringUsingEncoding:NSUTF8StringEncoding]),
([NSCallback cStringUsingEncoding:NSUTF8StringEncoding]), "true"); }

else { UnitySendMessage(([NSGameObject cStringUsingEncoding:NSUTF8StringEncoding]),
else { UnitySendMessage(([NSGameObject cStringUsingEncoding:NSUTF8StringEncoding]),
([NSCallback cStringUsingEncoding:NSUTF8StringEncoding]), "false"); }
}];
}
// if (iOS < 7) camera access is always permitted.
else {
UnitySendMessage(([NSGameObject cStringUsingEncoding:NSUTF8StringEncoding]),
UnitySendMessage(([NSGameObject cStringUsingEncoding:NSUTF8StringEncoding]),
([NSCallback cStringUsingEncoding:NSUTF8StringEncoding]), "true");
}
}

}

- (void) verifyPermission:(NSString *)NSGameObject withCallback:(NSString *)NSCallback
{
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

if (authStatus == AVAuthorizationStatusAuthorized) {
UnitySendMessage(([NSGameObject cStringUsingEncoding:NSUTF8StringEncoding]),
([NSCallback cStringUsingEncoding:NSUTF8StringEncoding]), "Authorized");
}
else if (authStatus == AVAuthorizationStatusDenied) {
UnitySendMessage(([NSGameObject cStringUsingEncoding:NSUTF8StringEncoding]),
([NSCallback cStringUsingEncoding:NSUTF8StringEncoding]), "Denied");
}
else if (authStatus == AVAuthorizationStatusNotDetermined) {
UnitySendMessage(([NSGameObject cStringUsingEncoding:NSUTF8StringEncoding]),
([NSCallback cStringUsingEncoding:NSUTF8StringEncoding]), "NotDetermined");
}
else if (authStatus == AVAuthorizationStatusRestricted) {
UnitySendMessage(([NSGameObject cStringUsingEncoding:NSUTF8StringEncoding]),
([NSCallback cStringUsingEncoding:NSUTF8StringEncoding]), "Restricted");
}
}
@end

// MIT License
//
// Copyright (c) 2021 Wonder Partner's
// www.wonder-partners.com
//
// Copyright (c) 2018 Cory Butler
// www.CoryButler.com
//

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.

Loading