2014-07-05 63 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