App crashes when trying to resign keyboard - ios

Since I updated to the latest Xcode 6 beta, I have been having issues with closing the keyboard. My app is crashing each time I try to close the keyboard, even though I have used the same code for ages and it used to work fine.
Here is what I have:
#IBAction func viewTapped(sender : AnyObject) {
//Closes keyboard when user touches screen.
transactionDateInput.resignFirstResponder()
transactionNameInput.resignFirstResponder()
textField.resignFirstResponder()
notesField.resignFirstResponder()
UIView.animateWithDuration(1.5, animations: {
self.valueEnter.alpha = 0
self.dateEnter.alpha = 0
self.notesDone.alpha = 0
})
}
Someone suggested changing it to something like this:
if (transactionDateInput.isFirstResponder() == true){
transactionDateInput.resignFirstResponder()
}
but that makes no difference. Anyone have any suggestions? Here is the error:
AffordIt[4445:1334424] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AffordIt.SecondViewController textFieldShouldReturn:]: unrecognized selector sent to instance 0x7fb169666880'

Check your Text Field outlet delegate (right click to Text Field)
there should be delegate to your UIViewController!
Look at this tutorial

Related

Encountering unrecognized selector sent to instance error" when trying to write a post to Firebase Realtime Database

I am currently working on a uiViewController that allows the user to write a post inside a textView then submit it to Firebase Realtime Database using a button function called "sendPost". I am encountering this error:
"Thread 1: Exception: "-[Healthify.CreatePostViewController sendPost:]: unrecognized selector sent to instance 0x7f965682cee0"
I have gone through the Firebase setup instructions multiple times and I have everything correct. I know my segue between the storyboard button and my view controller is functional.
I am not ultra-familiar with this error so I am wondering if it is an error with my code or if maybe my Realtime Database is not setup correctly.
#IBAction func sendPost(_ sender: Any, textView: UITextView) {
let postRef = Database.database().reference().child("posts").childByAutoId()
let postObject = [
"text": textView.text!,
"timestamp": [".sv":"timestamp"]
] as [String:Any]
postRef.setValue(postObject, withCompletionBlock: {error, ref in
if error == nil{
self.dismiss(animated: true, completion: nil)
}//end of if
else {
//handle error
}//end of else
})
}
I am following this tutorial for reference. I have only got from 0:0-2:50 on it. Does anybody have an idea of a potential solution?
It turns out that my issue was that in my IBAction function for my button I had called in textView: UITextView which, as stated in the comment, cannot be done. I deleted that and now everything works fine.

App crash when MFMessageComposeViewController is initialisation

