iOS后台应用程序获取无法正常工作

时间:2016-06-05 12:14:32

标签: ios swift swift2 background-fetch

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

1 个答案:

答案 0 :(得分:0)

response.data为零。

但是,您不会拨打completionHandler