clearbrian edited a comment on issue #703:
URL: 
https://github.com/apache/cordova-plugin-camera/issues/703#issuecomment-773472042


   Just noticed this bug too.
   Tapped on attach icon and sometimes nothing happening.
   When I run the iOS project I see background thread warnings in 
createFromPictureOptions:
   Access in CDVCameraPicker on background thread.
   
   The fix mentioned in the previous comment will work only if user accepts 
permission.
   Theres two other cases - user says no and app has permission already.
   
   
   The issue is caused further up in 
   - (void)takePicture:(CDVInvokedUrlCommand*)command
   - 
   theres a call to start a background thread
   
    [self.commandDelegate runInBackground:^{ 
   
    Then it asks user for permissions
   
    requestAccessForMediaType
    
    if user refuses it shows an Alert. This is wrapped in main_q
   
   but theres two other ways out of the method which should also be wrapped in 
mainq because later they launch CDVCameraPicker in showCameraPicker:withOptions:
   and you see purple background thread warnings.
     
    if user accepts permission or ALREADY has permission then it calls this 
method twice in 
    [weakSelf showCameraPicker:command.callbackId withOptions:pictureOptions];
    BUT
    its still in the background thread clause
    
   FIX it to wrap these two in main_q as well.
   
   
   ```
                    } else {
                        //BC added main thread call - when user Attaches photo 
camera picker was being called on background thread
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [weakSelf showCameraPicker:command.callbackId 
withOptions:pictureOptions];
                        });
                    }
                }];
           } else {
               dispatch_async(dispatch_get_main_queue(), ^{
                   [weakSelf showCameraPicker:command.callbackId 
withOptions:pictureOptions];
               });
               
           }
       }];
   }
   ```
   
   
   FULL METHOD
   
   ```
   - (void)takePicture:(CDVInvokedUrlCommand*)command
   {
       self.hasPendingOperation = YES;
       __weak CDVCamera* weakSelf = self;
   
       [self.commandDelegate runInBackground:^{
           CDVPictureOptions* pictureOptions = [CDVPictureOptions 
createFromTakePictureArguments:command];
           pictureOptions.popoverSupported = [weakSelf popoverSupported];
           pictureOptions.usesGeolocation = [weakSelf usesGeolocation];
           pictureOptions.cropToSize = NO;
   
           BOOL hasCamera = [UIImagePickerController 
isSourceTypeAvailable:pictureOptions.sourceType];
           if (!hasCamera) {
               NSLog(@"Camera.getPicture: source type %lu not available.", 
(unsigned long)pictureOptions.sourceType);
               CDVPluginResult* result = [CDVPluginResult 
resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"No camera available"];
               [weakSelf.commandDelegate sendPluginResult:result 
callbackId:command.callbackId];
               return;
           }
   
           // Validate the app has permission to access the camera
           if (pictureOptions.sourceType == 
UIImagePickerControllerSourceTypeCamera) {
               [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo 
completionHandler:^(BOOL granted)
                {
                    if(!granted)
                    {
                        // Denied; show an alert
                        dispatch_async(dispatch_get_main_queue(), ^{
                            UIAlertController *alertController = 
[UIAlertController alertControllerWithTitle:[[NSBundle mainBundle] 
objectForInfoDictionaryKey:@"CFBundleDisplayName"] 
message:NSLocalizedString(@"Access to the camera has been prohibited; please 
enable it in the Settings app to continue.", nil) 
preferredStyle:UIAlertControllerStyleAlert];
                            [alertController addAction:[UIAlertAction 
actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault 
handler:^(UIAlertAction * _Nonnull action) {
                                [weakSelf 
sendNoPermissionResult:command.callbackId];
                            }]];
                            [alertController addAction:[UIAlertAction 
actionWithTitle:NSLocalizedString(@"Settings", nil) 
style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                [[UIApplication sharedApplication] 
openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
                                [weakSelf 
sendNoPermissionResult:command.callbackId];
                            }]];
                            [weakSelf.viewController 
presentViewController:alertController animated:YES completion:nil];
                        });
                    } else {
                        //BC added main thread call - when user Attaches photo 
camera picker was being called on background thread
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [weakSelf showCameraPicker:command.callbackId 
withOptions:pictureOptions];
                        });
                    }
                }];
           } else {
               dispatch_async(dispatch_get_main_queue(), ^{
                   [weakSelf showCameraPicker:command.callbackId 
withOptions:pictureOptions];
               });
               
           }
       }];
   }
   ```
   
   
   
   
   
   
   
   
   
   
    


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to