3

我想扫描分享扩展中的二维码,下面的二维码可以在正常的iOS应用程序中正常运行

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

NSError *error;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (error != nil) {
    NSLog(@"no camera");
    return;
}

AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];

self.session = [[AVCaptureSession alloc] init];
[self.session addInput:input];
[self.session addOutput:output];

output.metadataObjectTypes = [NSArray arrayWithObject:AVMetadataObjectTypeQRCode];

self.preview = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
CGRect rect = CGRectMake(50, 50, 300, 600);
self.preview.frame = rect;

[self.view.layer insertSublayer:self.preview atIndex:0];

[self setRuntimeErrorHandlingObserver:[[NSNotificationCenter defaultCenter] addObserverForName:AVCaptureSessionRuntimeErrorNotification object:[self session] queue:nil usingBlock:^(NSNotification *note) {
    NSLog(@"session runtime error: %@", note);
}]];
[self.session startRunning];

但是当我在共享扩展中运行此代码时,它会显示错误消息:

NSConcreteNotification 0x174244a70 {name = AVCaptureSessionRuntimeErrorNotification; object = <AVCaptureSession: 0x17400a280 [AVCaptureSessionPresetHigh]>; userInfo = {

AVCaptureSessionErrorKey = "Error Domain=AVFoundationErrorDomain Code=-11837 \"Cannot Use (null)\" UserInfo=0x174262500 {NSLocalizedDescription=Cannot Use (null), NSLocalizedFailureReason=(null) may not be used by an app running in the background.}";}}

这是否意味着共享扩展正在后台运行,是否有任何其他方法可以修复。

4

0 回答 0