How would I create geofences using an array of information? [closed] - swift

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to follow this tutorial on setting up geofences for locations but I want to create geofences using an array of information I have grabbed from my Firebase Database. Does anyone know how I would do that or have any tutorials they could link for me? I'm struggling to wrap my head around how I would do this as I'm pretty new to Swift. Could someone help explain what I would do or point me to someone/somewhere that could explain this?

Something like this:
func startMonitoring(_ manager:CLLocationManager, region:CLCircularRegion) {
if !CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) {
print("Cannot monitor location")
return
}
if CLLocationManager.authorizationStatus() != .authorizedAlways {
print("Please grant access")
} else {
let locationManager = CLLocationManager()
locationManager.startMonitoring(for: region)
}
}
func getRegionForLocation(_ location:CLLocation) -> CLCircularRegion {
let radiusMeters:Double = 1000
let identifier = "MyGeofence \(location)"
let region = CLCircularRegion(center: location.coordinate, radius: radiusMeters, identifier: identifier)
region.notifyOnEntry = true
region.notifyOnExit = !region.notifyOnEntry
return region
}
func getLocationsFromFireBase() -> [CLLocation] {
var locations:[CLLocation] = []
// .. populate with locations from DB
return locations
}
//where you want to enable
let locationManager = CLLocationManager()
locationManager.requestAlwaysAuthorization()
let locations = getLocationsFromFireBase()
for location in locations {
let region = getRegionForLocation(location)
startMonitoring(locationManager, region: region)
}
I am glossing over how to enable location access (you have to add NSLocationAlwaysUsageDescription in your info.plist for example), but the general principles of adding multiple geofences is shown. You also need to add a delegate to the CLLocationManager so you are notified when the device goes into or out of a geofence.

Related

Is it possible to print the UI Content Presented on Screen | Swift [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Essentially I have UI View that presents on screen and takes up the entire phones screen, I would like to be able to select a button and print what is currently presented on the screen over AirPrint. Is something like this possible? Are there libraries or other ways of achieving this?
I appreciate any help.
Step 1: First for converting your UIView into image , add this extension:
extension UIView {
func toImage() -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale)
drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
Step 2: Add this into your button action for print:
#IBAction func printButton(sender: AnyObject) {
let printInfo = UIPrintInfo(dictionary:nil)
printInfo.outputType = UIPrintInfoOutputType.General
printInfo.jobName = "My UIView"
let printController = UIPrintInteractionController.sharedPrintController()
printController.printInfo = printInfo
printController.printingItem = self.view.toImage()
// If you want to specify a printer
guard let printerURL = URL(string: "Your printer URL here, e.g. ipps://HPDC4A3E0DE24A.local.:443/ipp/print") else { return }
guard let currentPrinter = UIPrinter(url: printerURL) else { return }
printController.print(to: currentPrinter, completionHandler: nil)
printController.presentFromRect(self.view.frame, inView: self.view, animated: true, completionHandler: nil)
}

How do I view a PDF file saved in my document directory using Swift? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I've built an ordering app and have encoded the order sheet as a PDF that saves to the document directory.
I want to have a method to preview the PDF prior to sending it as an email and was hoping someone can give me some advice on how to load/preview the PDF and how to send it using UIViewController.
You can use PDFKit.amazing tool for PDF viewing https://developer.apple.com/documentation/pdfkit
import PDFKit
class TextPreviewController: BaseController {
#IBOutlet weak private var pdfView: PDFView!
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
private func setupView() {
let fm = FileManager.default
let docsurl = try! fm.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let myurl = docsurl.appendingPathComponent("Owl.jpg")
guard let path = myurl,
let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path))else { return }
pdfView.displayMode = .singlePageContinuous
pdfView.autoScales = true
pdfView.displayDirection = .vertical
pdfView.document = pdfDocument
}
}
you just need to put you link on PDF and it will be open.

