2016-11-07 2 views
0

J'ai une application et quand jamais j'installe l'application via itunes/distribution Diawi adhoc il se bloque mais ne plante pas lorsque j'installe l'application via XCODE.Application crash quand adhoc distribuer mais ne plante pas lors de l'installation avec XCode

MON RAPPORT DE COLLISION ..

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) 

méthode de Swift nombre crash 1:

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() 
    } 

Méthode de Crash numéro 2:

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 L'iPhone sur l'application iOS donner COLLISION a 10.1, Bien qu'il fonctionne parfaitement sur iOS 9.3.5

Répondre

0

De la condition que vous n'êtes pas connecté, il ne sait pas quelle ligne est le 214e. (AppDelegate.swift: 214) et la raison de l'erreur est également manquante dans le journal.

Mais je vois que vous utilisez la force de lancer quelques endroits, je ferais en sorte que ces valeurs existent réellement lorsque vous essayez d'y accéder. Je suggère d'utiliser des déclarations de garde au lieu de la coulée de force:

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() 
} 

Mise à jour:

Si vous jetez un coup d'oeil sur la documentation des UIUserNotificationSettings vous pouvez le voir a été dépréciés dans iOS 10. Vous devriez utiliser le UNUserNotificationCenter sur iOS 10:

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

Vous pouvez trouver plus d'informations here et here

+0

Désolé ... Voici la 214ème ligne dans App Delegate laissez paramètres: UIUserNotificationSettings = UIUserNotificationSettings.init (forTypes: [.Alert, .Badge, .Sound], catégories: nil) –

+0

Est-ce que cette application se bloque en raison de générer construire à partir de Xcode 7.3, puis en essayant de l'exécuter sur iOS 10 ?? Parce que cette application fonctionne parfaitement sans anycrash sur iOS 9.3.5 –

+0

Alors que le numéro de ligne 265 est les crochets de fermeture de dispatch_async (dispatch_get_main_queue(), { // Ceci est le numéro de ligne 264 dans ViewController.swift ->}) –