A simple code to detect any beacon in swift
SWIFT 3:
First of all you should add the CoreLocation.Framework
In the .Plist file add the key/string
NSLocationAlwaysUsageDescription
with appropriate stringIn your Object add the
CLLocationManagerDelegate
Add the
CLLocationManagerDelegate
delegate methods in this example i will just add the didRangeBeacons method
func locationManager(_ manager: CLLocationManager, didRangeBeacons
beacons: [CLBeacon], in region: CLBeaconRegion) {
print(beacons) }
Create and initialise the locationManager
var locationManager : CLLocationManager = CLLocationManager()
Create the
CLBeaconRegion
let beaconRegion : CLBeaconRegion = CLBeaconRegion(
proximityUUID: NSUUID.init(uuidString:"****-****-****-****-******") as! UUID,
identifier: "my beacon")Add the delegate to your Object
locationManager.delegate = self
- Request the Location authorization from the user with
locationManager.requestAlwaysAuthorization()
Now let's start the Range
locationManager.startRangingBeacons(in: beaconRegion)
This will automatically call the delegate method
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
print(beacons)
}
Note : if you want to monitor beacon's entry/ exit state you need to add
locationManager.startMonitoring(for: beaconRegion)
Finally : Make sure that your beacon is turned ON and you are testing the iBeacon Frame Enjoy :D
Simple demo proximity events iOS app not working (Estimote beacons)
I don't see a call to startObservingZones
(:
In your case, you could add this line at the bottom of viewDidLoad
:
self.proximityObserver.startObserving([blueberryZone])
Detect iBeacon with different major and minor values
When you initialize your region, use
region = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:identifierString];
instead of something like
region = [[CLBeaconRegion alloc] initWithProximityUUID:uuid major:[major integerValue] minor:[minor integerValue] identifier:identifierString];
By not specifying the major and minor values, all beacons with the matching UUID should be detected. It works for me.
Beacon monitoring in background iOS
It is common to use a software filter to ignore spurious region exit events if an entry event happens soon afterward.
To make this independent of any one ViewController, it is important to have the logic triggered by the AppDelegate. Two choices here:
Put region monitoring callbacks and filter logic directly in the AppDelegate. This is appropriate for small and simple apps.
Put callbacks and filter logic in a custom class, and initialize it from the AppDelegate's didFinishLaunching method. This is the best approach for larger and more complex apps to keep the AppDelegate simple and clean.
Either way, it is critical to trigger starting of monitoring from the didFinishLaunching method. This ensures proper background CoreLocation setup should your app be auto launched by region transitions.
Swift - Get iBeacon - external class
It looks like you are assigning your advertise service to a local variable at the end of your viewDidLoad. This instance will be destroyed at the end of this method (one line later ;))
Please make sure to assign your advertiser to an instance variable or property and assign self (your viewcontroller) as a delegate to your advertiser.
This should do the trick.
Related Topics
Showing Hidden View Really Slow
Saving Array Using Nsuserdefaults Crashes App
Indexing into Array of Functions: Expression Resolves to an Unused L-Value
In Swift, for Anyobject, How to Setvalue() Then Call Valueforkey()
Deinit Method Is Not Called in Xcode 10 Beta 6 Playground
Why Do I Get an Mkerrordomain Error When Doing a Local Search
Please Add the Host Targets for the Embedded Targets to the Podfile
Swift Unable to Use API Data in Collection View
How to Insert a Override Function into a If Else Statement
How to Download and View Images from the New Firebase Storage
Uicollectionviewcontroller Error in Swift 3.0: Must Be Initialized with a Non-Nil Layout Parameter
Allow Users to Send Messages to Multiple Users Simultaneously in a Messaging App
Uitapgesturerecognizer Called Immediately
How to Change Uinavigationbar Per View Controller
App Installation Failed: Unknown Error Xcode 7
Uitableviewcell Shows Incorrect Results from Document Folder