I want to send a message through my app. I call the following method on click
#IBAction func sendMessage(_ sender: Any) {
if (MFMessageComposeViewController.canSendText()) {
let composeVC = MFMessageComposeViewController()
composeVC.messageComposeDelegate = self
// Configure the fields of the interface.
composeVC.recipients = ["4085551212"]
composeVC.body = "Hello from California!"
// Present the view controller modally.
self.presentViewController(composeVC, animated: true, completion: nil)
}
}
The problem is if I run the app on device, it crashes at MFMessageComposeViewController initialisation i.e.
let composeVC = MFMessageComposeViewController()
If I run the app in simulator, it crashes on presentation i.e.
self.present(composeVC, animated: true, completion: nil)
Error log when I run the app on device
2018-01-29 12:03:57.826816+0530 EWS[2495:806400] *** Assertion failure in -[UICGColor encodeWithCoder:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3698.33.7/UIColor.m:1722
2018-01-29 12:03:57.827522+0530 EWS[2495:806400] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only RGBA or White color spaces are supported in this situation.'
*** First throw call stack:
(0x183702364 0x182948528 0x183702238 0x18409d7f4 0x18d08fffc 0x1840574dc 0x18405e8d8 0x18d4ebf20 0x18d4eb74c 0x18d4e7044 0x1835d03c0 0x18d4e6d44 0x18d7f16fc 0x18d60bd18 0x18d60b94c 0x19aee4534 0x19aecbff4 0x19aecc314 0x1984ce830 0x101159ec0 0x101158888 0x1011584b4 0x1011589c0 0x18cc186b4 0x18cc18634 0x18cc031dc 0x18cc17f28 0x18cc17a48 0x18cc12f60 0x18cbe3f64 0x18d53931c 0x18d53b8a8 0x18d5347c0 0x1836aa97c 0x1836aa8fc 0x1836aa184 0x1836a7d5c 0x1835c7e58 0x185474f84 0x18cc4767c 0x101157170 0x1830e456c)
libc++abi.dylib: terminating with uncaught exception of type NSException
Error log when I run the app on simulator (without checking if(MFMessageComposeViewController.canSendText()))
2018-01-29 12:07:49.395714+0530 EWS[3533:67163] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <EWS.BlockVC: 0x7f899c61db10>.'
*** First throw call stack:
Any help would be appreciated.
Okay,
When you run it in iPhone : The crash log clearly states that "Only RGBA or White color spaces are supported in this situation." So by hint I would say you are assigning color somewhere, (Some Global tint, navigation bar tint) , which doesn't apply to a MFMessageComposeViewController, hence its crashing.
When you run it in the simulator :
if (MFMessageComposeViewController.canSendText()), this line will prevent it from initialising the MFMessageComposeViewController, in the simulator as it cant send text, hence when you try to present it, its not yet initialised and crashing.
On a second thought, as per your code : self.presentViewController(composeVC, animated: true, completion: nil), this line should not even be called while running in the simulator as this line is inside the if statement, which wont get through.
Also change the tint color or any while initialising the controller, and tell me what the results are.

How to debug inconsistent crash to SIGABRT and NSLayoutConstraint constant is not finite! That's illegal. constant:nan

I have looked through all the SO answers and everything seems to point to a divide by zero issue, however, I am not dividing by zero anywhere (at least that I can tell).
Total error I get:
2017-08-17 10:08:49.760586-0500 APP[90643:6605106] * Assertion failure in -[NSLayoutConstraint _setSymbolicConstant:constant:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/Foundation/Foundation-1350/Layout.subproj/NSLayoutConstraint.m:711
2017-08-17 10:08:49.761100-0500 APP[90643:6605106] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSLayoutConstraint constant is not finite! That's illegal. constant:nan firstAnchor: secondAnchor:(null)'
*** First throw call stack:
(0x18ccaafe0 0x18b70c538 0x18ccaaeb4 0x18d743760 0x18d6e4b40 0x18d6e4a04 0x102ac2b7c 0x102ac28c4 0x102abddf4 0x102abe038 0x192df4838 0x192e8f9a4 0x18cba06c0 0x18cb976fc 0x192df4ac0 0x192df45a8 0x19312d080 0x193069594 0x19305b630 0x192dcf28c 0x18cc589a8 0x18cc56630 0x18cc56a7c 0x18cb86da4 0x18e5f1074 0x192e41c9c 0x100038ec8 0x18bb9559c)
libc++abi.dylib: terminating with uncaught exception of type NSException
As it does talk about .width, it seems that a divide by zero might be the issue, though I have triple checked my code and only have one division anywhere in the code:
self.startButton.frame = CGRect(x:self.view.frame.midX - (self.view.frame.midX / 2),y:self.view.frame.midY - 20,width:self.view.frame.midX,height:40)
Which is only there to center the button.
The crash is INCONSISTENT (which is always frustrating!), meaning that I can often repeat the same action and it works fine before a crash (sometimes after 2-3 times, other times, it happens right away...)
Here's the scenario and code around the issue:
I'm in a tabBarController (which has several tabs with all functions working fine) and receive a push notification (various push notifications work fine for setting all actions and pages using this same code).
let vc = currentViewController.storyboard?.instantiateViewController(withIdentifier: "\(rcView)")
if vc != nil {
currentViewController.present(vc!, animated: true, completion: nil)
}
This takes me to a UIViewController that includes a map, directions, various buttons, etc. (again, working great ).
Here is where we see the crash SOMETIMES....
When we click the 'Cancel' button on the controller
let cancelButton = UIButton(type: .system)
cancelButton.addTarget(self, action: #selector(self.cancelIt), for: .touchUpInside)
which goes to the cancelIt function
self.navigationController?.popViewController(animated: true)
self.dismiss(animated: true, completion: nil)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "Home") as! UITabBarController
UIApplication.shared.keyWindow?.rootViewController = viewController
viewController.selectedIndex = 0
After this cancel, I can send another push notification and SOMETIMES it works, while other times, it crashes.
When it crashes, I get the error listed above and in AppDelegate.swift, a red line highlighting
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
with a "Thread 1: signal SIGABRT" (which I have seen before and usually means a duplicate name on the main storyboard)
I'm pretty sure this is all related to the less-than-elegant (IMHO) way of moving from the tabBarController to the UIViewController, though why is it so inconsistent?
Other ways of doing this switchover are welcome - I did a lot of research on how others do it and this seems to be the most clean, but working 'sometimes' isn't going to make the client happy!
self.dismiss(animated: true) { [weak self] in
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "Home") as! UITabBarController
UIApplication.shared.keyWindow?.rootViewController = viewController
viewController.selectedIndex = 0
}
Try calling the dismiss with a completion handler. Oh, and why do you pop first and then dismiss? Is your map controller presented or pushed? Maybe you don't need both.
After a bit more searching (and thanks to a tip I saw in a comment of another SO question - sorry, I lost the link...) on how to better read xcode crashes (https://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1), I found the issue to be in the Mapbox software I was using (https://github.com/mapbox/mapbox-navigation-ios) and not in my code at all.....
The issue of inconsistency boiled down to the location changing from the Simulate Location and the push notification coming in as/at the 'destination' point (when the 'current location' and 'destination location' were the same, MapBox software was coming up with a nan).
I fixed that by doing a check for nan (.isNaN) though got another crash a bit later.....
Now, I've updated the SDK and am fighting other issues (one being that we no longer get the voice - another that the Simulate Location seems to not be working..... AARRGGHH!) but at least the particular issue in the question is answered - it was in the library!

iOS App keeps crashing when using an AdMob banner

I'm making an iOS 8.0+ game using Xcode 7.3.1, SpriteKit, and Swift. My app keeps on crashing whenever I add the AdMob banner to it.
I've pretty much copied the banner code from here: https://firebase.google.com/docs/admob/ios/quick-start
Here's a part of my code:
class GameViewController: UIViewController, GADBannerViewDelegate{
#IBOutlet var bannerView: GADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene(fileNamed:"GameScene") {
let skView = self.view as! SKView
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
skView.presentScene(scene)
let request = GADRequest()
bannerView.delegate = self
bannerView.adUnitID = "(myAppID)"
bannerView.rootViewController = self
bannerView.loadRequest(request)
}
}
}
It runs perfectly fine on any simulator, but it crashes the app on an actual device. I keep getting this error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'*** setObjectForKey: object cannot be nil (key: <f0cdd52e 01000000>)'
*** First throw call stack: (0x1820b659c 0x19280c0e4 0x181fa11f8 0x1000ed1f8 0x100bbce30 0x100bbcdf0 0x100bc7854 0x100bc0120 0x100bc975c 0x100bcaf18 0x19302d2e4 0x19302cfa8)
libc++abi.dylib: terminating with uncaught exception of type NSException(lldb)
If I remove part of the code that deals with the banner ad, my app runs perfectly.
Any suggestions on what I can do to fix it? Also, this is my first time coding an app using Xcode.
Thanks!
It took a while, but I figured it out. I found out that the Google Mobile Ads SDK framework was the problem. Version 7.9.1 doesn't seem to work on iOS 8.0, but works well on iOS 9. I'm guessing that's why it didn't run well on my device, since I was running iOS 8 with SDK 7.9.1. With earlier versions such as 7.8.0, ads can run easily on iOS 8 devices.

