Skip to content

Instantly share code, notes, and snippets.

@hashaam
hashaam / get-specific-urlqueryitem-from-urlcomponents.swift
Last active September 2, 2017 09:13
Get Specific URLQueryItem from URLComponents
// https://hashaam.com/2017/09/02/get-specific-urlqueryitem-from-urlcomponents
func queryParam(urlString: String?, param: String) -> String? {
guard let urlString = urlString else { return nil }
guard let urlComponents = URLComponents(string: urlString) else { return nil }
guard let queryItems = urlComponents.queryItems else { return nil }
return queryItems.filter { queryItem in
return queryItem.name == param
}.first?.value
@hashaam
hashaam / calling-get-specific-urlqueryitem-from-urlcomponents.swift
Last active September 2, 2017 09:12
Calling Get Specific URLQueryItem from URLComponents
// https://hashaam.com/2017/09/02/get-specific-urlqueryitem-from-urlcomponents
if let utmTermValue = queryParam(urlString: "http://www.example.com/?utm_source=adsite&utm_campaign=adcampaign&utm_term=adkeyword", param: "utm_term") {
// handle utmTermValue
}
@hashaam
hashaam / adjust-scrollview-content-inset-based-on-keyboard-frame-viewwillappear.swift
Last active September 2, 2017 10:20
Adjust Scroll View Content Inset Based on Keyboard Frame
// https://hashaam.com/2017/09/02/adjust-scroll-view-content-inset-based-on-keyboard-frame
override func viewWillAppear(_ animated: Bool) {
NotificationCenter.default.addObserver(self, selector: #selector(adjustForKeyboardHandler(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(adjustForKeyboardHandler(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
@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 / google-screenshot-firebase-cloud-function.ts
Created January 16, 2019 05:52
This firebase cloud function takes screenshot of google home page and saves in firebase storage bucket under screenshots/google.png, every time it is run.
import * as functions from 'firebase-functions';
import * as admin from "firebase-admin";
import * as puppeteer from "puppeteer";
admin.initializeApp()
export const takeGoogleScreenshot = functions
.runWith({ memory: "1GB" })
.https.onRequest(async (request, response) => {
const browser = await puppeteer.launch({
@hashaam
hashaam / specify-root-view-controller-of-uiwindow-in-scenedelegate.swift
Last active August 16, 2020 16:56
Specify root view controller of UIWindow in SceneDelegate
// Youtube: https://www.youtube.com/watch?v=7AlZxClmhPw
// Source: https://hashaam.com/2020/07/30/creating-camera-application-with-avfoundation/
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
let nibName = String(describing: LaunchViewController.self)
let bundle = Bundle.main
let launchViewController = LaunchViewController(nibName: nibName, bundle: bundle)
window?.rootViewController = launchViewController
@hashaam
hashaam / background-color-of-view-in-viewcontroller.swift
Last active August 16, 2020 16:56
Change background color of view in view controller
// Youtube: https://www.youtube.com/watch?v=7AlZxClmhPw
// Source: https://hashaam.com/2020/07/30/creating-camera-application-with-avfoundation/
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
}
// Youtube: https://www.youtube.com/watch?v=7AlZxClmhPw
// Source: https://hashaam.com/2020/07/30/creating-camera-application-with-avfoundation/
import UIKit
class RequestCameraAuthorizationView: UIView {
@IBOutlet private weak var contentView: UIView!
override init(frame: CGRect) {