قالب وردپرس درنا توس
Home / IOS Development / Ios – Function that burns to the user's location, has been updated

Ios – Function that burns to the user's location, has been updated



Important note: I realize that I could only call the callback function in didUpdateLocations and achieve what I want. Unfortunately, this route cannot be taken because of some existing design decisions made in this project (which I have no control over).

I have to write a function that burns the first time the user's coordinates are updating, and then go to the coordinates of a completion manager. In my case, this skill handler is a function called fetchCountry (from Location: CLLocation) which returns the land corresponding to CLLocation provided.

In other words, I will write a function similar to didUpdateLocations with the ability to call a completion manager after the updates are received:

  func getUserLocation (callback: @escaping (CLLocation?) -> Void ) {

// wait until the user's location is retrieved by the site manager, somehow

// Prepare the callback function output and send it as an argument
la latitude = manager.location! .coordinate.latitude
la longitude = manager.location! .coordinate.longitude
leave place = CLLocation (latitude: latitude, longitude: longitude)

callback (location)
}

In short, getUserLocation is just a wrapper for didUpdateLocations but I'm really not sure how I would go about writing this feature so it achieves what I want.

My major goal here is to show the user a local alert only if they are in a particular country (e.g., USA) at the launch of the app. It is a hard claim that my application should decide to schedule / not schedule this alert within AppDelegate.swift but this decision cannot be made until the user's location is retrieved. I plan to use getUserLocation inside appDelegate like this:

I hope that I have clear that I want to achieve this by using a completion manager . Here's what I want my code to do (ie my state of use), inside AppDelegate.swift :

  // inside didFinishLaunchingWithOptions
UNUserNotificationCenter.current (). RequestAuthorization (options: [.badge, .alert, .sound]) {(given, error) i
if given {

// this is the use of the function I am trying to write
LocationManager.shared.getLocation (completion: {location in

// retrieves the country from a given location using reverse geocoding
LocationManager.shared.fetchCountry (from: location, completion: {country in

whose country == "USA" {
leave notice = LocalNotification ()
notificationManager.schedule (notification: notification)
}
})
})
}
}


Source link