2016-11-23 76 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應用程序有訪問權限。