【问题标题】:iOS Background App Fetch not workingiOS后台应用程序获取不起作用
【发布时间】:2016-06-05 12:14:32
【问题描述】:

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

【问题讨论】:

  • 您的网络请求将异步完成,但您在网络请求完成之前在闭包之外调用了completionHandler。我不确定这是否是您崩溃的原因,但您应该将 completionHandler 调用移到您的闭包中。
  • 做到了。仍然崩溃。

标签: ios swift swift2 background-fetch


【解决方案1】:

response.data 为零。

但是,你不打电话给completionHandler

【讨论】:

    猜你喜欢
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 2018-10-26
    相关资源
    最近更新 更多