2015-06-12 21 views
0

我一直在使用大量的教程,並且每個教程都持續有完全相同的問題。當我運行下面的代碼或其他教程吧其他的化身,我不斷收到:用Swift在CoreLocation中搜索區域的正確方法是什麼?

2015-06-12 13:50:33.797 Scanner[4599:2593734] *** Assertion failure in -[CLLocationManager startRangingBeaconsInRegion:], /SourceCache/CoreLocationFramework/CoreLocation-1756.0.20/Framework/CoreLocation/CLLocationManager.m:1129 
2015-06-12 13:50:33.798 Scanner[4599:2593734] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: region != nil' 
*** First throw call stack: 
(0x183efc2d8 0x1957200e4 0x183efc198 0x184db0ed4 0x18464f0d4 0x10008bebc 0x10008bf60 0x18893cc84 0x18893c994 0x1889431d0 0x188940880 0x1889b28ec 0x188bc6a94 0x188bc9208 0x188bc7778 0x18c7053c8 0x183eb427c 0x183eb3384 0x183eb19a8 0x183ddd2d4 0x1889a843c 0x1889a2fac 0x1000923b4 0x195d9ea08) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

我切換(地區)(區域!),並得到一個輸出說,雖然展開意想不到的零發生錯誤。

我非常惱火,並嘗試了一些可能性。有誰知道問題是什麼?它在屏幕加載時崩潰。

import UIKit 
import CoreLocation 


class ViewController: UIViewController, CLLocationManagerDelegate { 
    let locationManager = CLLocationManager() 
    let region = CLBeaconRegion(proximityUUID: NSUUID(UUIDString: "1800"), identifier: "BLE113") 

    override func viewDidLoad(){ 
     super.viewDidLoad() 

     locationManager.delegate = self; 

     if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedWhenInUse) { 
     locationManager.requestWhenInUseAuthorization() 
     } 

     locationManager.startRangingBeaconsInRegion(region) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

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

} 

回答

0

以下是我的具有正確簽名的代碼版本。請注意,NSUUID:UUIDString是一個可選的初始值設定項,可以返回nil。

進口的UIKit 進口CoreLocation

類的ViewController:UIViewController中,CLLocationManagerDelegate {

let locationManager = CLLocationManager() 
var region: CLBeaconRegion? 
override func viewDidLoad(){ 
    if let proximityUUID = NSUUID(UUIDString: "40ACF125-D06F-492D-BD4F-0FC9D93F906C") { // generated using uuidgen tool 

     self.region = CLBeaconRegion(proximityUUID: proximityUUID, identifier: "BLE113") 
     if let region = self.region { 
      locationManager.delegate = self 

      if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedWhenInUse) { 
       locationManager.requestWhenInUseAuthorization() 
      } 

      locationManager.startRangingBeaconsInRegion(region) 
     } 
    } 
    super.viewDidLoad() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 


func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) { 
    print(beacons) 
} 

}

運行一下,看看你在哪裏突破?你的地區是否爲零?因爲如果您的NSUUID構造不正確,CLBeaconRegion將返回nil。

+0

我遇到了您的func locationManager版本問題。它給了我:/Users/markhardy/Desktop/Development/Scanner/Scanner/ViewController.swift:68:10:Objective-C方法'locationManager:didRangeBeacons:inRegion:'由方法提供'locationManager(_:didRangeBeacons:inRegion :) '與協議'CLLocationManagerDelegate'中的可選需求方法'locationManager(_:didRangeBeacons:inRegion :)'衝突'但是我可以通過用Anyobject替換CLBeacon來構建它' – user3246092

+0

它正在構建中,但它似乎並沒有起作用我的設備很不幸 – user3246092

+0

你可以打破並跟蹤2「如果讓」。你在使用什麼UUIDString? – Laurent

相關問題