Showing posts with label Swift. Show all posts
Showing posts with label Swift. Show all posts

Tuesday, February 14, 2017

Building iOS Map Applications in Swift 3 (2)

Building iOS Map Applications in Swift 3 (2)
Normally, the general steps for getting longitude and latitude of user location.
  1. Add CoreLocation.framework to BuildPhases -> Link Binary With Libraries (no longer necessary as of XCode 7.2.1)
  2. Add import CoreLocation to your class - probably ViewController.swift
  3. Add CLLocationManagerDelegate to your class declaration
  4. Add
    NSLocationWhenInUseUsageDescription and
    NSLocationAlwaysUsageDescription
    to plist
  5. Initialize location manager :
locationManager = CLLocationManager()
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
get User Location By:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    var userLocation:CLLocation = locations[0] as! CLLocation
    let long = userLocation.coordinate.longitude;
    let lat = userLocation.coordinate.latitude;
    //Do What ever you want with it       
}

Tuesday, January 17, 2017

Close iOS Keyboard Using Swift

Close iOS Keyboard Using Swift
There are two ways to close iOS keyboard.

1. Close the virtual keyboard while finishing typing at any textField

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}

2. Close the virtual keyboard while one or more fingers touch down in a view or window

Assume that finish typing in passwordFiled.
@IBOutlet weak var passwordField: UITextField!
Add passwordField.delegate = self to function viewDidLoad.
override func viewDidLoad() {
    super.viewDidLoad()    
    passwordField.delegate   = self
}
Add a function touchesBegan.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
    self.passwordField.resignFirstResponder()
    super.touchesBegan(touches, with:event)
}
That’s it!

Wednesday, November 30, 2016

Building iOS Map Applications in Swift 3

Building iOS Map Applications in Swift 3
When using Mapkit in Xcode, all we need is to do configurations in the following steps:

1. Enable Maps capability

project > capabilities > Maps > on

2. Adding import libraries

Add these two lines in ViewController.swift
import CoreLocation
import MapKit

3. Adding keys in info.plist

NSLocationWhenInUseUsageDescription : 需要時取得使⽤用者位置
NSLocationAlwaysUsageDescription : 不斷的取得位置

4. Create a Location.gpx if using simulator and customizing default location


Setting the default location:
product > scheme > edit scheme
scheme