0

i am able to implement buttonactions,tableview,textfields ,switch... all are working fine in ActionExtension

BUT

Scan Functionality doesn't work (no camera opening to scan ) in Action Extension . Below i shared my scanning code in swift and screenshot of my Extension

Declarations :

var scannedBarcode = NSString(string: "")
var scannedMetadataOutput = AVCaptureMetadataOutput()

var gCaptureMetadataOutput = AVCaptureMetadataOutput()
var _prevLayer = AVCaptureVideoPreviewLayer()
var _session = AVCaptureSession()

Scan Button Action Code :

 @IBAction func scanExtenAction(sender: AnyObject) {

    scanExtAction.enabled = false


    var error:NSError? = nil
    //let _input = (try! AVCaptureDeviceInput.deviceInputWithDevice(gCaptureDevice))
    let _input = (try! AVCaptureDeviceInput.init(device: gCaptureDevice))

    if _session.canAddInput(_input)
    {
        _session.addInput(_input)
    }

    var _output = AVCaptureMetadataOutput()

    gCaptureMetadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())

    if _session.canAddOutput(gCaptureMetadataOutput)
    {
        _session.addOutput(gCaptureMetadataOutput)
    }

    //        gCaptureMetadataOutput.metadataObjectTypes = gCaptureMetadataOutput.availableMetadataObjectTypes
    gCaptureMetadataOutput.metadataObjectTypes = gCaptureMetadataOutput.availableMetadataObjectTypes

    _prevLayer = AVCaptureVideoPreviewLayer(layer: _session) as AVCaptureVideoPreviewLayer

    //_prevLayer.frame = self.view.bounds
    //_prevLayer.frame = CGRectMake( DEVICE_WIDTH * 0.10 , DEVICE_HEIGHT * 0.10, DEVICE_WIDTH * 0.8, DEVICE_HEIGHT * 0.4 )
    _prevLayer.frame = CGRectMake(0 , 0, 280, 300 )
    _prevLayer.cornerRadius = 10


    _prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;


   self.view.layer.addSublayer(_prevLayer)


    _session.startRunning()



}

enter image description here

Sanju
  • 1,148
  • 11
  • 26

1 Answers1

3

App Extensions cannot do the following:

1.Access the camera or microphone on an iOS device.

2.Receive data using AirDrop (It can however send data using AirDrop).

3.Perform long-running background tasks.

4.Use any API marked in header files with the NS_EXTENSION_UNAVAILABLE macro, or similar unavailability macro, or any API in an unavailable framework for example EventKit and HealthKit are unavailable to app extensions.

5.Access a sharedApplication object, and so cannot use any of the methods on that object

refer below link for more info: link

Rohit Pradhan
  • 3,867
  • 1
  • 21
  • 29
  • he thanks i didn't read full theory part in that link Before . then i can't give scan functionality in Action Extension :) . – Sanju Feb 26 '16 at 12:25