定位框一闪而过 iOS Swift
需求:获取经纬度。
方案:我自定义了一个类模块CLLocationModule.swift
备注以下代码里
let IS_IOS8 = (UIDevice.currentDevice().systemVersion as NSString).doubleValue >= 8.0
最开始的代码
import UIKit
import CoreLocation
class CLLocationModule: NSObject ,CLLocationManagerDelegate{
var latitude:Double?
var longitude:Double?
var city:NSString?
func GetLatitudeLongitudeAndCity(){
if CLLocationManager.locationServicesEnabled() == false {
print("此设备不能定位")
return
}
var locManager = CLLocationManager()
locManager.delegate = self
locManager.desiredAccuracy = kCLLocationAccuracyBest
locManager.distanceFilter = 1000.0
//设置定位权限仅ios8有意义
if IS_IOS8 {
locManager.requestWhenInUseAuthorization()// 前台定位
locManager.requestAlwaysAuthorization()
}
locManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
for location in locations {
print("纬度:%g",location.coordinate.latitude);
print("经度:%g",location.coordinate.longitude);
latitude = location.coordinate.latitude
longitude = location.coordinate.longitude
self.saveLatitude(latitude!.description)
self.saveLongitude(longitude!.description)
let geocoder: CLGeocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location) { (placemarks, error) in
if error != nil {
print("reverse geodcode fail: \(error!.localizedDescription)")
return
}
let pm = placemarks! as [CLPlacemark]
if (pm.count > 0){
for placemark :CLPlacemark in placemarks! {
let placemarkDict = placemark.addressDictionary
let placemarkStr = placemarkDict!["State"]
self.city = placemarkStr?.substringToIndex((placemarkStr?.length)!-1)
self.saveCity(self.city!)
}
}else{
print("No Placemarks!")
}
}
}
manager.stopUpdatingLocation()
}
func locationManager(manager:CLLocationManager, didFailWithError error:NSError) {
print("locationManager error");
}
func saveLatitude(latitude: NSString) {
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(latitude, forKey: "latitude")
defaults.synchronize()
}
func readLatitude() -> NSString {
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
let latitude = defaults.objectForKey("latitude") as! NSString
return latitude;
}
func saveLongitude(longitude: NSString) {
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(longitude, forKey: "longitude")
defaults.synchronize()
}
func readLongitude() -> NSString {
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
let longitude = defaults.objectForKey("longitude") as! NSString
return longitude;
}
func saveCity(city: NSString) {
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(city, forKey: "city")
defaults.synchronize()
}
func readCity() -> NSString {
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
let city = defaults.objectForKey("city") as! NSString
return city;
}
}
外部调用是:
CLLocationModule().GetLatitudeLongitudeAndCity()
结果:定位框一闪而过 (允许 不允许那个弹出框) 导致不走代理方法didUpdateLocations 最后查阅网页 知道

(a)locManager没定义为全局变量
(b)自定义的CLLocationModule.swift这个类 由于ARC的缘故 自动释放了
针对以上(a)问题的解决办法为自定义类CLLocationModule.swift中将var locManager = CLLocationManager() 改为全局变量
var locManager: CLLocationManager!
locManager = CLLocationManager()
针对以上(b)问题的解决方案有两种
(1)外部调用时改为:(改为强引用)
var aCLLocationModule = CLLocationModule()
self.aCLLocationModule.GetLatitudeLongitudeAndCity()
(2)自定义的类CLLocationModule.swift改为单例
//单例
private static let aSharedInstance: CLLocationModule = CLLocationModule()
private override init() {}
class func sharedInstance() -> CLLocationModule {
return aSharedInstance
}
外部调用时改为:
CLLocationModule.sharedInstance().GetLatitudeLongitudeAndCity()
成功的代码附上两份
(1)单例
import UIKit
import CoreLocation
class CLLocationModule: NSObject ,CLLocationManagerDelegate{
//单例
private static let aSharedInstance: CLLocationModule = CLLocationModule()
private override init() {}
class func sharedInstance() -> CLLocationModule {
return aSharedInstance
}
var locManager: CLLocationManager!
var latitude:Double?
var longitude:Double?
var city:NSString?
func GetLatitudeLongitudeAndCity(){
if CLLocationManager.locationServicesEnabled() == false {
print("此设备不能定位")
return
}
locManager = CLLocationManager()
locManager.delegate = self
locManager.desiredAccuracy = kCLLocationAccuracyBest
locManager.distanceFilter = 1000.0
//设置定位权限仅ios8有意义
if IS_IOS8 {
locManager.requestWhenInUseAuthorization()// 前台定位
locManager.requestAlwaysAuthorization()
}
locManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
for location in locations {
print("纬度:%g",location.coordinate.latitude);
print("经度:%g",location.coordinate.longitude);
latitude = location.coordinate.latitude
longitude = location.coordinate.longitude
self.saveLatitude(latitude!.description)
self.saveLongitude(longitude!.description)
let geocoder: CLGeocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location) { (placemarks, error) in
if error != nil {
print("reverse geodcode fail: \(error!.localizedDescription)")
return
}
let pm = placemarks! as [CLPlacemark]
if (pm.count > 0){
for placemark :CLPlacemark in placemarks! {
let placemarkDict = placemark.addressDictionary
let placemarkStr = placemarkDict!["State"]
self.city = placemarkStr?.substringToIndex((placemarkStr?.length)!-1)
self.saveCity(self.city!)
}
}else{
print("No Placemarks!")
}
}
}
manager.stopUpdatingLocation()
}
func locationManager(manager:CLLocationManager, didFailWithError error:NSError) {
print("locationManager error");
}
func saveLatitude(latitude: NSString) {
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(latitude, forKey: "latitude")
defaults.synchronize()
}
func readLatitude() -> NSString {
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
let latitude = defaults.objectForKey("latitude") as! NSString
return latitude;
}
func saveLongitude(longitude: NSString) {
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(longitude, forKey: "longitude")
defaults.synchronize()
}
func readLongitude() -> NSString {
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
let longitude = defaults.objectForKey("longitude") as! NSString
return longitude;
}
func saveCity(city: NSString) {
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(city, forKey: "city")
defaults.synchronize()
}
func readCity() -> NSString {
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
let city = defaults.objectForKey("city") as! NSString
return city;
}
}
外部调用:
CLLocationModule.sharedInstance().GetLatitudeLongitudeAndCity()
(2)强引用
import UIKit
import CoreLocation
class CLLocationModule: NSObject ,CLLocationManagerDelegate{
var locManager: CLLocationManager!
var latitude:Double?
var longitude:Double?
var city:NSString?
func GetLatitudeLongitudeAndCity(){
if CLLocationManager.locationServicesEnabled() == false {
print("此设备不能定位")
return
}
locManager = CLLocationManager()
locManager.delegate = self
locManager.desiredAccuracy = kCLLocationAccuracyBest
locManager.distanceFilter = 1000.0
//设置定位权限仅ios8有意义
if IS_IOS8 {
locManager.requestWhenInUseAuthorization()// 前台定位
locManager.requestAlwaysAuthorization()
}
locManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
for location in locations {
print("纬度:%g",location.coordinate.latitude);
print("经度:%g",location.coordinate.longitude);
latitude = location.coordinate.latitude
longitude = location.coordinate.longitude
self.saveLatitude(latitude!.description)
self.saveLongitude(longitude!.description)
let geocoder: CLGeocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location) { (placemarks, error) in
if error != nil {
print("reverse geodcode fail: \(error!.localizedDescription)")
return
}
let pm = placemarks! as [CLPlacemark]
if (pm.count > 0){
for placemark :CLPlacemark in placemarks! {
let placemarkDict = placemark.addressDictionary
let placemarkStr = placemarkDict!["State"]
self.city = placemarkStr?.substringToIndex((placemarkStr?.length)!-1)
self.saveCity(self.city!)
}
}else{
print("No Placemarks!")
}
}
}
manager.stopUpdatingLocation()
}
func locationManager(manager:CLLocationManager, didFailWithError error:NSError) {
print("locationManager error");
}
func saveLatitude(latitude: NSString) {
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(latitude, forKey: "latitude")
defaults.synchronize()
}
func readLatitude() -> NSString {
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
let latitude = defaults.objectForKey("latitude") as! NSString
return latitude;
}
func saveLongitude(longitude: NSString) {
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(longitude, forKey: "longitude")
defaults.synchronize()
}
func readLongitude() -> NSString {
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
let longitude = defaults.objectForKey("longitude") as! NSString
return longitude;
}
func saveCity(city: NSString) {
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(city, forKey: "city")
defaults.synchronize()
}
func readCity() -> NSString {
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
let city = defaults.objectForKey("city") as! NSString
return city;
}
}
外部调用:
var aCLLocationModule = CLLocationModule()
self.aCLLocationModule.GetLatitudeLongitudeAndCity()
定位框一闪而过 iOS Swift的更多相关文章
- iOS swift的xcworkspace多项目管理(架构思想)
iOS swift的xcworkspace多项目管理(架构思想) 技术说明: 今天在这里分享 swift下的 xcworkspace多项目管理(架构思想),能为我们在开发中带来哪些便捷?能为我们对整 ...
- iOS Swift 模块练习/swift基础学习
SWIFT项目练习 SWIFT项目练习2 iOS Swift基础知识代码 推荐:Swift学习使用知识代码软件 0.swift中的宏定义(使用方法代替宏) 一.视图 +控件 1.UIImag ...
- ios swift 实现饼状图进度条,swift环形进度条
ios swift 实现饼状图进度条 // // ProgressControl.swift // L02MyProgressControl // // Created by plter on 7/2 ...
- Building gRPC Client iOS Swift Note Taking App
gRPC is an universal remote procedure call framework developed by Google that has been gaining inter ...
- iOS Swift WisdomScanKit图片浏览器功能SDK
iOS Swift WisdomScanKit图片浏览器功能SDK使用 一:简介 WisdomScanKit 由 Swift4.2版编写,完全兼容OC项目调用. WisdomScanKit的 ...
- iOS Swift WisdomScanKit二维码扫码SDK,自定义全屏拍照SDK,系统相册图片浏览,编辑SDK
iOS Swift WisdomScanKit 是一款强大的集二维码扫码,自定义全屏拍照,系统相册图片编辑多选和系统相册图片浏览功能于一身的 Framework SDK [1]前言: 今天给大家 ...
- iOS Swift WisdomHUD 提示界面框架
iOS Swift WisdomHUD 提示界面框架 Framework Use profile(应用简介) 一:WisdomHUD简介 今天给大家介绍一款iOS的界面显示器:WisdomHUD,W ...
- iOS Swift WisdomKeyboardKing 键盘智能管家SDK
iOS Swift WisdomKeyboardKing 键盘智能管家SDK [1]前言: 今天给大家推荐个好用的开源框架:WisdomKeyboardKing,方面iOS日常开发,优点和功能请 ...
- iOS swift项目IM实现,从长连接到数据流解析分析之Socket
iOS swift项目IM实现,从长连接到底层数据解析分析之Socket 一:项目简介: 去年开始接手了一个国企移动项目,项目的需求是实现IM即时通讯功能. * 一期版本功能包括了: ...
随机推荐
- 深入理解DOM节点类型第二篇——文本节点Text
× 目录 [1]特征 [2]空白 [3]属性[4]方法[5]性能 前面的话 文本节点顾名思义指向文本的节点,网页上看到的文字内容都属于文本节点.该节点简单直观,本文将详细介绍该部分内容 特征 文本节点 ...
- Android多媒体框架图
Android多媒体整体架构图 MediaPlayer框架图 Camera框架图 SoundRecorder框架图 VideoCamera框架图 OpenCore与Skia ALSA Audio框架图 ...
- 【容器云】十分钟快速构建 Influxdb+cadvisor+grafana 监控
本文作者:七牛云布道师@陈爱珍,DBAPlus社群联合发起人.前新炬技术专家.多年企业级系统的应用运维及分布式系统实战经验.现专注于容器.微服务及DevOps落地的研究与实践. 安装过程 三个都直接下 ...
- ASP.NET压缩输出的HTML内容
在ASP.NET中,怎么压缩输出的HTML内容,怎么替换HTML中的换行符,空白,TAB等符号呢? 1.新建一个基类,继承自System.Web.UI.Page,代码如下: using System. ...
- iOS系列 基础篇 06 标签和按钮 (Label & Button)
iOS系列 基础篇 06 标签和按钮 (Label & Button) 目录: 标签控件 按钮控件 小结 标签和按钮是两个常用的控件,下面咱们逐一学习. 1. 标签控件 使用Single Vi ...
- Oracle hint
1.use_concat 网上说法: CONCATENATION和UNION/UNION ALL操作比较类似,根据OR查询条件,将一个查询分解为两个或更多的部分,然后在去掉两个部分重复的记录.由于CO ...
- SQLite学习笔记(十一)&&虚拟机原理
前言 我们知道任何一种关系型数据库管理系统都支持SQL(Structured Query Language),相对于文件管理系统,用户不用关心数据在数据库内部如何存取,也不需要知道底层的存储 ...
- mongodb 3.x 之实用新功能窥看[1] ——使用TTLIndex做Cache处理
mongodb一直都在不断的更新,不断的发展,那些非常好玩也非常实用的功能都逐步加入到了mongodb中,这不就有了本篇对ttlindex的介绍, 刚好我们的生产业务场景中就有这个一个案例... 一: ...
- linux ssh远程免登陆
一.备份: 操作之前先将/root/.ssh/下的known_hosts备份成known_hosts.bak
- Mac下安装GIT的坑
先去 https://git-scm.com/download/mac 下载 GIT 客户端 双击安装,界面中有三个文件 接着双节 .pkg 文件,却提示无法安装 解决方式是按住 Control ,再 ...