Last active
July 25, 2019 04:44
-
-
Save yashthaker7/3387fcd7faee8550171187b835cabfdf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Extension+UIViewController.swift | |
// | |
// Created by Yash on 15/01/19. | |
// Copyright © 2019 Yash. All rights reserved. | |
// | |
import UIKit | |
import AVKit | |
extension UIViewController { | |
func createUniqueVideoUrlInDD() -> URL { | |
return createUrlInAppDD(UUID().uuidString, extension: ".MOV") | |
} | |
func createUniqueVideoUrlInTemp() -> URL { | |
return createUrlInTemp(UUID().uuidString, extension: ".MOV") | |
} | |
func createUrlInTemp(_ nameWithFileExtension: String) -> URL { | |
return URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(nameWithFileExtension) | |
} | |
func createUrlInAppDD(_ name: String) -> URL { | |
let fileManager = FileManager.default | |
let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first | |
return documentDirectory!.appendingPathComponent(name) | |
} | |
func removeFileIfExists(fileURL : URL) { | |
let fileManager = FileManager.default | |
if fileManager.fileExists(atPath: fileURL.path) { | |
do { | |
try fileManager.removeItem(at: fileURL) | |
} catch { | |
print("Could not delete exist file so cannot write to it") | |
} | |
} | |
} | |
public static func clearTempFolder() { | |
let fileManager = FileManager.default | |
let tempFolderPath = NSTemporaryDirectory() | |
do { | |
let filePaths = try fileManager.contentsOfDirectory(atPath: tempFolderPath) | |
for filePath in filePaths { | |
try fileManager.removeItem(atPath: tempFolderPath + filePath) | |
} | |
} catch { | |
print("Could not clear temp folder: \(error)") | |
} | |
} | |
func secureCopyItem(at srcUrl: URL, to dstUrl: URL) -> (isSuccess: Bool,error: Error?) { | |
do { | |
if FileManager.default.fileExists(atPath: dstUrl.path) { | |
try FileManager.default.removeItem(at: dstUrl) | |
} | |
try FileManager.default.copyItem(at: srcUrl, to: dstUrl) | |
} catch (let error) { | |
print("Cannot copy item at \(srcUrl) to \(dstUrl): \(error)") | |
return (false,error) | |
} | |
return (true, nil) | |
} | |
func runAfter(_ seconds: Double, completion: @escaping () -> ()) { | |
DispatchQueue.main.asyncAfter(deadline: .now() + seconds, execute: { | |
completion() | |
}) | |
} | |
func showAlert(title: String? = nil, message: String? = nil, okHandler: (() -> ())? = nil) { | |
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) | |
let ok = UIAlertAction(title: "OK", style: .default) { (alertAction) in | |
if let okHandler = okHandler { okHandler() } | |
} | |
alert.addAction(ok) | |
present(alert, animated: true, completion: nil) | |
} | |
func askedCameraPermission(successHandler: @escaping () -> Void) { | |
switch AVCaptureDevice.authorizationStatus(for: .video) { | |
case .authorized: | |
DispatchQueue.main.async { successHandler() } | |
case .denied, .restricted: | |
DispatchQueue.main.async { self.openSettingsForCameraPermission() } | |
case .notDetermined: | |
AVCaptureDevice.requestAccess(for: .video) { granted in | |
if granted { | |
DispatchQueue.main.async { successHandler() } | |
return | |
} | |
DispatchQueue.main.async { self.openSettingsForCameraPermission() } | |
} | |
} | |
} | |
func openSettingsForCameraPermission() { | |
openSettingsForPermission(title: "App name", message: "This App doesn't have permission to use the camera, please change privacy settings") | |
} | |
func askedMicrophonePermission(successHandler: @escaping () -> Void) { | |
switch AVAudioSession.sharedInstance().recordPermission { | |
case .granted: | |
DispatchQueue.main.async { successHandler() } | |
case .denied: | |
DispatchQueue.main.async { self.openSettingsForMicrophonePermission() } | |
case .undetermined: | |
AVAudioSession.sharedInstance().requestRecordPermission({ (granted) in | |
if granted { | |
DispatchQueue.main.async { successHandler() } | |
return | |
} | |
DispatchQueue.main.async { self.openSettingsForMicrophonePermission() } | |
}) | |
} | |
} | |
func openSettingsForMicrophonePermission() { | |
openSettingsForPermission(title: "App name", message: "This App doesn't have permission to use the microphone, please change privacy settings") | |
} | |
func askedPhotoUsagePermission(successHandler: @escaping () -> Void) { | |
PHPhotoLibrary.requestAuthorization { status in | |
switch status { | |
case .authorized: | |
DispatchQueue.main.async { successHandler() } | |
case .denied, .restricted: | |
DispatchQueue.main.async { self.openSettingsForPhotoPermission() } | |
case .notDetermined: | |
PHPhotoLibrary.requestAuthorization({ (newStatus) in | |
if newStatus == .authorized { | |
DispatchQueue.main.async { successHandler() } | |
return | |
} | |
DispatchQueue.main.async { self.openSettingsForPhotoPermission() } | |
}) | |
} | |
} | |
} | |
func openSettingsForPhotoPermission() { | |
openSettingsForPermission(title: "App name", message: "This App doesn't have permission to use the photos, please change privacy settings") | |
} | |
private func openSettingsForPermission(title: String, message: String) { | |
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) | |
let settingsAction = UIAlertAction(title: "Settings", style: .default) { (alertAction) in | |
if let appSettings = URL(string: UIApplication.openSettingsURLString) { | |
UIApplication.shared.open(appSettings, options: [:], completionHandler: nil) | |
} | |
} | |
alertController.addAction(settingsAction) | |
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) | |
alertController.addAction(cancelAction) | |
self.present(alertController, animated: true, completion: nil) | |
} | |
func playVideo(_ videoUrl: URL) { | |
let player = AVPlayer(url: videoUrl) | |
let playerVC = AVPlayerViewController() | |
playerVC.player = player | |
self.present(playerVC, animated: true) { | |
playerVC.player?.play() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment