所以,我想使用带位置的API检查一下并发送UILocalNotification
。
这就是我从另一个类获取AppDelegate中的位置的方法:
locationManager.stopUpdatingLocation()
if gotResponse {
return
}
locationA = locations
gotResponse = !gotResponse
let geocoder = CLGeocoder()
let locationArray = locationA as NSArray
let locationObj = locationArray.lastObject as! CLLocation
let coord = locationObj.coordinate
let latitude = coord.latitude
let longitude = coord.longitude
lat = latitude
long = longitude
AppDelegate.lat = latitude
AppDelegate.long = longitude
在AppDelegate中我有这个:
@UIApplicationMain
class AppDelegate:UIResponder,UIApplicationDelegate,CLLocationManagerDelegate {
static var lat = CLLocationDegrees()
static var long = CLLocationDegrees()
....
func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
let currentURL = "\(AppDelegate.lat),\(AppDelegate.long)?"
print("currentURLInBackround: \(currentURL) \(application.scheduledLocalNotifications!.count)")
Answers.logCustomEventWithName("Sent RainAlerts", customAttributes: nil)
Alamofire.request(.GET, currentURL)
.responseJSON { response in
let jsonData: AnyObject?
do {
jsonData = try NSJSONSerialization.JSONObjectWithData(response.data!, options: [])
guard let jsonDict = jsonData as? NSDictionary else {return}
guard let dailyDict = jsonDict["daily"] as? NSDictionary else {return}
guard let dataArr = dailyDict["data"] as? NSArray else {return}
guard let tomorrowTemp = dataArr[1] as? NSDictionary else {return}
guard let precipChance = tomorrowTemp["precipProbability"] as? Double else {return}
print("There is a chance of rain of: \(Int(precipChance * 100) )")
// MARK: set push notification
if self.defaults.boolForKey("rainAlertSwitch") == true && Int(precipChance * 100) > 40 && application.scheduledLocalNotifications?.count == 0{
let notifTime: NSDate = NSDate().dateByAddingTimeInterval(1.0)
let notification: UILocalNotification = UILocalNotification()
// Configure notification
application.scheduledLocalNotifications?.append(notification)
Answers.logCustomEventWithName("Sent RainAlerts", customAttributes: nil)
}
}catch{
}
}
completionHandler (.NewData)
}
我的问题是我的分析一直在说应用程序在performFetchWithCompletionHandler
崩溃。我在实际设备上进行Debug-> Stimulate Background Fetch并且工作得很好。我不知道为什么分析会说应用程序崩溃了。
以下是崩溃的分析链接:http://crashes.to/s/86193953c05
答案 0 :(得分:0)
response.data
为零。
但是,您不会拨打completionHandler