gpt4 book ai didi

ios - 如何检查 ios 中的 RequestPermissions?

转载 作者:行者123 更新时间:2023-11-29 00:00:30 26 4
gpt4 key购买 nike

我需要在我的应用程序中访问照片和 map 。所以我在 plist 文件中添加了相应的功能。但是我需要检查请求是否被允许或拒绝。我不知道该怎么做,请建议我这个。

最佳答案

您必须明确检查不同类型的权限,例如图像/视频、位置、联系人列表等。

  1. 图片/视频/音频/文档等

    NSString *mediaType = AVMediaTypeVideo;
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
    if(authStatus == AVAuthorizationStatusAuthorized)
    {
    // do your logic
    } else if(authStatus == AVAuthorizationStatusDenied)
    {
    // denied
    [self showSettingsAlert:@"Previously you have denied"];
    } else if(authStatus == AVAuthorizationStatusRestricted)
    {
    // restricted, normally won't happen
    [self showSettingsAlert:@"You have revoke"];
    } else if(authStatus == AVAuthorizationStatusNotDetermined)
    {
    // not determined?!
    [AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted)
    {
    if(granted)
    {
    [self openVideoPicker:buttonIndex];
    } else
    {
    [self showSettingsAlert:@"You have denied"];
    }
    }];
    } else
    {
    [self showSettingsAlert:@"You have revoke"];
    }

只需根据您的要求更改 AVMediaTypes。

  1. 手机联系人列表

    CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
    if (status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusRestricted)
    {
    //Show Alert Access Denied
    return;
    }

    CNContactStore *store = [[CNContactStore alloc] init];
    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error)
    {
    // make sure the user granted us access
    if (!granted)
    {
    dispatch_async(dispatch_get_main_queue(), ^{

    //Show Alert Access Denied
    return;
    }

    BOOL success = [store enumerateContactsWithFetchRequest:request error:&fetchError usingBlock:^(CNContact *contact, BOOL *stop)
    {
    // build array of contacts
    NSMutableArray *contacts = [NSMutableArray array];

    NSError *fetchError;
    CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[CNContactIdentifierKey, [CNContactFormatter descriptorForRequiredKeysForStyle:CNContactFormatterStyleFullName], CNContactPhoneNumbersKey, CNContactGivenNameKey, CNContactFamilyNameKey]];
    [contacts addObject:contact];
    }];
    if (!success)
    {
    [UIUtil showWarningAlert:fetchError.localizedDescription onController:self];
    return;
    }

    将此代码放入将处理此类权限验证的函数中。

  2. 对于位置,您必须实现CLLocationManagerDelegate,这两个函数将为您提供位置信息。

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
    {
    currentLocation = [locations objectAtIndex:0];
    [locationManager stopUpdatingLocation];

    latitude = [NSString stringWithFormat:@"%f", currentLocation.coordinate.latitude];
    longitude = [NSString stringWithFormat:@"%f", currentLocation.coordinate.longitude];
    }

    -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    {
    if (error.domain == kCLErrorDomain && error.code == kCLErrorDenied)
    {
    [locationManager stopUpdatingLocation];
    [self showSettingsAlert:@"You have denied Location Permission"];
    }
    }

不要忘记为这些代码导入适当的包。

关于ios - 如何检查 ios 中的 RequestPermissions?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49610496/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com