logo
down
shadow

How to call didUpdateLocation in another viewcontroller class from custom location manager?


How to call didUpdateLocation in another viewcontroller class from custom location manager?

Content Index :

How to call didUpdateLocation in another viewcontroller class from custom location manager?
Tag : swift , By : MikeW
Date : November 29 2020, 04:01 AM

I wish this helpful for you I have the following custom location manager class, I have another view controller that has a map view, how do I use this location manager to update the map view's coordinate when the user moves? For some reason I cannot even call the didUpdateLocations function in my other view controller. , There are more than one way to achieve it.
Delegation:
protocol CustomLocationManagerDelegate: class {
    func customLocationManager(didUpdate locations: [CLLocation])
}

class CustomLocationManager:NSObject, CLLocationManagerDelegate {
    static let shared = CustomLocationManager()

    // tip: it is better to declare `locationManager` as private, so you can only access it
    // from the manager...
    private var locationManager = CLLocationManager()

    // here is the delegate:
    weak var delegate: CustomLocationManagerDelegate?

    private override init()
    {
        super.init()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest}
    func startTracking()
    {
        locationManager.startUpdatingLocation()
        locationManager.startUpdatingHeading()
    }

    func stopTracking()
    {
        locationManager.stopUpdatingHeading()
        locationManager.stopUpdatingLocation()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        // calling the delegate method
        delegate?.customLocationManager(didUpdate: locations)
    }
}
class MyViewController: UIViewController {
    // ...
    override func viewDidLoad() {
        super.viewDidLoad()

        CustomLocationManager.shared.startTracking()
        CustomLocationManager.shared.delegate = self
    }
    // ...
}

extension MyViewController: CustomLocationManagerDelegate {
    func customLocationManager(didUpdate locations: [CLLocation]) {
        // here we go:
        for currentLocation in locations {
            print("\(index):\(currentLocation)")
        }
    }
}
class CustomLocationManager:NSObject, CLLocationManagerDelegate {
    static let shared = CustomLocationManager()

    // here is the closure
    var updatedLocations: (([CLLocation]) -> Void)?

    // tip: it is better to declare `locationManager` as private, so you can only access it
    // from the manager...
    private var locationManager = CLLocationManager()

    // here is the delegate:
    weak var delegate: CustomLocationManagerDelegate?

    private override init()
    {
        super.init()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest}
    func startTracking()
    {
        locationManager.startUpdatingLocation()
        locationManager.startUpdatingHeading()
    }

    func stopTracking()
    {
        locationManager.stopUpdatingHeading()
        locationManager.stopUpdatingLocation()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        // here we call it:
        updatedLocations?(locations)
    }
}
class MyViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        CustomLocationManager.shared.startTracking()

        CustomLocationManager.shared.updatedLocations = { [weak self] locations in
            guard let unwarappedSelf = self else { return }
            // unwarappedSelf.blablabla

            for currentLocation in locations {
                print(currentLocation)
            }
        }
    }
}

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Get user location in a simple ViewController in iOS 7 (didUpdateLocation)


Tag : ios , By : Richard Laksana
Date : March 29 2020, 07:55 AM
help you fix your problem Firstly check that you actually have a valid WiFi and 3G connection
then

Call custom Class' obj of type NStimer into viewController


Tag : ios , By : Brian
Date : March 29 2020, 07:55 AM
it should still fix some issue There are a couple of issues here. Firstly make Timer an instance variable of the view controller as when you do this:
-(void)setupGame{

    // blah
    Timer *newTimer = [[Timer alloc]init];
    // blah
}

Storyboard viewcontroller's custom class set as a viewcontroller without xib shows black


Tag : ios , By : unadopted
Date : March 29 2020, 07:55 AM
should help you out You can't create a ViewController in Storyboard which is a subclass of a ViewController which is loaded with a nib. That would be 2 interface files (nib + storyboard) for one ViewController.

Location Manager Not Updating Outside ViewController


