This file contains 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
//: # 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)") |
This file contains 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
//: # Swift 3: CGD and URLSessionDataTask | |
import Foundation | |
import PlaygroundSupport | |
//: ### Create background queue | |
let queue = DispatchQueue.global(qos: .background) | |
//: ### Computed variable | |
var time:DispatchTime! { |
This file contains 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
//: # 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 |
This file contains 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
//: # 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"), |
This file contains 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
//: # 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? |
This file contains 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
//: # 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 {} |
This file contains 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
import UIKit | |
import MapKit | |
extension MKMapView { | |
func showLocation(at coordinate: CLLocationCoordinate2D) { | |
self.removeOverlays(self.overlays) | |
self.removeAnnotations(self.annotations) | |
let annotation = MKPointAnnotation() |
This file contains 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
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 |
This file contains 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
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() |
This file contains 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
// 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 |
OlderNewer