2014 snapchat source code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.2 KiB

  1. //
  2. // SCCaptureDeviceAuthorization.m
  3. // Snapchat
  4. //
  5. // Created by Xiaomu Wu on 8/19/14.
  6. // Copyright (c) 2014 Snapchat, Inc. All rights reserved.
  7. //
  8. #import "SCCaptureDeviceAuthorization.h"
  9. #import <BlizzardSchema/SCAEvents.h>
  10. #import <SCFoundation/SCTrace.h>
  11. #import <SCLogger/SCLogger.h>
  12. @import AVFoundation;
  13. @implementation SCCaptureDeviceAuthorization
  14. #pragma mark - Public
  15. + (BOOL)notDeterminedForMediaType:(NSString *)mediaType
  16. {
  17. return [AVCaptureDevice authorizationStatusForMediaType:mediaType] == AVAuthorizationStatusNotDetermined;
  18. }
  19. + (BOOL)deniedForMediaType:(NSString *)mediaType
  20. {
  21. return [AVCaptureDevice authorizationStatusForMediaType:mediaType] == AVAuthorizationStatusDenied;
  22. }
  23. + (BOOL)restrictedForMediaType:(NSString *)mediaType
  24. {
  25. return [AVCaptureDevice authorizationStatusForMediaType:mediaType] == AVAuthorizationStatusRestricted;
  26. }
  27. + (void)requestAccessForMediaType:(NSString *)mediaType completionHandler:(void (^)(BOOL granted))handler
  28. {
  29. [AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:handler];
  30. }
  31. #pragma mark - Convenience methods for AVMediaTypeVideo
  32. + (BOOL)notDeterminedForVideoCapture
  33. {
  34. return [self notDeterminedForMediaType:AVMediaTypeVideo];
  35. }
  36. + (BOOL)deniedForVideoCapture
  37. {
  38. return [self deniedForMediaType:AVMediaTypeVideo];
  39. }
  40. + (void)requestAccessForVideoCaptureWithCompletionHandler:(void (^)(BOOL granted))handler
  41. {
  42. BOOL firstTimeAsking =
  43. [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo] == AVAuthorizationStatusNotDetermined;
  44. [self requestAccessForMediaType:AVMediaTypeVideo
  45. completionHandler:^(BOOL granted) {
  46. if (firstTimeAsking) {
  47. SCAPermissionPromptResponse *responseEvent = [[SCAPermissionPromptResponse alloc] init];
  48. [responseEvent setPermissionPromptType:SCAPermissionPromptType_OS_CAMERA];
  49. [responseEvent setAccepted:granted];
  50. [[SCLogger sharedInstance] logUserTrackedEvent:responseEvent];
  51. }
  52. if (handler) {
  53. handler(granted);
  54. }
  55. }];
  56. }
  57. @end