Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Custom Keyboard - camera not working

I want to create a custom keyboard, that acts as a barcode scanner. I already did the whole coding, but the output is not as expected: I am being asked for camera permissions (the first time), but the camera sends no video to the view.

I think, that there might be some restrictions using keyboards for safety reasons?!?

1.) Turn on the torch

-(void) turnFlashOn
{
    AVCaptureDevice *flashLight = [AVCaptureDevice
                                   defaultDeviceWithMediaType:AVMediaTypeVideo];
    if([flashLight isTorchAvailable] && [flashLight
                                         isTorchModeSupported:AVCaptureTorchModeOn])
    {
        BOOL success = [flashLight lockForConfiguration:nil];
        if(success){
            NSError *error;
            [flashLight setTorchMode:AVCaptureTorchModeOn];
            [flashLight setTorchModeOnWithLevel:1.0 error:&error];
            NSLog(@"Error: %@", error);
            [flashLight unlockForConfiguration];
            NSLog(@"flash turned on -> OK");

        }
        else
        {
            NSLog(@"flash turn on -> ERROR");
        }
    }

}

This gives me this log output, but nothing happens with the flash:

Error: (null)
flash turned on -> OK

2.) Scan the barcode (part of viewDidLoad)

    // SCANNER PART
self.captureSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error];
if(videoInput)
    [self.captureSession addInput:videoInput];
else
    NSLog(@"Error: %@", error);

AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc] init];
[self.captureSession addOutput:metadataOutput];
[metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[metadataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code]];

AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];

camView = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
previewLayer.frame = camView.layer.bounds;
[camView.layer addSublayer:previewLayer];
self.keyboard.barcodeView.clipsToBounds=YES;
camView.center = CGPointMake(self.keyboard.barcodeView.frame.size.width/2, self.keyboard.barcodeView.frame.size.height/2);

[self.keyboard.barcodeView addSubview:camView];

And if I press a special key on my keyboard this one is called:

-(void)scanBarcodeNow{
AudioServicesPlaySystemSound(systemSoundTock);
NSLog(@"Start scanning...");
self.keyboard.barcodeView.hidden=false;
[self.keyboard.barcodeView addSubview:camView];
[self.keyboard.barcodeView setBackgroundColor:[UIColor redColor]];
[self.captureSession startRunning];

}

The only thing happens, is that the keyboard.barcodeView changes its background color to red. I've made this to see, that all the wiring that I've done should be Ok. But no video from the cam is shown....

Can anyone help me out?

like image 809
Supagusti Avatar asked Sep 21 '14 08:09

Supagusti


People also ask

How do I use my iPhone camera keyboard?

Open Camera, then position iPhone so the text appears within the camera frame. Swipe or use grab points to select text, then do any of the following: Copy Text: Copy text to paste into another app such as Notes or Messages. Select All: Select all the text within the frame.

What is a keyboard camera iPhone?

In iOS 15 you can easily grab Text from Camera, with the help of a new built-in keyboard viewfinder. Apple calls this feature Live Text as it instantly pastes the identified text in the keyboard input field of Messages, Mail, Safari, WhatsApp, social media websites.

How do I customize my keyboard iOS?

Navigate to Settings > General > Keyboard > Keyboards > Add New Keyboard and select AC Custom Keyboard. This will add it to the list of available keyboards. Go back to your app and bring up the keyboard by tapping the text view. Tap and hold the globe key and select AC Keyboard from the list that pops up.


1 Answers

The reason you're getting back null is because you don't have access to it. It's actually not a bug. According to Apple guidelines certain APIs are not available to iOS 8 extensions (See bullet #3 below).

enter image description here

It sucks, but I always encourage people to read up on new features and see if what they want to do is possible, before dwelling into an idea (Saves a lot of time). Definitely check out the App Extension Programming Guide for more information.

like image 170
KingPolygon Avatar answered Oct 16 '22 07:10

KingPolygon