iBeacon不工作,我没有收到任何通知,日志中没有打印任何内容

时间:2016-03-18 04:18:05

标签: ios swift ibeacon

如果手机靠近我的笔记本电脑,我正在尝试发送通知,但我没有收到任何通知,也没有在控制台中记录任何内容。我已经完成了info.plist pre reqs NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    let uuidString = "A29ADC96-F6F6-478B-9D4B-0E6420FA1F71" //I used the terminal command "uuidgen"
    let beaconIdentifier = "iBeaconModules.us"
    let beaconUUID = 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()



    UITabBar.appearance().barTintColor = UIColorFromRGB("4B4B4B")
    UITabBar.appearance().tintColor = UIColorFromRGB("00D6B4")

    if(application.respondsToSelector("registerUserNotificationSettings:")) {
        application.registerUserNotificationSettings(
            UIUserNotificationSettings(
                forTypes: [.Alert, .Badge, .Sound],
                categories: nil
            )
        )
    }



    return true
}

extension AppDelegate {
func sendLocalNotificationWithMessage(message: String!, playSound: Bool) {
    let notification:UILocalNotification = UILocalNotification()
    notification.alertBody = message

    if(playSound) {
        // classic star trek communicator beep
        //  http://www.trekcore.com/audio/
        //
        // note: convert mp3 and wav formats into caf using:
        //  "afconvert -f caff -d LEI16@44100 -c 1 in.wav out.caf"
        // http://stackoverflow.com/a/10388263

        notification.soundName = "tos_beep.caf";
    }

    UIApplication.sharedApplication().scheduleLocalNotification(notification)
}

func locationManager(manager: CLLocationManager,
    didRangeBeacons beacons: [CLBeacon],
    inRegion region: CLBeaconRegion) {
       // let viewController:Locations = window!.rootViewController as! Locations
       // viewController.beacons = beacons as [CLBeacon]?
       // viewController.tableView!.reloadData()

        NSLog("didRangeBeacons");
        var message:String = ""

        var playSound = false

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

            if(nearestBeacon.proximity == lastProximity ||
                nearestBeacon.proximity == CLProximity.Unknown) {
                    return;
            }
            lastProximity = nearestBeacon.proximity;

            switch nearestBeacon.proximity {
            case CLProximity.Far:
                message = "You are far away from the beacon"
                playSound = true
            case CLProximity.Near:
                message = "You are near the beacon"
            case CLProximity.Immediate:
                message = "You are in the immediate proximity of the beacon"
            case CLProximity.Unknown:
                return
            }
        } else {

            if(lastProximity == CLProximity.Unknown) {
                return;
            }

            message = "No beacons are nearby"
            playSound = true
            lastProximity = CLProximity.Unknown
        }

        NSLog("%@", message)
        sendLocalNotificationWithMessage(message, playSound: playSound)
}

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

        NSLog("You entered the region")
        sendLocalNotificationWithMessage("You entered the region", playSound: false)
}

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

        NSLog("You exited the region")
        sendLocalNotificationWithMessage("You exited the region", playSound: true)
}
}

0 个答案:

没有答案