如何在ios中检查RequestPermissions?

时间:2018-04-02 11:33:54

标签: ios cordova-plugins

我需要在我的应用程序中访问照片和地图。所以我在plist文件中添加了相应的功能。但是我需要检查请求是否被允许或拒绝。没有得到怎么做请建议我此

2 个答案:

答案 0 :(得分:1)

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

  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"];
    }
    
  2. 只需根据您的要求更改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"];
           }
      }
      
    3. 不要忘记为这些代码导入适当的软件包。

答案 1 :(得分:0)

可以使用照片选择器

../app/assets/images/uploads/profile/avatar/20/thumb_8eb67953-d383-4c1f-a42d-662903386814.jpg

对于loaction,你可以使用

UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) or .camera

并且不要忘记导入corelocation