cari

Rumah  >  Soal Jawab  >  teks badan

swift - ios地理定位请求窗口一闪而过?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

<code>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);

    }

}</code>

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

1

2

3

4

5

6

<code>func richScan(){

    var test = UserLocation();

    test.startUserlocation();

}

 

</code>

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

怪我咯怪我咯2900 hari yang lalu818

membalas semua(3)saya akan balas

  • PHPz

    PHPz2017-04-17 17:13:37

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

    balas
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:13:37

    楼上正解。
    把UserLocation写成单例好像也能解决问题。

    balas
    0
  • 高洛峰

    高洛峰2017-04-17 17:13:37

    没解决这问题的 请看这篇文章http://www.cnblogs.com/huangzs/p/5334130... 还没解决的 私聊我

    balas
    0
  • Batalbalas