Skip to content

Instantly share code, notes, and snippets.

@hashaam
hashaam / adjust-scrollview-content-inset-based-on-keyboard-frame-method.swift
Last active September 2, 2017 10:21
Adjust Scroll View Content Inset Based on Keyboard Frame
// https://hashaam.com/2017/09/02/adjust-scroll-view-content-inset-based-on-keyboard-frame
func adjustForKeyboardHandler(notification: Notification) {
// this method assumes scrollView is defined
guard let userInfo = notification.userInfo else { return }
guard let value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue else { return }
let keyboardFrame = value.cgRectValue
@hashaam
hashaam / url-encode-only-unicode-characters.swift
Last active November 13, 2017 06:03
URL Encode only Unicode Characters
// https://hashaam.com/2017/11/13/url-encode-only-unicode-characters/
let originalURLString = "https://www.exampleserver.com/articles/latest/لن-تصدق-ما-يمكن-أن-تجده-في-مطبخك-فاحذر-غسل-هذه-الأداة.html"
let allowedCharacterSet = CharacterSet.urlFragmentAllowed
let urlString = originalURLString.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet)!
let url = URL(string: urlString)!
@hashaam
hashaam / adjust-baseline-of-text-using-nsattributedstring.swift
Last active November 13, 2017 06:08
Adjust Baseline of Text Using NSAttributedString
// https://hashaam.com/2017/11/13/adjust-baseline-of-text-using-nsattributedstring/
let attributedString = NSAttributedString(string: “text", attributes: [
NSFontAttributeName: font,
NSForegroundColorAttributeName: UIColor.white,
NSBaselineOffsetAttributeName: -5.0
])
@hashaam
hashaam / get-filename-without-extension.swift
Last active March 22, 2019 10:11
Get filename without extension
// https://hashaam.com/2017/09/02/get-filename-without-extension
let path = "hls_a128_v4.m3u8"
let url = URL(string: path)
let filename = url?.deletingPathExtension().lastPathComponent
print(filename) // hls_a128_v4
let fileExtension = url?.pathExtension
// Youtube: https://www.youtube.com/watch?v=7AlZxClmhPw
// Source: https://hashaam.com/2020/07/30/creating-camera-application-with-avfoundation/
import UIKit
class LaunchViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
// Youtube: https://www.youtube.com/watch?v=7AlZxClmhPw
// Source: https://hashaam.com/2020/07/30/creating-camera-application-with-avfoundation/
extension LaunchViewController: RequestCameraAuthorizationViewDelegate {
func requestCameraAuthorizationActionButtonTapped() {
requestCameraAuthorization()
}
}
// Youtube: https://www.youtube.com/watch?v=7AlZxClmhPw
// Source: https://hashaam.com/2020/07/30/creating-camera-application-with-avfoundation/
func requestCameraAuthorization() {
RequestCameraAuthorizationController.requestCameraAuthorization(completionHandler: { status in
switch status {
case .granted:
print("granted")
case .notRequested:
break
// Youtube: https://www.youtube.com/watch?v=7AlZxClmhPw
// Source: https://hashaam.com/2020/07/30/creating-camera-application-with-avfoundation/
import AVFoundation
let status = AVCaptureDevice.authorizationStatus(for: .video)
switch status {
case .authorized:
print("authorized")
case .notDetermined:
// Youtube: https://www.youtube.com/watch?v=7AlZxClmhPw
// Source: https://hashaam.com/2020/07/30/creating-camera-application-with-avfoundation/
import AVFoundation
AVCaptureDevice.requestAccess(for: .video, completionHandler: { granted in
if granted {
print("granted")
} else {
print("unauthorized")
// Youtube: https://www.youtube.com/watch?v=7AlZxClmhPw
// Source: https://hashaam.com/2020/07/30/creating-camera-application-with-avfoundation/
import Foundation
import AVFoundation
enum CameraAuthorizationStatus {
case notRequested
case granted
case unauthorized