2016-08-15 20 views
1

狀況:如何在用戶進入CLLocation時通知用戶?

我也跟着下面的教程:

https://www.raywenderlich.com/95014/geofencing-ios-swift


問題:

下面的功能不被觸發:

APPD elegate.swift

func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) { 
    if region is CLCircularRegion { 
     handleRegionEvent(region) 
    } 
} 

func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) { 
    if region is CLCircularRegion { 
     handleRegionEvent(region) 
    } 
} 

func handleRegionEvent(region: CLRegion!) { 

    print("Geofence triggered!") 

    // Show an alert if application is active 
    if UIApplication.sharedApplication().applicationState == .Active { 
     if let message = notefromRegionIdentifier(region.identifier) { 
      if let viewController = window?.rootViewController { 
       showSimpleAlertWithTitle("Congratulations", message: "You just found: " + message , viewController: viewController) 
      } 
     } 
    } else { 
     // Otherwise present a local notification 
     let notification = UILocalNotification() 
     notification.alertBody = "You just found: " + notefromRegionIdentifier(region.identifier)! 

     notification.soundName = "Default"; 
     UIApplication.sharedApplication().presentLocalNotificationNow(notification) 
    } 
} 

問題:

本教程是爲iOS 8書面我目前在iOS 9.3。你認爲是什麼引起了這個問題,我該如何解決?

+0

編輯:代碼工作正常,在XCode模擬器(我可以通過gpx文件模擬XCode中的點A到點B的移動),但不是在我的iPhone上...爲什麼?確實存在解決方案。 – Coder1000

回答

1

做的兩件事情肯定: - :斯威夫特

locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.distanceFilter = kCLDistanceFilterNone 
    locationManager.requestWhenInUseAuthorization() 
    locationManager.startMonitoringSignificantLocationChanges() 
    locationManager.startUpdatingLocation() 

另一種替代requestWhenInUseAuthorization()startUpdatingLocation()初始化特定於雨燕2.2,因爲 -

1)您已經添加了這些你viewDidLoad() 2.2不推薦使用選擇器的字符串文字,而是需要使用這個新的運算符#選擇器。 : -

你也可以使用 - :

NSLocationAlwaysUsageDescription : String :-> I need location.

NSLocationWhenInUseUsageDescription: String :-> I need location.

privacy - location usage description: String :-> I need location.

-

locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.distanceFilter = kCLDistanceFilterNone 

    locationManager.startMonitoringSignificantLocationChanges() 
    if locationManager.respondsToSelector(#selector(locationManager.requestWhenInUseAuthorization)) { 
     locationManager.requestWhenInUseAuthorization() 
    } 
    else { 
     locationManager.startUpdatingLocation() 
    } 
     //Prefer the FIRST ONE. 

2)你有更新了的info.plist

編輯I need location根據應用的需要

PS: - 如果仍然不能調用你的locationManager功能

模擬器: - 尋找你的模擬器設置你的應用程序的location設置。

設備: - 進去settings > Privacy > Location services > Your app > Always.

你也可能會發現這樣的解釋非常有用: - https://stackoverflow.com/a/26090094/6297658

+0

嗨,它適用於我的模擬器,但不是在我的手機上。看到我對我的問題的評論。 – Coder1000

+0

它解決了你的問題嗎? – Dravidian

+0

看起來像! Thx:D – Coder1000

2

您沒有顯示用於設置CL的代碼 - 這可能是您的問題所在。

您是否編輯過info.plist?

您是否需要權限?

您是否在CL管理器上調用過其中一個啓動函數?

+0

讓我編輯我的問題。編輯:完成。是的,我根據需要編輯了info.plist。 – Coder1000

0

初始化位置管理器在應用程序委託上沒有完成啓動

+0

這就是我已經做的:/ – Coder1000