Adds example of usage of the UIAlertController to show error messages to the user.

This commit is contained in:
miguelcampiao 2015-07-15 15:07:12 +01:00
parent d666ab665b
commit 0507f72f11
2 changed files with 34 additions and 7 deletions

View File

@ -51,9 +51,21 @@ public class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate {
/// Property to determine if the manager should show the camera permission popup immediatly when it's needed or you want to show it manually. Default value is true. Be carful cause using the camera requires permission, if you set this value to false and don't ask manually you won't be able to use the camera.
public var showAccessPermissionPopupAutomatically = true
/// A block creating UI to present error message to the user.
/// A block creating UI to present error message to the user. This can be customised to be presented on the Window root view controller, or to pass in the viewController which will present the UIAlertController, for example.
public var showErrorBlock:(erTitle: String, erMessage: String) -> Void = { (erTitle: String, erMessage: String) -> Void in
UIAlertView(title: erTitle, message: erMessage, delegate: nil, cancelButtonTitle: NSLocalizedString("Ok", comment:"")).show()
// var alertController = UIAlertController(title: erTitle, message: erMessage, preferredStyle: .Alert)
// alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (alertAction) -> Void in
// //
// }))
//
// let topController = UIApplication.sharedApplication().keyWindow?.rootViewController
//
// if (topController != nil) {
// topController?.presentViewController(alertController, animated: true, completion: { () -> Void in
// //
// })
// }
}
/// Property to determine if manager should write the resources to the phone library. Default value is true.

View File

@ -36,7 +36,7 @@ class ViewController: UIViewController {
self.askForPermissionsLabel.hidden = true
let currentCameraState = self.cameraManager.currentCameraStatus()
if currentCameraState == .NotDetermined {
self.askForPermissionsButton.hidden = false
self.askForPermissionsLabel.hidden = false
@ -47,6 +47,7 @@ class ViewController: UIViewController {
self.flashModeButton.enabled = false
self.flashModeButton.setTitle("No flash", forState: UIControlState.Normal)
}
}
override func viewWillAppear(animated: Bool)
@ -69,8 +70,20 @@ class ViewController: UIViewController {
private func addCameraToView()
{
self.cameraManager.addPreviewLayerToView(self.cameraView, newCameraOutputMode: CameraOutputMode.VideoWithMic)
CameraManager.sharedInstance.showErrorBlock = { (erTitle: String, erMessage: String) -> Void in
UIAlertView(title: erTitle, message: erMessage, delegate: nil, cancelButtonTitle: "OK").show()
CameraManager.sharedInstance.showErrorBlock = { (erTitle: String, erMessage: String) -> Void in
// var alertController = UIAlertController(title: erTitle, message: erMessage, preferredStyle: .Alert)
// alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (alertAction) -> Void in
// //
// }))
//
// let topController = UIApplication.sharedApplication().keyWindow?.rootViewController
//
// if (topController != nil) {
// topController?.presentViewController(alertController, animated: true, completion: { () -> Void in
// //
// })
// }
}
}
@ -78,6 +91,8 @@ class ViewController: UIViewController {
@IBAction func changeFlashMode(sender: UIButton)
{
switch (self.cameraManager.changeFlashMode()) {
case .Off:
sender.setTitle("Flash Off", forState: UIControlState.Normal)
@ -110,8 +125,8 @@ class ViewController: UIViewController {
} else {
self.cameraManager.stopRecordingVideo({ (videoURL, error) -> Void in
println(videoURL)
if let errorOccured = error {
UIAlertView(title: "Error occured", message: errorOccured.localizedDescription, delegate: nil, cancelButtonTitle: "OK").show()
if let errorOccured = error {
self.cameraManager.showErrorBlock(erTitle: "Error occurred", erMessage: errorOccured.localizedDescription)
}
})
}