2016-11-07 143 views
0

我有一個應用程序,當我通過itunes/Diawi adhoc分發安裝應用程序崩潰,但不會崩潰時,我通過XCODE安裝應用程序。應用程序崩潰時adhoc分發,但不會崩潰時使用XCode

MY CRASH REPORT ..

Thread 0 Crashed: 
0 Vabo       0x00000001000bb07c specialized AppDelegate.registerDeviceForPushNotification(UIApplication) ->() (AppDelegate.swift:214) 
1 Vabo       0x00000001000ab260 ViewController.(connectToWebWith(String, password : String) ->()).(closure #2).(closure #3) (ViewController.swift:265) 

的碰撞數1夫特方法:崩潰數2的

func registerDeviceForPushNotification(application:UIApplication) -> Void { 

     let settings: UIUserNotificationSettings = UIUserNotificationSettings.init(forTypes: [.Alert,.Badge,.Sound], categories: nil) 
     self.pushNotificationToken = FIRInstanceID.instanceID().token()! 
     let userID = self.userData["id"] as! NSNumber 

     print("InstanceID token: \(self.pushNotificationToken)") 
     self.registerDeviceOnServerWith(self.pushNotificationToken, userID: userID) 
     application.registerUserNotificationSettings(settings) 
     application.registerForRemoteNotifications() 
    } 


func registerDeviceOnServerWith(token:String, userID:NSNumber) -> Void { 
     let params = ["api_token":token, "user_id":userID , "type":"iOS"] 

     //  params.setValue(username, forKey: "email") 
     //  params.setValue(password, forKey: "password") 
     let urlString = Constants.kMainURL + Constants.kRegisterDeviceToken; 
     let request = NSMutableURLRequest(URL: NSURL(string: urlString)!) 
     let session = NSURLSession.sharedSession() 
     request.HTTPMethod = "POST" 

     do { 
      request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(params, options: .PrettyPrinted) 
     } catch { 


      //handle error. Probably return or mark function as throws 
      print(error) 
      return 
     } 
     request.addValue(self.tokenID as String, forHTTPHeaderField: "token") 
     request.addValue("application/json", forHTTPHeaderField: "Content-Type") 
     request.addValue("application/json", forHTTPHeaderField: "Accept") 

     let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in 
      // handle error 
      guard error == nil else { 

       return 
      } 

      print("Response: \(response)") 
      let strData = NSString(data: data!, encoding: NSUTF8StringEncoding) 
      print("Body: \(strData)") 

      let json: NSDictionary? 
      do { 
       json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves) as? NSDictionary 
      } catch let dataError { 
       // Did the JSONObjectWithData constructor return an error? If so, log the error to the console 
       print(dataError) 
       let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding) 
       print("Error could not parse JSON: '\(jsonStr)'") 
       // return or throw? 
       return 
      } 

      // The JSONObjectWithData constructor didn't return an error. But, we should still 
      // check and make sure that json has a value using optional binding. 
      if let parseJSON = json { 
       // Okay, the parsedJSON is here, let's get the value for 'success' out of it 
       let success:NSString = (parseJSON["status"] as? NSString)! 
       if success.isEqualToString("Success"){ 
        print("APNS is Registeration is : \(success)") 


       }else{ 

        self.registerDeviceOnServerWith(token, userID: userID) 
        // Status Failed 

       } 

      } 
      else { 
       // Woa, okay the json object was nil, something went worng. Maybe the server isn't running? 

      } 

     }) 

     task.resume() 
    } 

方法:

