Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift iBeacon背景测距_Swift_Xcode_Bluetooth Lowenergy_Core Location_Ibeacon - Fatal编程技术网

Swift iBeacon背景测距

Swift iBeacon背景测距,swift,xcode,bluetooth-lowenergy,core-location,ibeacon,Swift,Xcode,Bluetooth Lowenergy,Core Location,Ibeacon,我正在尝试将我的应用程序设置为一种方式,当用户点击肩部按钮或主页按钮(并非所有时间)时,我不想使用后台模式,在后台开始鸣响并收到通知。所以我用swift编码了它,当我的应用程序在前台和后台工作10秒时,它就工作了,当用户点亮手机屏幕时,测距就不会重新启动。首先,我启动了后台任务,然后在应用程序中,我尝试在当前范围内开始对信标进行测距。有人能帮我吗 #Background Task class BackgroundTask1 : NSObject { privat

我正在尝试将我的应用程序设置为一种方式,当用户点击肩部按钮或主页按钮(并非所有时间)时,我不想使用后台模式,在后台开始鸣响并收到通知。所以我用swift编码了它,当我的应用程序在前台和后台工作10秒时,它就工作了,当用户点亮手机屏幕时,测距就不会重新启动。首先,我启动了后台任务,然后在应用程序中,我尝试在当前范围内开始对信标进行测距。有人能帮我吗

#Background Task   

    class BackgroundTask1 : NSObject {

        private let application: UIApplication!
        private var identifier = UIBackgroundTaskInvalid


        init(application: UIApplication) {
            self.application = application
        }

        class func run(application: UIApplication, handler: (BackgroundTask1) -> ()) {

            let backgroundTask = BackgroundTask1(application: application)
            backgroundTask.begin()
            handler(backgroundTask)
        }

        func begin() {
            print("begin")
            self.identifier = application.beginBackgroundTask {
                self.end()
            }
        }

        func end() {
            if (identifier != UIBackgroundTaskInvalid) {
                application.endBackgroundTask(identifier)
            }
            identifier = UIBackgroundTaskInvalid
        }}

#AppDelegate
    func init_()
        {
            let uuidString = "43F2ACD1-5522-4E0D-9E3F-4A828EA12C24"
            let beaconRegionIdentifier = "Hello"
            let beaconUUID:UUID = UUID(uuidString:uuidString)!
            beaconRegion = CLBeaconRegion(proximityUUID: beaconUUID, identifier: beaconRegionIdentifier)

            beaconRegion.notifyEntryStateOnDisplay = true

            locationManager = CLLocationManager()


            if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.authorizedWhenInUse) {
                locationManager!.requestWhenInUseAuthorization()
            }
            locationManager!.delegate = self
            locationManager!.pausesLocationUpdatesAutomatically=false
        }

        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

            init_()

            locationManager?.startRangingBeacons(in: beaconRegion)

            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.
        }

        var timer = Timer()
        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.
            BackgroundTask1.run(application:
            application) { (BackgroundTask1_) in

                DispatchQueue.global(qos: .default).async
                    {
                        DispatchQueue.main.async {

                            self.init_()
                            self.locationManager?.startRangingBeacons(in: self.beaconRegion)
                        }
                }

            }


        }

你知道你的
didRangeBeacons
方法是否被调用过?如果你不知道,那么你应该添加一个调试行来确定它是否被调用。@DavidYoung谢谢你的帮助,我更改了上面的代码,当用户点亮屏幕时,它仍然无法开始测距,我测试了你提供的这个应用程序“”,当手机退出或重新进入该区域时,它不会重新启动时钟,我使用的是iOS 10.3和iPhone6。