1

所以,我想使用带有位置的 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

类 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

4

1 回答 1

0

response.data是零。

但是,您不调用completionHandler

于 2018-10-22T13:04:27.847 回答