簡體   English   中英

當用戶進入提供ios的特定區域時,如何獲得本地通知

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

我正在開發這樣一個應用程序執行以下操作的項目:

1.獲取用戶當前位置。 2.當用戶進入或提供我提供的特定位置時,獲取本地通知。

我所做的是:

我已經下載了區域示例代碼(提供的蘋果),以便使用IOS搭配框架找出我當前的位置。它工作正常。下面的代碼如下:

//根據地圖視圖的中心創建一個新區域。

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]];

現在,我的問題是如何添加具有緯度和經度的特定區域以獲得通知?

非常感謝幫助。任何人都知道任何示例或教程。

SetSDK應該有助於使這個超級簡單, https ://cocoapods.org/pods/SetSDK。 它允許您設置用戶到達和離開位置的通知。 它目前正在動態學習這些位置,但有一個即將發布的版本包括任意位置訂閱。 您的應用程序將收到通知,您可以從那里執行任何您想要的處理程序。 看起來像這樣,

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 */
    }
}

探索iOS核心位置框架提供的基於位置的服務。

這里有一些很好的教程。 它可能會幫助你

1.獲取用戶當前位置。

我有很長的帖子和示例代碼,我在我的博客和Github上發布了如何獲取iOS 7的位置。

2.當用戶進入或提供我提供的特定位置時,獲取本地通知。

您必須使用startMonitoringForRegion來監視您創建的區域。

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

從locationManager委托,您將在進入該區域時收到通知。

-(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];
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM