swift - ios地理定位请求窗口一闪而过?
怪我咯
怪我咯 2017-04-17 17:11:37
0
3
737
import UIKit
import CoreLocation;

class UserLocation: NSObject {
    
    var userPosition: CLLocationCoordinate2D?
    let manager = CLLocationManager()
    
    /// 获取用户位置授权,定位用户当前坐标
    func startUserlocation() {
        
        manager.delegate = self;
        manager.desiredAccuracy = kCLLocationAccuracyBest;  //设置精确度
        manager.distanceFilter = 50 ;   //重新定位变化距离
        
        
        if #available(iOS 8, *) {   //ios8 或更高
            print("request");
            //manager.requestWhenInUseAuthorization();    //当app进入前台开始定位
            manager.requestAlwaysAuthorization();       //请求始终进行定位
        }
        
        if !CLLocationManager.locationServicesEnabled() {
            print("设备没开启定位");return;
        }
        
        manager.startUpdatingLocation();    //开始定位
        
        print("start");
    }

}

extension UserLocation: CLLocationManagerDelegate {
    // 地理位置更新
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print(locations)
        if(locations.count <= 0 ){
            return;
        }
        
        print("定位定位")
        
        let userPos = locations.last! as CLLocation  //获得最新一次定位信息
        userPosition = userPos.coordinate
        
        manager.stopUpdatingLocation()
    }
    
    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        print(status);
    }
    
    //定位失败
    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print(error);
    } 
}

如上:
写了一个location类,然后再界面上点击一个按钮触发定位:

func richScan(){
    var test = UserLocation();
    test.startUserlocation();
}

但是弹出请求权限接口后 一闪而过,根本来不及点。这是什么情况。。。

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(3)
PHPzhong

test 被 释放了,arc 的问题,设置一个成员变量强引用,如:self.test = UserLocation();

Peter_Zhu

Correct answer upstairs.
Writing UserLocation as a singleton seems to solve the problem.

小葫芦

If this problem is not solved yet, please read this article http://www.cnblogs.com/huangzs/p/5334130... If it is not solved yet, please message me privately

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template