Tag : ios , By : Stephen Judge
Date : March 29 2020, 07:55 AM
it fixes the issue First of all, I am not sure this solution will fix your issue. But just try to delete your Location Manager initialization in your FooPath and ViewController class and create the Location Manager constant to your AppDelegate and get this locationManager from your class.
You can create the locationManager in your AppDelegate like this;
let locationManager = CLLocationManager()
private let locationManager = AppDelegate().locationManager

custom Location Manager class using CLLocationManager


Tag : iphone , By : KT.
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • How to add storyboard canvas orientation in git ignore in Xcode
  • How to execute different implementation of a method of a generic struct based on the generic type?
  • How to handle Errors on never ending chain with materialize?
  • RxSwift convert Observable<String> into String
  • Alamofire sending a request to an API 'NSInvalidArgumentException'
  • SwiftUI Conditional List - Unable to infer complex closure return type
  • Result values in '? :' expression have mismatching types 'String.SubSequence' (aka 'Substring') and 'String'
  • SwiftUI - Align contents of Button to the left
  • Swift handle did select on multiple annotations on same location
  • How to use NSPredicate for whether a List of Int contains a Int number?
  • SetNeedsDisplay having no effect
  • Can't initialize class with "if"
  • SKPaymentQueue.default().add(payment) ignored
  • How to share UIImage to Camera Roll using UIActivityViewController
  • Play Sound From String Arrays Swift
  • Runtime error using Combine: dyld: Symbol not found: _$s7Combine9PublishedV9PublisherCyx_GAadAMc
  • addPeriodicTimeObserver generate a escaping closure error
  • Since iOS 13 Beta 3, my app only displays a black window
  • showing uiview controller as a pop up
  • SwiftUI Beta 3 Black Screen
  • What is Content in SwiftUI?
  • How do I access the underlying key of a SymmetricKey in CryptoKit?
  • How to get dynamic Text height for a ScrollView with SwiftUI
  • SwiftUI: NavigationButton initializer gives error
  • How to limit character for username in Swift
  • How do I use UserDefaults with SwiftUI?
  • I'm an absolute beginner in swift. I don't what's wrong with the output of my code, This is based on mutating methods
  • How to reload data in UITableView when popping view controller
  • Cannot hook into another RxSwift stream based on a check
  • SwiftUI Change View with Button
  • SwiftUI: How to draw filled and stroked shape?
  • How to access inner object's properties from superclass without boilerplate
  • SwiftUI Table Custom Swipe?
  • Unwrapping optional inside map closure in Swift
  • What is the best way to wrap `purchaserInfo` for easy comparisons throughout your code using RevenueCat
  • How to shuffle multidimensional array?
  • Can you use a Publisher directly as an @ObjectBinding property in SwiftUI?
  • Changing certain words' colour in SCNText
  • Limit frequency with which firestore retrieves data
  • SwiftUI - Half modal?
  • Call a function multiple times and get a list of results in swift?
  • Open multiple preview same time in SwiftUI
  • What is simdTransform in SceneKit and ARKit?
  • How can I fill only part of the custom UIView rectangle?
  • Type of expression is ambiguous without more context in Xcode 11
  • How to setup Horizontal UICollectionView with paging, but items per page arrange from left to right & top to bottom
  • Why does this method with an optional parameter not override the base class method?
  • Implement a collection view SwiftUI
  • RealityKit – Set text programmatically of an Entity of Reality Composer
  • How to open file dialog with SwiftUI on platform "UIKit for Mac"?
  • Sliding one SwiftUI view out from underneath another
  • how to set a IBAction action on a button
  • Is 'window' not an identifier in Swift iOS Development?
  • How to Sort a string date array
  • Can anyone explain Swift Combine's Subject.eraseToAnySubject() method and where it should be used?
  • Is there a method to blur a background in SwiftUI?
  • Getting RSA private key's n and d using Swift
  • Code=50 “No such payment_intent” when confirm payment intent for stripe
  • xcodebuild -create-framework error: unable to read the file
  • Conditional rendering with optionals in SwiftUI
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com