How to get local notification when user enters into specific region provided ios

不羁的心 提交于 2020-03-03 10:12:47

问题


I am working on such a project where apps do the following things:

1.Getting user current location. 2. Get local notifications when user enters or nearby the particular locations i provided.

What i have done is:

I have downloaded the regions sample code(apple provided) to find out my current location using IOS corelocation framework.It works fine.here's the code below:

// Create a new region based on the center of the map view.

CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(regionsMapView.centerCoordinate.latitude, regionsMapView.centerCoordinate.longitude);
CLRegion *newRegion = [[CLRegion alloc] initCircularRegionWithCenter:coord radius:2.0  identifier:[NSString stringWithFormat:@"%f, %f", regionsMapView.centerCoordinate.latitude, regionsMapView.centerCoordinate.longitude]];

Now, my question is how to add specific regions with latitude& longitude to get notified?

Help is highly appreciated.Anybody knows any example or tutorial.


回答1:


Explore the location-based services provided by the iOS Core Location framework.

here are some good tutorials. It may help you

  • Geofencing with Core Location
  • Geofencing on iOS
  • iOS Geofencing API Tutorial Using Core Location Framework



回答2:


The SetSDK should help make this super easy, https://cocoapods.org/pods/SetSDK. It allows you to setup notifications for user arrival and departure from locations. It currently learns those locations on the fly, but there is a coming release that includes arbitrary location subscription. Your app will receive a notification and you can execute whatever handler you want from there. It would look something like this,

SetSDK.instance.onArrival(to: .any) { newArrival in
    /* Compare the new location with the one of interest (50m) */
    if newArrival.location.distance(from: placeOfInterest) < 50 {
      /* do your things here */
    }
}



回答3:


1.Getting user current location.

I have lengthy post and sample codes that I posted on my blog and Github on how to get location for iOS 7.

  • Background Location Update Programming for iOS 7

  • Github Project: Background Location Update Programming for iOS 7

2. Get local notifications when user enters or nearby the particular locations i provided.

You will have to use startMonitoringForRegion to monitor the region that you created.

CLLocationCoordinate2D regionCentre = CLLocationCoordinate2DMake(latitude, longitude);
CLCircularRegion *region= [[CLCircularRegion alloc] initWithCenter:regionCentre radius:radius identifier:@"Name"];        
[locationManager startMonitoringForRegion:region];

From the locationManager delegate, you will be notified when you enter the region.

-(void)locationManager:(CLLocationManager *)manager 
    didEnterRegion:(CLRegion *)region{
NSString* message = [NSString stringWithFormat:@"Message";        
UIApplicationState state = [[UIApplication sharedApplication] applicationState];

if (state == UIApplicationStateBackground || state == UIApplicationStateInactive)
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate date];
NSTimeZone* timezone = [NSTimeZone defaultTimeZone];
notification.timeZone = timezone;
notification.alertBody = message;
notification.alertAction = @"Show";
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}


来源:https://stackoverflow.com/questions/23535653/how-to-get-local-notification-when-user-enters-into-specific-region-provided-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!