This file contains hidden or 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
| 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 |
This file contains hidden or 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
| 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 | |
| } |
This file contains hidden or 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
| // 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 |
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| // 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 | |
| } |
This file contains hidden or 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 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 | |
| } |
This file contains hidden or 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
| 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)") | |
| } |
This file contains hidden or 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
| fetchUserData().then(downloadAvatar).then(fetchFriendsList).finally(reloadList) |
This file contains hidden or 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
| // 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) |
This file contains hidden or 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
| // Create a new DateInRegion which represent the current moment (Date()) in current device's local settings | |
| let now = DateInRegion() |