How to pass data back with no storyboard using Swift [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have 2 controllers. Let's call them A and B
Controller A is the main one and I have to check in viewWillAppear() if user exists.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard let user = currentUser else {
let setupProfileViewController = SetupProfileViewController()
print("Current user empty")
let navigationController = UINavigationController(rootViewController: setupProfileViewController)
present(navigationController, animated: true, completion: nil)
return
}
}
Here user sees controller B and needs to fill data. On submit I validate all fields and create new User. Then I am dismissing B controller.
self.dismiss(animated: true) {
let matchViewController = MatchViewController()
matchViewController.currentUser = user
matchViewController.kolodaView.reloadData()
}
But what happens it A controller says there is no value in currentUser. I appreciate any advice on how to solve this.
The problem here is that
let matchViewController = MatchViewController()
matchViewController.currentUser = user
is a new instance of the class not the currently presented one , so setting user to it has no value , you have to use delegate or share your user via singleton class

Find out if statUpdatingLocation is active in Swift

I have a location based app running in swift. I am trying to detect if the self.locationManager.startUpdatingLocation() is currently active.
I am struggling to find out how to do this nor can I find much on the internet about it. This I am fairly sure is rather simple to achieve. I don't want to set a BOOL as this needs to be global.
if CLLocationManager.locationServicesEnabled() && /* START UPDATE LOCATION GOES HERE */ {
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
//self.locationManager.startMonitoringSignificantLocationChanges()
sender.setTitle("END DAY", forState: UIControlState.Normal)
} else {
}
I know this is a late answer now. But this is possibly one solution if you want to know if the locationManager is updating.
There should only be one instance of CLLocationManager in your app. So creating a Singleton is ideal. Then, you should override the methods startUpdatingLocation and stopUpdatingLocation.
(Swift 3)
import CoreLocation
class LocationManager: CLLocationManager {
var isUpdatingLocation = false
static let shared = LocationManager()
override func startUpdatingLocation() {
super.startUpdatingLocation()
isUpdatingLocation = true
}
override func stopUpdatingLocation() {
super.stopUpdatingLocation()
isUpdatingLocation = false
}
}
Usage:
if LocationManager.shared.isUpdatingLocation {
print("Is currently updating location.")
} else {
print("Location updates have stopped.")
}
You cannot not know whether startUpdatingLocation() is "active" because you are the one who said it.
If you need to keep track of this from elsewhere, make a Bool property and set it to true when you call startUpdatingLocation() and to false when you call stopUpdatingLocation().

Dragging and dropping sprites without hardcoding names using swift [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm just trying to have the user drag and drop certain sprites around the screen. The answers I saw for this are checking the name of the sprite (something like below). There must be a better way to do this. Thanks for any input.
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
var nodeTouched = SKNode()
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
nodeTouched = self.nodeAtPoint(location)
if(nodeTouched.name? == "character") {
character.position=location
currentNodeTouched = nodeTouched
} else {
currentNodeTouched.name = ""
}
}
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
var nodeTouched = SKNode()
nodeTouched = self.nodeAtPoint(location)
if(currentNodeTouched.name == "character") {
character.position = location
}
}
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
var nodeTouched = SKNode()
nodeTouched = self.nodeAtPoint(location)
if(currentNodeTouched.name == "character") {
character.position = location
}
}
}
Since you want to drag and drop only specific sprites, there has to be a way of identifying them. This can be anything that can be used to distinguish the nodes. The following are a few suggestions on what can be used in this context.
The code in the answer is in Objective-C, hope you can translate it to Swift.
The name property
As you said, this is the most commonly used way of determining whether the sprite can be dragged or not.
The userData property
This is a property associated with SKNode which lets you set any custom data you want. You can read about it here.
[node.userData setValue:#(YES) forKey:#"draggable"]; //Setting the value
if ([[node.userData valueForKey:#"draggable"] boolValue]) //Checking the value
Subclassing
You can also create a custom class which can either handle the deag-drop functionality on it's own or implement a BOOL as a property which can be checked in the touch delegates.
Class name
In some cases, you might want only the nodes of a specific class to be dragged.
if ([node isKindOfClass:[CustomNode class]])
if ([(NSStringFromClass([node class]) isEqualToString:#"CustomNode"])