Skip to content

Instantly share code, notes, and snippets.

View malcommac's full-sized avatar
👋
Nice to meet u

Daniele Margutti malcommac

👋
Nice to meet u
View GitHub Profile
@malcommac
malcommac / SwiftLocation_ReverseGeocoding.swift
Last active May 1, 2016 09:33
Reverse Geocoding with SwiftLocation
let address = "1 Infinite Loop, Cupertino (USA)"
LocationManager.shared.reverseAddress(address: address, onSuccess: { foundPlacemark in
// foundPlacemark is a CLPlacemark object
}) { error in
// failed to reverse geocoding due to an error
}
let coordinates = CLLocationCoordinate2DMake(41.890198, 12.492204)
// Use Google service to obtain placemark
let coordinates = CLLocationCoordinate2DMake(41.890198, 12.492204)
let request = BeaconManager.shared.monitorGeographicRegion(centeredAt: coordinates, radius: 1400, onEnter: { in
// on enter in region
}) { in
// on exit from region
}
@malcommac
malcommac / SwiftLocation_BeaconMonitor.swift
Created May 1, 2016 09:44
Beacon Monitor with SwiftLocation
// MONITOR BEACON FAMILY
let request = BeaconManager.shared.monitorForBeaconFamily(proximityUUID: familyUUID, onRangingBeacons: { beaconsFound in
// beaconsFound is an array of found beacons ([CLBeacon])
}) { error in
// something bad happened
}
// Sometimes in the future you may decide to stop observing
request.stop()
// MONITOR BEACON
@malcommac
malcommac / ThenAsyncExampleTask.swift
Last active July 28, 2016 19:20
How to create a simple async task in ThenAsync
enum Error: ErrorType {
case Network(message: String?)
case EmptyData
case MalformedImage(data: NSData)
case DuplicateFile(path: String)
case Generic(message: String)
}
func downloadImage(url: NSURL, in context: TaskContext) -> Task<UIImage> {
let task = Task<UIImage>(in: context) { (fullfill, reject) -> (Void) in
@malcommac
malcommac / ThenAsyncExampleCall.swift
Created July 28, 2016 19:01
Call a previously defined Task function
// https://gist.github.com/malcommac/a663796c162995523e6c0871e9a08820
let url = NSURL(string: "http://seeklogo.com/images/S/swift-logo-E9182990F5-seeklogo.com.gif")!
downloadImage(url, in: .Background).then { image in
// Hell yeah! we have our image, already unwrapped!
}.failure { error in
// Something went wrong
}
@malcommac
malcommac / ThenAsyncExampleTask2.swift
Last active July 28, 2016 19:26
Another async function
func saveToDisk(name:String, image: UIImage, in context: TaskContext) -> Task<NSURL> {
let task = Task<NSURL>(in: context) { (fullfill, reject) -> (Void) in
let filemanager = NSFileManager.defaultManager()
let documentsPath : AnyObject = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true).first!
let fPath = documentsPath.stringByAppendingString("/\(name)") as String
if(!filemanager.fileExistsAtPath(fPath)) {
guard let data = UIImagePNGRepresentation(image) else {
reject(Error.Generic(message: "Cannot convert image"))
return
}
@malcommac
malcommac / ThenAsyncChain.swift
Created July 28, 2016 19:47
Chain Task with ThenAsync and use the result of a task
downloadImage(imgURL, in: .Background).then { image in
saveToDisk("myImage", image: image, in: .Background) }.then { url in
print("Image from \(imgURL) is now saved in \(url)")
}
fetchUserData().then(downloadAvatar).then(fetchFriendsList).finally(reloadList)
@malcommac
malcommac / DateInRegion_Create.swift
Created September 24, 2016 14:26
New DateInRegion from absolute date
// Create a Region in Rome TimeZone with Gregorian Calendar and Italy locale settings
let regionRome = Region(tz: TimeZoneName.europeRome, cal: CalendarName.gregorian, loc: LocaleName.italianItaly)
// Resulting object is DateInRegion which express the current moment in Rome
let dateInRome = DateInRegion(absoluteDate: Date(), in: regionRome)
@malcommac
malcommac / DateInRegion_NewLocal.swift
Created September 24, 2016 14:44
DateInRegion from now in current device's local settings
// Create a new DateInRegion which represent the current moment (Date()) in current device's local settings
let now = DateInRegion()