-
Notifications
You must be signed in to change notification settings - Fork 25
/
iOsLocationService.cs
57 lines (49 loc) · 1.58 KB
/
iOsLocationService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Threading;
using System.Threading.Tasks;
using UIKit;
using Xamarin.Forms;
using XamarinForms.LocationService.Messages;
using XamarinForms.LocationService.Services;
namespace XamarinForms.LocationService.iOS.Services
{
public class iOsLocationService
{
nint _taskId;
CancellationTokenSource _cts;
public async Task Start()
{
_cts = new CancellationTokenSource();
_taskId = UIApplication.SharedApplication.BeginBackgroundTask("com.company.product.name", OnExpiration);
try
{
var locShared = new Location();
locShared.setRunningStateLocationService(true);
await locShared.Run(_cts.Token);
}
catch (OperationCanceledException)
{
}
finally
{
if (_cts.IsCancellationRequested)
{
var message = new StopServiceMessage();
Device.BeginInvokeOnMainThread(
() => MessagingCenter.Send(message, "ServiceStopped")
);
}
}
var time = UIApplication.SharedApplication.BackgroundTimeRemaining;
UIApplication.SharedApplication.EndBackgroundTask(_taskId);
}
public void Stop()
{
_cts.Cancel();
}
void OnExpiration()
{
UIApplication.SharedApplication.EndBackgroundTask(_taskId);
}
}
}