Posts

Alarm in iOS Swift

In iOS, we don't have Alarm manager.But we will use local notification as an alarm manager. We will play the sound as like alarm. Code:          let notification = UILocalNotification ()         notification. soundName = "alarm.mp3" // add soundfile to your project and put the name here         notification. timeZone = NSTimeZone . local         notification. userInfo = data  as ? [ String : AnyObject ] //set the notification data here and get and process in the receiving end         notification. fireDate = NsDate()  // set the alarm time          UIApplication . shared . scheduleLocalNotification (notification)   // fire the notification

Smallest array from current location iOS Swift

The below function was used to return the smallest distance location arrays from Location you sent. Also, the mutable array must have a model with lat, lang and distance values. Code:     func matchLocation(currentLocation: CLLocation ,geoPlace: NSMutableArray ) -> NSMutableArray {                  var smallestDistanceArray = NSMutableArray ()                  for location in geoPlace {             let tempLocation = location             let markLocation: CLLocation = CLLocation (latitude: tempLocation. latitude , longitude: tempLocation. longitide )             let distance = markLocation. distance (from: currentLocation)              if   distance < Double (tempLocation. distance ) {         ...

Global LocationManager Singleton class iOS Swift

If you are using location based values in all the screen of apps then its a better to implement as a singleton class.First import the core location.The class will be as follows. Code: import UIKit import CoreLocation protocol LocationServiceDelegate {     func tracingLocation(currentLocation: CLLocation )     func tracingLocationDidFailWithError(error: NSError ) } class LocationSingleton: NSObject , CLLocationManagerDelegate {     var locationManager: CLLocationManager ?     var lastLocation: CLLocation ?     var delegate: LocationServiceDelegate ?     static let sharedInstance: LocationSingleton = {         let instance = LocationSingleton ()         return instance     }()          override init () {         super . init ()         self . lo...

Hide the keyBoard when tapping on the View iOS Swift

If the user is started typing in the text field and if the user taps on the view then keyboard wants to be dismissed then as follows. Code: class  ViewController: UIViewController , UIGestureRecognizerDelegate { } extension ViewController {      func hideKeyboardWhenTappedAround() {         let tap: UITapGestureRecognizer = UITapGestureRecognizer (target: self , action: #selector ( ViewController . dismissKeyboard ))         tap. cancelsTouchesInView = false         view . addGestureRecognizer (tap)     }          func dismissKeyboard() {         view . endEditing ( true )     } }

Display date as hour and minute in text field iOS Swift

If you want to display the date as time only with an hour and minutes then follow the code.                 Code:                 let dates =  NSDate ()                 let dateFormater1 = DateFormatter ()                 dateFormater1. dateFormat =  "h:mm a"                 let dateString =  dateFormater1. string (from:dates    as ! Date )

Sort the array of Dates in the Model iOS Swift

Sort the date array using for loop is not a great way.So we can use the below method to sort the array of dates Code:              let  DateSorted = NSMutableArray (array:datearray. sortedArray (using: [ NSSortDescriptor (key: "date" , ascending: true )])) The array model contains the key attribute named as the date.So we used the key value as date and sort using ascending order.

Print the switch status or boolean in iOS Swift

         Check the switch status. Code: print ( "Check " , (switch. isOn ) ? "True" : "false" ) Print the Boolean value. Code: print ( "Check " , (BoolVariable ) ?  "True"  :  "false" )