From 0507f72f11f725a71440e68a808cb206ee9a31a9 Mon Sep 17 00:00:00 2001 From: miguelcampiao Date: Wed, 15 Jul 2015 15:07:12 +0100 Subject: [PATCH] Adds example of usage of the UIAlertController to show error messages to the user. --- camera/CameraManager.swift | 16 ++++++++++++++-- camera/ViewController.swift | 25 ++++++++++++++++++++----- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/camera/CameraManager.swift b/camera/CameraManager.swift index 99a9202..ce81cb6 100644 --- a/camera/CameraManager.swift +++ b/camera/CameraManager.swift @@ -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. diff --git a/camera/ViewController.swift b/camera/ViewController.swift index cdd8a3e..6178967 100644 --- a/camera/ViewController.swift +++ b/camera/ViewController.swift @@ -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) } }) }