func connectToWebWith(username:String, password:String) -> Void { 
     self.startLoadingAnimator() 
     let params = ["email":username, "password":password] 

//  params.setValue(username, forKey: "email") 
//  params.setValue(password, forKey: "password") 
     let urlString = Constants.kMainURL + Constants.kSignInURL; 
     let request = NSMutableURLRequest(URL: NSURL(string: urlString)!) 
     let session = NSURLSession.sharedSession() 
     request.HTTPMethod = "POST" 

     do { 
      request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(params, options: .PrettyPrinted) 
     } catch { 


      dispatch_async(dispatch_get_main_queue(), { 
       self.stopLoadingAnimator() 
       let alertView = UIAlertView.init(title: "Error", message: "Failed to authenticate", delegate: nil, cancelButtonTitle: "OK") 
       alertView.show() 


      }) 
      //handle error. Probably return or mark function as throws 
      print(error) 
      return 
     } 
     request.addValue("application/json", forHTTPHeaderField: "Content-Type") 
     request.addValue("application/json", forHTTPHeaderField: "Accept") 

     let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in 
      // handle error 
      guard error == nil else { 

       dispatch_async(dispatch_get_main_queue(), { 

        self.stopLoadingAnimator() 
        let alertView = UIAlertView.init(title: "Error", message: "Couldn't establish connection", delegate: nil, cancelButtonTitle: "OK") 
        alertView.show() 


       }) 
       return 
      } 

      print("Response: \(response)") 

      let json: NSDictionary? 
      do { 
       json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves) as? NSDictionary 
      } catch let dataError { 
       // Did the JSONObjectWithData constructor return an error? If so, log the error to the console 
       print(dataError) 
       dispatch_async(dispatch_get_main_queue(), { 

        self.stopLoadingAnimator() 
        let alertView = UIAlertView.init(title: "Error", message: "Failed to authenticate", delegate: nil, cancelButtonTitle: "OK") 
        alertView.show() 


       }) 
       let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding) 
       print("Error could not parse JSON: '\(jsonStr)'") 
       // return or throw? 
       return 
      } 

      self.stopLoadingAnimator() 
      // The JSONObjectWithData constructor didn't return an error. But, we should still 
      // check and make sure that json has a value using optional binding. 
      if let parseJSON = json { 
       print("JSON = \(parseJSON)") 
       // Okay, the parsedJSON is here, let's get the value for 'success' out of it 
       let success:NSString = (parseJSON["status"] as? NSString)! 
       if success.isEqualToString("Success"){ 
        print("Succes: \(success)") 
        dispatch_async(dispatch_get_main_queue(), { 

          let userDefault = NSUserDefaults.standardUserDefaults() 
          userDefault.setValue(username, forKey: Constants.kVaboEmail) 
          userDefault.synchronize() 
          userDefault.setValue(password, forKey: Constants.kVaboPassword) 
          userDefault.synchronize() 
          userDefault.setBool(true, forKey: Constants.kIsLoggedIn) 
          userDefault.synchronize() 
          let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 
          appDelegate.tokenID = (parseJSON["token"] as? NSString)! 
          let array = parseJSON["userData"] as! NSArray 
          appDelegate.userData = array.objectAtIndex(0) as! NSDictionary 
          appDelegate.userDidLoggedIn() 


        }) 
       }else{ 
        let errorString = parseJSON["messageData"] as! String 
        dispatch_async(dispatch_get_main_queue(), { 
         let alertView = UIAlertView.init(title: "Vabo", message: errorString, delegate: nil, cancelButtonTitle: "Dismiss") 
         alertView.show() 

        }) 


       } 

      } 
      else { 
       // Woa, okay the json object was nil, something went worng. Maybe the server isn't running? 
       let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding) 
       print("Error could not parse JSON: \(jsonStr)") 
       dispatch_async(dispatch_get_main_queue(), { 

        self.stopLoadingAnimator() 
        let alertView = UIAlertView.init(title: "Error", message: "Server Response Failed", delegate: nil, cancelButtonTitle: "OK") 
        alertView.show() 


       }) 
      } 

     }) 

     task.resume() 
    } 

P.S 在iPhone上的應用程序給崩潰了iOS的10.1, 雖然它完美地運行在iOS 9.3.5

回答

0

從你提供的,目前還不清楚哪一行是第214日誌。 (AppDelegate.swift:214),並且日誌中也缺少崩潰的原因。

但我看到你正在使用強制轉換幾個地方,當你試圖訪問它們時,我會確保這些值實際存在。我建議使用保護語句,而不是力鑄造:

func registerDeviceForPushNotification(application:UIApplication) -> Void { 

    let settings: UIUserNotificationSettings = UIUserNotificationSettings.init(forTypes: [.Alert,.Badge,.Sound], categories: nil) 

    guard let token = FIRInstanceID.instanceID().token(), let userID = self.userData["id"] as? NSNumber { 
    // You might want to log something here 
    return 
    } 

    print("InstanceID token: \(self.pushNotificationToken)") 
    self.registerDeviceOnServerWith(self.pushNotificationToken, userID: userID) 
    application.registerUserNotificationSettings(settings) 
    application.registerForRemoteNotifications() 
} 

更新

如果你把它在iOS的10被棄用,你可以看到UIUserNotificationSettings的文件一看你應該使用UNUserNotificationCenter在iOS上10:

let center = UNUserNotificationCenter.currentNotificationCenter() 
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in 
     // Enable or disable features based on authorization. 
    } 
application.registerForRemoteNotifications() 

你可以找到更多信息herehere

+0

對不起... 這裏是第214行App Delegate 讓設置:UIUserNotificationSettings = UIUserNotificationSettings.init(forTypes:[.Alert,.Badge,.Sound],類別:零) –

+0

這個應用程序崩潰,由於生成從Xcode 7.3構建,然後嘗試在iOS 10上運行它? 由於這個程序在iOS 9.3.5 –

+0

完美運行而不anycrash而線數265是dispatch_async的關閉括號(dispatch_get_main_queue(),{ //這是行號264在ViewController.swift - >}) –