Google Autocomplete function Crash after Call

I am using Google PlaceAutoComplete method to get suggestions of the Addess that is entered in textField.
func placeAutocomplete(text:String) {
let placesClient = GMSPlacesClient()
let filter = GMSAutocompleteFilter()
filter.type = .Address
placesClient.autocompleteQuery("New Delhi", bounds: nil, filter: nil) { (results, error) in
guard error == nil else {
print("Autocomplete error \(error)")
return
}
self.addressArray.removeAll()
for result in results! {
self.addressArray.append(result.attributedFullText.string)
print("Result \(result.attributedFullText.string) with placeID \(result.placeID)")
}
}
}
When i call this method. It crashes, say the Following error.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary boolValue]: unrecognized selector sent to instance 0x7fe338f01e40'
I have tried to found using exception breakpoint but doesn't work.
Can any have idea, where i am wrong?
I have resolved the issue by correcting in the plist for "allow arbitrary loads" in App Transport security Settings. I was typed it true but its type was set string instead for Boolean
Somewhere a NSDictionary is being passed to the code where it is expecting a something that can be interpreted as a boolean such as an NSString or NSNumber. I don't see anything like that in the code you provided. If exception breakpoints aren't working I would try adding normal breakpoints somewhere and stepping over code until it crashes. You could also try removing certain sections and code and seeing if the crash is still happening, this will let you narrow down what portion of your code is to blame.

Resources