2014-10-02 27 views
0

我已經構建了一個應該接收ibeacon的類。據我所知,我做的都是對的。但它不起作用。 我的應用程序不要求位置權限,位置管理器不會開始更新。 我也在plist中設置了屬性。Swift - 獲取iBeacon - 外部類

NSLocationWhenInUseUsageDescription

NSLocationAlwaysUsageDescription

它只有當我初始化就在AppDelegate的一切工作。 正如在這裏的教程。

http://ibeaconmodules.us/blogs/news/14702963-tutorial-swift-based-ibeacon-app-development-with-corelocation-on-apple-ios-7-8

但我想這個服務外包的一類。 我不知道我在做什麼錯。我是Swift的新成員,在Objective-C中它一直工作。 任何人都可以幫助我嗎?

下面的代碼 -

的ViewController

進口的UIKit

類的ViewController:UIViewController中,BeaconServiceProtocolDelegate {

override func viewDidLoad() { 
    super.viewDidLoad() 

    //Set BackgroundImage and hide Navigationbar 
    self.view.backgroundColor = UIColor(patternImage: UIImage(named:"bg")); 
    self.navigationController?.navigationBar.hidden = true 

    ////////////////////////create new Beacon Service which is looking for beacon which are given with the parameters 
    var beaconAdvertiseService = BeaconService(parauuidString: "F0018B9B-7509-4C31-A905-1A27D39C003C",parabeaconIdentifier:"iBeaconTalentTeamAudi"); 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 

} 

//Notification if our service found a beacon 
func sendLocalNotificationWithMessage(message: String!) { 
    let notification:UILocalNotification = UILocalNotification() 
    notification.alertBody = message 
    UIApplication.sharedApplication().scheduleLocalNotification(notification) 
} 

//Delegate from BeaconService 
func foundBeacon() { 
    println("Found Beacon") 
} 

}

課程和班級 - BeaconAdvertiseService

import Foundation 
import CoreBluetooth 
import CoreLocation 

//BeaconService Delegate 
protocol BeaconServiceProtocolDelegate 
{ 
    func foundBeacon() 
} 


import Foundation 
import CoreBluetooth 
import CoreLocation 

//BeaconService Delegate 
protocol BeaconServiceProtocolDelegate 
{ 
    func foundBeacon() 
} 

class BeaconService: NSObject, CLLocationManagerDelegate{ 

    let beaconServiceDelegate:BeaconServiceProtocolDelegate? 

    let uuidString:String//UUID Beacon as String 
    let beaconIdentifier:String//Identifier Beacon 
    let beaconUUID:NSUUID//UUID Beacon 
    let beaconRegion:CLBeaconRegion//Beacon Region 
    let locationManager:CLLocationManager ////location manager 

    ////////////////////////Init class (constructor) // Params UUID as String & and Identifier from beacon 
    init(parauuidString:String,parabeaconIdentifier:String) { 



     self.uuidString   = parauuidString 
     self.beaconIdentifier = parabeaconIdentifier 
     self.beaconUUID   = NSUUID(UUIDString: self.uuidString) 
     self.beaconRegion  = CLBeaconRegion(proximityUUID:self.beaconUUID,identifier: self.beaconIdentifier) 
     self.locationManager   = CLLocationManager(); 


     super.init() 
     self.initLocationMangager() 

    } 

    ////////////////////////Init location manager 
    private func initLocationMangager() 
    { 
     println(self.locationManager) 

     if(self.locationManager.respondsToSelector("requestAlwaysAuthorization")) { 
      locationManager.requestAlwaysAuthorization() 
     } 

     self.locationManager.delegate = self 
     self.locationManager.pausesLocationUpdatesAutomatically = false 

     self.locationManager.startMonitoringForRegion(beaconRegion) 
     self.locationManager.startRangingBeaconsInRegion(beaconRegion) 
     self.locationManager.startUpdatingLocation() 
    } 


    ////////////////////////////////////////////////////////// 
    ////////////////////////////////////////////////////////// 
    ////////////////////////LocationManager Delegates 
    ////////////////////////////////////////////////////////// 
    ////////////////////////////////////////////////////////// 

    ////////////////////////LocationManager didStartMonitoringForRegion 
    func locationManager(manager: CLLocationManager!, didStartMonitoringForRegion region: CLRegion!) { 
     manager.startRangingBeaconsInRegion(region as CLBeaconRegion) 
     println("Did Start") 
    } 

    ////////////////////////LocationManager didEnterRegion 
    func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) { 
     manager.startRangingBeaconsInRegion(region as CLBeaconRegion) 
    } 

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

      var message:String = "" 

      if(beacons.count > 0) { 
       let nearestBeacon:CLBeacon = beacons[0] as CLBeacon 

       switch nearestBeacon.proximity { 
       case CLProximity.Far: 
        message = "Far" 
       case CLProximity.Near: 
        message = "Near" 
       case CLProximity.Immediate: 
        message = "Immediate" 
       case CLProximity.Unknown: 
        return 
       } 
      } else { 
       message = "No Beacons" 
      } 

      //Call BeaconServiceDelegate 
      beaconServiceDelegate?.foundBeacon() 
    } 

    ////////////////////////LocationManager monitoringDidFailForRegion 
    func locationManager(manager: CLLocationManager!, monitoringDidFailForRegion region: CLRegion!, withError error: NSError!) { 
     println(error) 
    } 

    ////////////////////////LocationManager didExitRegion 
    func locationManager(manager: CLLocationManager!, didExitRegion region: CLRegion!) { 
     manager.stopRangingBeaconsInRegion(region as CLBeaconRegion) 

    } 

} 

回答

0

它看起來像你的分配你的viewDidLoad結束廣告服務給本地變量去。此方法將在此方法結束時銷燬(一行後);)

請確保將您的廣告客戶分配給實例變量或屬性,並將self(您的viewcontroller)作爲代理分配給您的廣告客戶。

這應該可以做到。

0

我看到至少有一兩件事很簡單。它看起來像initLocationMangager()方法永遠不會被調用。您應該從init(parauuidString:String,parabeaconIdentifier:String)方法中添加對此方法的調用。

0

對不起,我忘記了,我已經出去了。 但它也不會與呼叫

self.initLocationMangager()