Skip to content

Instantly share code, notes, and snippets.

@Anruin
Last active June 18, 2021 21:12
Show Gist options
  • Save Anruin/3e1182ead6cda4b4b11b8167ca15ec25 to your computer and use it in GitHub Desktop.
Save Anruin/3e1182ead6cda4b4b11b8167ca15ec25 to your computer and use it in GitHub Desktop.
UE4 Android Permission request wrapper macro
// Copyright 2021 Impossibility Labs Inc. https://github.com/ArtheonVR.
// Created by Egor Pristavka.
// Macro definition:
#if PLATFORM_ANDROID
#include "AndroidPermissionFunctionLibrary.h"
#include "AndroidPermissionCallbackProxy.h"
#define RUN_WITH_PERMISSION(perm, ...)\
LogF("AndroidPermission: Checking %s permission.", perm);\
if (!UAndroidPermissionFunctionLibrary::CheckPermission(perm)) {\
TArray<FString> Permissions;\
Permissions.Add(perm);\
if (UAndroidPermissionCallbackProxy* Callback = UAndroidPermissionFunctionLibrary::AcquirePermissions(Permissions)) {\
LogF("AndroidPermission: Requested %s permission.", perm);\
Callback->OnPermissionsGrantedDelegate.BindLambda([this](const TArray<FString>& Permissions, const TArray<bool>& GrantResults) {\
LogF("AndroidPermission: On permissions granted called.");\
if (GrantResults.Num() > 0) {\
if (GrantResults[0]) {\
LogF("AndroidPermission: Acquired %s permission.", perm);\
__VA_ARGS__;\
} else {\
LogWarningF("AndroidPermission: Failed to acquire %s permission: grant result is false.", perm);\
}\
} else {\
LogWarningF("AndroidPermission: Failed to acquire %s permission: grant results array is empty.", perm);\
}\
});\
} else {\
LogWarningF("AndroidPermission: Failed to acquire %s permission.", perm);\
}\
} else {\
LogF("AndroidPermission: Already have %s permission.", perm);\
__VA_ARGS__;\
}
#else
#define RUN_WITH_PERMISSION(perm, ...)
#endif
// Usage example:
#if PLATFORM_ANDROID
RUN_WITH_PERMISSION(TEXT("android.permission.RECORD_AUDIO"), {
LogF("Vivox: Requesting permissions.");
});
#endif
// If you find this helpful, consider to support our project at Patreon https://www.patreon.com/artheon
// Or you can send a tip to Bitcoin 1E6ixVkj5bG9MpBGXhZejEcGJuPQJMWV4V or Ethereum 0xAd3c93F3F82f4bE7366E0677005c106Eaf9120df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment