2016-11-23 78 views
0

正如标题所示,我试图在键盘的视图内添加一个相机,就像当前Messages应用程序中的相机一样。在自定义键盘中添加相机视图 - Swift 3.0和IOS 10

随着我当前的代码,键盘只是简单的被跳过当我试图改变它,它绝不会要求得到允许访问摄像头,即使我设置了正确的密钥的info.plist内(我将它设置在键盘和主类的info.plist)。我没有写其他代码。

这是我的代码我KeyboardViewController

import UIKit 
import AVFoundation 

class KeyboardViewController: UIInputViewController { 

@IBOutlet weak var cameraView: UIView! 
@IBOutlet var nextKeyboardButton: UIButton! 

var session : AVCaptureSession? 
var stillImageOutput : AVCaptureStillImageOutput? 
var videoPreviewLayer : AVCaptureVideoPreviewLayer? 

var captureDevice : AVCaptureDevice? 

override func updateViewConstraints() { 
    super.updateViewConstraints() 

    // Add custom view sizing constraints here 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Perform custom UI setup here 
    self.nextKeyboardButton = UIButton(type: .system) 

    self.nextKeyboardButton.setTitle(NSLocalizedString("Next Keyboard", comment: "Title for 'Next Keyboard' button"), for: []) 
    self.nextKeyboardButton.sizeToFit() 
    self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false 

    self.nextKeyboardButton.addTarget(self, action: #selector(handleInputModeList(from:with:)), for: .allTouchEvents) 

    self.view.addSubview(self.nextKeyboardButton) 

    self.nextKeyboardButton.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true 
    self.nextKeyboardButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true 
} 

override func viewWillAppear(_ animated: Bool) { 

    session = AVCaptureSession() 
    session!.sessionPreset = AVCaptureSessionPresetPhoto 

    let videoDevices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo) 

    for device in videoDevices! { 

     let device = device as! AVCaptureDevice 
     if device.position == AVCaptureDevicePosition.front { 

      captureDevice = device 

     } 

    } 

    //We will make a new AVCaptureDeviceInput and attempt to associate it with our backCamera input device. 
    //There is a chance that the input device might not be available, so we will set up a try catch to handle any potential errors we might encounter. 
    var error : NSError? 
    var input : AVCaptureDeviceInput! 
    do { 

     input = try AVCaptureDeviceInput(device: captureDevice) 

    } catch let error1 as NSError { 

     error = error1 
     input = nil 
     print(error!.localizedDescription) 

    } 

    if error == nil && session!.canAddInput(input) { 

     session!.addInput(input) 

     // The remainder of the session setup will go here... 

     stillImageOutput = AVCaptureStillImageOutput() 
     stillImageOutput?.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG] 

     if session!.canAddOutput(stillImageOutput) { 

      session!.addOutput(stillImageOutput) 

      //configure live preview here 

      videoPreviewLayer = AVCaptureVideoPreviewLayer(session: session) 
      videoPreviewLayer!.videoGravity = AVLayerVideoGravityResizeAspect 
      videoPreviewLayer!.connection?.videoOrientation = AVCaptureVideoOrientation.portrait 

      cameraView.layer.addSublayer(videoPreviewLayer!) 

      session!.startRunning() 

     } 

    } 

} 

override func viewDidAppear(_ animated: Bool) { 
    videoPreviewLayer!.frame = cameraView.bounds 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated 
} 

override func textWillChange(_ textInput: UITextInput?) { 
    // The app is about to change the document's contents. Perform any preparation here. 
} 

override func textDidChange(_ textInput: UITextInput?) { 
    // The app has just changed the document's contents, the document context has been updated. 

    var textColor: UIColor 
    let proxy = self.textDocumentProxy 
    if proxy.keyboardAppearance == UIKeyboardAppearance.dark { 
     textColor = UIColor.white 
    } else { 
     textColor = UIColor.black 
    } 
    self.nextKeyboardButton.setTitleColor(textColor, for: []) 
} 

}

回答

0

你没有从专用键盘扩展进入相机内部。根据Apple准则,某些API不适用于iOS扩展。请查看Appple Extensions guideline下的“某些API对应用程序扩展无法使用”。

访问iOS设备上的照相机或麦克风(一个的iMessage应用程序, 不像其它应用程序扩展,确实有权访问这些资源, 只要它正确地配置NSCameraUsageDescription和 NSMicrophoneUsageDescription Info.plist的键)

但是,iMessage应用程序有访问权限。