0
我是swift新手,已經開始嘗試弄清楚它。如果我不得不猜測,但我需要一些幫助,我知道我已經過於複雜了。在Swift中將CLBeacon的信息從AppDelegate獲取到View Controller中
我試圖使用iBeacon讀取信標的UUID,主要和次要值,然後使用它來驅動視圖控制器中的圖像。
在AppDelegate.swift文件中,我能夠獲取信息並使用println將其解開。該文件的AppDelegate是:
import UIKit
import CoreLocation
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var locationManager: CLLocationManager?
var lastProximity: CLProximity?
var lastUUID: NSUUID!
var lastBeacanIdentifier:String = ""
var lastMajorValue: NSNumber = 0.0
var lastMinorValue: NSNumber = 0.0
func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
let uuidString = "99C2E498-7606-4575-A353-5F710834E75B"
let beaconIdentifier = "co.Company"
let beaconUUID:NSUUID = NSUUID(UUIDString: uuidString)
let beaconRegion:CLBeaconRegion = CLBeaconRegion(proximityUUID: beaconUUID, identifier: beaconIdentifier)
locationManager = CLLocationManager()
if(locationManager!.respondsToSelector("requestAlwaysAuthorization")) {
locationManager!.requestAlwaysAuthorization()
}
locationManager!.delegate = self
locationManager!.pausesLocationUpdatesAutomatically = false
locationManager!.startMonitoringForRegion(beaconRegion)
locationManager!.startRangingBeaconsInRegion(beaconRegion)
locationManager!.startUpdatingLocation()
if(application.respondsToSelector("registerUserNotificationSettings:")) {
application.registerUserNotificationSettings(
UIUserNotificationSettings(
forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Sound,
categories: nil
)
)
}
return true
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
extension AppDelegate: CLLocationManagerDelegate {
func sendLocalNotificationWithMessage(message: String!) {
let notification:UILocalNotification = UILocalNotification()
notification.alertBody = message
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
func locationManager(manager: CLLocationManager!,
didRangeBeacons beacons: [AnyObject]!,
inRegion region: CLBeaconRegion!) {
NSLog("didRangeBeacons");
var message:String = ""
if(beacons.count > 0) {
let nearestBeacon:CLBeacon = beacons[0] as CLBeacon
if(nearestBeacon.proximity == lastProximity ||
nearestBeacon.proximity == CLProximity.Unknown) {
return;
}
lastProximity = nearestBeacon.proximity;
lastMajorValue = nearestBeacon.major;
lastMinorValue = nearestBeacon.minor;
lastUUID = nearestBeacon.proximityUUID;
switch nearestBeacon.proximity {
case CLProximity.Far:
message = "You are far away from the beacon";
println(lastMajorValue)
println(lastMinorValue)
println(lastUUID)
case CLProximity.Near:
message = "You are near the beacon";
println(lastMajorValue)
println(lastMinorValue)
println(lastUUID)
case CLProximity.Immediate:
message = "You are in the immediate proximity of the beacon";
println(lastMajorValue)
println(lastMinorValue)
println(lastUUID)
case CLProximity.Unknown:
return
}
} else {
message = "No beacons are nearby"
}
NSLog("%@", message)
sendLocalNotificationWithMessage(message)
}
func locationManager(manager: CLLocationManager!,
didEnterRegion region: CLRegion!) {
manager.startRangingBeaconsInRegion(region as CLBeaconRegion)
manager.startUpdatingLocation()
NSLog("You entered the region")
sendLocalNotificationWithMessage("You entered the region")
}
func locationManager(manager: CLLocationManager!,
didExitRegion region: CLRegion!) {
manager.stopRangingBeaconsInRegion(region as CLBeaconRegion)
manager.stopUpdatingLocation()
NSLog("You exited the region")
sendLocalNotificationWithMessage("You exited the region")
}
}
視圖控制器文件如下:
import Foundation
import UIKit
import CoreLocation
class ViewController: UIViewController{
@IBOutlet weak var advertismentImageArea: UIImageView!
@IBAction func closeAdvertisementButton(sender: UIButton) {
advertismentImageArea.hidden = true
}
var beaconInformation: AppDelegate!
override func viewDidLoad() {
super.viewDidLoad()
var closestBeacon = beaconInformation
var majorNumber = closestBeacon.lastMajorValue
if majorNumber == 6303 {
advertismentImageArea.image = UIImage(named: "AdOne")
} else if majorNumber == 21456 {
advertismentImageArea.image = UIImage(named: "AdTwo")
} else {
return advertismentImageArea.hidden = true;
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我真的很感激任何幫助,您都可以提供。