zjf
2023-03-06 392b76515f40376b6d36f40a114850ef63650384
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
//  SGPermission.m
//  SGQRCodeExample
//
//  Created by kingsic on 2022/7/8.
//  Copyright © 2022 kingsic. All rights reserved.
//
 
#import "SGPermission.h"
#import "SGPermissionCamera.h"
#import "SGPermissionPhoto.h"
 
@interface SGPermission ()
@property (nonatomic, assign) SGPermissionType type;
@end
 
@implementation SGPermission
 
- (void)initWithType:(SGPermissionType)type completion:(SGPermissionBlock)block {
    [SGPermission permissionWithType:type completion:block];
}
 
+ (void)permissionWithType:(SGPermissionType)type completion:(SGPermissionBlock)block {
    SGPermission *permission = [[SGPermission alloc] init];
    permission.type = type;
    
    if (type == SGPermissionTypeCamera) {
        [SGPermissionCamera camera:^(SGPermissionCamera * _Nonnull camera, SGPermissionStatus status) {
            if (block) {
                block(permission, status);
            }
        }];
    } else if (type == SGPermissionTypePhoto) {
        [SGPermissionPhoto photo:^(SGPermissionPhoto * _Nonnull photos, SGPermissionStatus status) {
            if (block) {
                block(permission, status);
            }
        }];
    }
}
 
- (void)request:(void (^)(BOOL))handler {
    if (self.type == SGPermissionTypeCamera) {
        [SGPermissionCamera request:handler];
    } else if (self.type == SGPermissionTypePhoto) {
        [SGPermissionPhoto request:handler];
    }
}
 
@end