2014-07-05 57 views
0

首先,我是iOS开发新手,所以请不要向我解释Objective-C的例子。CLLocationManagerDelegate没有捕获事件

我的问题是:
我做了这个类:

import UIKit 
import CoreLocation 

class BeaconRadar: NSObject, CLLocationManagerDelegate { 

    var manager : CLLocationManager! 
    var region : CLBeaconRegion! 

    var seenError   = false 
    var locationFixAchieved = false 
    var locationStatus  = "Not Started" 

    init() { 
     super.init() 

     var major = CLBeaconMajorValue(10) 
     var uuid = NSUUID(UUIDString: "E2C56DB5-DFFB-48D2-B060-D0F5A71096E0") 
     var id  = "test" 

     manager = CLLocationManager() 
     region = CLBeaconRegion(proximityUUID: uuid, major: major, identifier: id) 

     manager.delegate  = self 
     manager.desiredAccuracy = kCLLocationAccuracyBest 

     manager.requestAlwaysAuthorization() 
     manager.startRangingBeaconsInRegion(region) 
    } 

    func locationManager(manager: CLLocationManager!, didRangeBeacons beacons: AnyObject[], inRegion region: CLBeaconRegion!){ 

     if (locationFixAchieved == false) { 
      locationFixAchieved = true 

      println(beacons) 

     } 

    } 

    // If failed 
    func locationManager(_manager: CLLocationManager!, rangingBeaconsDidFailForRegion region: CLBeaconRegion!, withError error: NSError!){ 

     manager.stopUpdatingLocation() 
     if (error) { 
      if (seenError == false) { 
       seenError = true 
       println("Error getting location") 
      } 
     } 
    } 

    // // authorization status 
    func locationManager(_manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus){ 
     println("2") 
     var shouldIAllow = false 

     switch status { 
      case CLAuthorizationStatus.Restricted: 
       locationStatus = "Restricted Access" 
      case CLAuthorizationStatus.Denied: 
       locationStatus = "User denied access" 
      case CLAuthorizationStatus.NotDetermined: 
       locationStatus = "Status not determined" 
      default: 
       locationStatus = "Allowed Access" 
       shouldIAllow = true 
     } 

     NSNotificationCenter.defaultCenter().postNotificationName("LabelHasbeenUpdated", object: nil) 
     if (shouldIAllow == true) { 
      println("Location Allowed") 
      // Start location services 
      manager.startUpdatingLocation() 
     } else { 
      println("Denied access: \(locationStatus)") 
     } 
    } 
} 

和我的viewController是:

class ViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view, typically from a nib. 

     var location = BeaconRadar() 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

} 

我期待得到消息之一在授权状态的实现func中。 (BeaconRadar中的最后一个)。但我什么也没有。

如果我将实现在的viewController,而不是在新的类 所有这些方法(即:

class ViewController: UIViewController, CLLocationManagerDelegate { 
..... 
} 

那么它会工作得很好...

我失去了东西?

回答

2

您已将BeaconRadar实例声明为viewDidLoad中的局部变量,因此只要这个乐趣ction退出,BeaconRadar实例被释放。

你应该移动BeaconRegion变出来的功能 -

class ViewController: UIViewController { 

    let location = BeaconRadar() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view, typically from a nib. 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

} 
+0

谢谢!!!! noob错误 – guyklainer