On the latest beta5 version, the behavior I see is that even if you give permission for the app to use location services, when you go to the widget gallery to select your widget it pops up another permission box asking if you want to allow/deny location services in the widget.
So looks like giving permission in the app does not automatically give permission to the widget.
However, this might be a bug, if I turn off permission for the widget in the settings -> privacy section, when calling locationManager.authorizationStatus property it will return I have permission but calling the location APIs just returns an error.
Post
Replies
Boosts
Views
Activity
I'm not sure of the best way to use CLLocationManager inside WidgetKit, it would be nice to get a sample from Apple around it showing exactly how we should do it.
To fix the main thread issue, what I did was create a wrapper class that I instantiate in the widget struct that will hold a reference to the CLLocationManager, then on the call to timeline() I create the CLLocationManager instance at that time and store a reference to it in the wrapper, this means it is created on the right thread. If you don't store a reference to this class that lives beyond the call to timeline() the callbacks are never called.
Then, instead of streaming updates from the CLLocationManager instance, I use CLLocationManager requestLocation() to get a one time callback.
Something like this:
LocationWrapper.swift
final class LocationWrapper {
var location: CLLocationManager?
	func fetchLocation(completion: @escaping (CLLocation) -> Void) {
		 // call requestLocation(), hook up to callbacks etc.
		 // call completion once you a
	}
}
widget_main.swift
struct Provider: TimelineProvider {
var locationWrapper = LocationWrapper()
public func timeline(with context: Context, completion: @escaping (Timeline<SimpleEntry>) -> ()) {
if locationManager.location == nil {
locationManager.location = CLLocationManager()
}
		locationManager.fetchLocation { location in
				// call completion, update ui etc.
		}
}
I have CLLocationManager working inside a widget. I had to add these two values to the widgets info.plist file:
<key>NSLocationUsageDescription</key>
<string>1</string>
<key>NSWidgetWantsLocation</key>
<true/>