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


   Im not sure this is the right place to wrap the code in main q It needs to 
be done further up where user is asked for permission to access camera and 
where the code back goes into the background.
   The issue is caused further up in 
   - (void)takePicture:(CDVInvokedUrlCommand*)command
   theres a call to 
    [self.commandDelegate runInBackground:^{ 
    Then it checks 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:
     
     
    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