Skip to content

Instantly share code, notes, and snippets.

View stinger's full-sized avatar

Ilian Konchev stinger

View GitHub Profile
@stinger
stinger / Swift3Dates.swift
Last active May 21, 2018 02:04
Swift 3: Working with dates
//: # Swift 3: Working with dates
import Foundation
let date = Date()
let myLocale = Locale(identifier: "bg_BG")
//: ### Setting an application-wide `TimeZone`
//: Notice how we use if-let in case the abbreviation is wrong. It will fallback to the default timezone in that case.
if let myTimezone = TimeZone(abbreviation: "EEST") {
print("\(myTimezone.identifier)")
@stinger
stinger / Swift3GCDURLSessionDataTask.swift
Last active March 26, 2018 21:36
Swift 3: GCD and URLSessionDataTask with a completionHandler
//: # Swift 3: CGD and URLSessionDataTask
import Foundation
import PlaygroundSupport
//: ### Create background queue
let queue = DispatchQueue.global(qos: .background)
//: ### Computed variable
var time:DispatchTime! {
@stinger
stinger / Swift3Base64.swift
Created June 17, 2016 14:58
Swift 3: Base64 encoding and decoding strings
//: # Swift 3: Base64 encoding and decoding
import Foundation
extension String {
//: ### Base64 encoding a string
func base64Encoded() -> String? {
if let data = self.data(using: .utf8) {
return data.base64EncodedString()
}
return nil
@stinger
stinger / Swift3URLComponent.swift
Created June 21, 2016 11:03
Swift 3: Using URLComponents
//: # Swift 3: Using URLComponents
import Foundation
//: ### Compose the componens
var url = URLComponents()
url.scheme = "http"
url.host = "google.com"
//: ### Pass the query string parameters
url.queryItems = [
URLQueryItem(name: "test", value: "data"),
@stinger
stinger / Swift3DataTaskDelegate.swift
Last active March 25, 2023 22:12
Swift 3: URLSessionDelegate
//: # Swift 3: URLSessionDelegate
//: The following example shows how to use `OperationQueue` to queue the network requests. This is useful in many ways (for delaying queued requests when the networking goes down for example)
import Foundation
import PlaygroundSupport
class Requester:NSObject {
let opQueue = OperationQueue()
var response:URLResponse?
@stinger
stinger / Swift3JSONStructs.swift
Last active May 26, 2023 10:58
Swift 3: JSON-serializable structs using protocols
//: # Swift 3: JSON-serializable structs using protocols
//: Most of the implementation is based on the code in [this blog post](http://codelle.com/blog/2016/5/an-easy-way-to-convert-swift-structs-to-json/)
import Foundation
//: ### Defining the protocols
protocol JSONRepresentable {
var JSONRepresentation: Any { get }
}
protocol JSONSerializable: JSONRepresentable {}
@stinger
stinger / MKMapView+Extensions.swift
Last active November 17, 2016 14:23
Swift 3: MKMapView Helpers
import UIKit
import MapKit
extension MKMapView {
func showLocation(at coordinate: CLLocationCoordinate2D) {
self.removeOverlays(self.overlays)
self.removeAnnotations(self.annotations)
let annotation = MKPointAnnotation()
@stinger
stinger / ios10LocalNotifications.swift
Created November 17, 2016 14:28
Swift 3: ios10 Local notifications
func showPushNotification(title: String, details: String) {
if #available(iOS 10.0, *) {
let interval = TimeInterval(1)
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: false)
let content = UNMutableNotificationContent()
content.title = title
content.body = details
let req = UNNotificationRequest(identifier: "localPushNotification", content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.getNotificationSettings(completionHandler: { settings in
@stinger
stinger / SDPresentationManager.swift
Last active September 25, 2019 08:58
Swift 3: Custom Presentation Manager
import UIKit
final class SDPresentationManager: NSObject, UIViewControllerTransitioningDelegate {
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return SDModalPresentationController(presentedViewController: presented, presenting: source)
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return SDModalTransitionPresentationAnimator()
@stinger
stinger / AVPhotoCaptureSample.swift
Last active December 4, 2021 15:21
An example of using the new AVPhotoCaptureOutput class in iOS 10
// Add the following to the Info.plist
// <key>NSCameraUsageDescription</key>
// <string>Allow our app to take photos</string>
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: {[weak self] status in
if status {
let session = AVCaptureSession()
for device in AVCaptureDevice.devices() {
if let device = device as? AVCaptureDevice, device.position == AVCaptureDevicePosition.back {
self.device = device