Cannot find 'application' in scope

i get 2 of these errors ""Cannot find 'application' in scope"" what am i doing wrong or why is it not in scope?

Code Block //
// ViewController.swift
// Diabell App
//
// Created by Richard Klug on 23/04/2021.
//
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
 override func viewDidLoad() {
  super.viewDidLoad()
  // Do any additional setup after loading the view.
 }
  
 //Function Email Bottom Left
  
 @IBAction func Mail(_ sender: Any) {
  showMailComposer()
 }
  
 func showMailComposer() {
   guard MFMailComposeViewController.canSendMail() else{
    return
   }
   let composer = MFMailComposeViewController()
   composer.mailComposeDelegate = self
   composer.setToRecipients(["richard.klug@diabell.se"])
   composer.setSubject("Diabell App Help")
   composer.setMessageBody("Fyll i vad du behöver hjälp med", isHTML: false)
   present(composer, animated: true)
 }
   
 //Funktion Call Bottom Right
   
  @IBAction func btnCallClick(_ sender: UIButton) {
    if let phoneURL = URL(string: "tel://+46706106310"){
      if application.canOpenUrl(phoneURL){
        application.open(phoneURL, [:], completionHandler: nil)
      }else{
      }
  }
  
}
}


I cannot find any declaration of application in your code.
You need to explicitly declare it.

With another fixes:
Code Block
@IBAction func btnCallClick(_ sender: UIButton) {
let application = UIApplication.shared //<-
if let phoneURL = URL(string: "tel://+46706106310") {
if application.canOpenURL(phoneURL) { //<- `canOpenURL`, not `canOpenUrl`
application.open(phoneURL, options: [:], completionHandler: nil) //<- `options:` needed
} else {
//...
}
}
}


hmm now the app just crashes when i press the call button i think i still got something in the code?


2021-04-23 14:47:35.995353+0200 Diabell App[79068:6184787] -[Diabell_App.ViewController Call:]: unrecognized selector sent to instance 0x104e095f0
2021-04-23 14:47:35.996463+0200 Diabell App[79068:6184787] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Diabell_App.ViewController Call:]: unrecognized selector sent to instance 0x104e095f0'
* First throw call stack:
(0x1ae4a19d8 0x1c2824b54 0x1ae3b1bbc 0x1b0d40d30 0x1ae4a401c 0x1ae4a5f8c 0x1b0d12f38 0x1b06a8184 0x1b06a84c8 0x1b06a6e00 0x1b0d4dbc0 0x1b0d4f4e8 0x1b0d2ab0c 0x1b0dad078 0x1b0db1818 0x1b0da8afc 0x1ae421bf0 0x1ae421af0 0x1ae420e38 0x1ae41b3e0 0x1ae41aba0 0x1c5180598 0x1b0d0c2f4 0x1b0d11874 0x1c1b98b54 0x104990114 0x10499008c 0x104990158 0x1ae0f9568)
libc++abi.dylib: terminating with uncaught exception of type NSException

  • ** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Diabell_App.ViewController Call:]: unrecognized selector sent to instance 0x104e095f0'

terminating with uncaught exception of type NSException
(lldb) 


Code Block //
// ViewController.swift
// Diabell App
//
// Created by Richard Klug on 23/04/2021.
//
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
 override func viewDidLoad() {
  super.viewDidLoad()
  // Do any additional setup after loading the view.
 }
  
 //Function Email Bottom Left
  
 @IBAction func Mail(_ sender: Any) {
  showMailComposer()
 }
  
 func showMailComposer() {
   guard MFMailComposeViewController.canSendMail() else{
    return
   }
   let composer = MFMailComposeViewController()
   composer.mailComposeDelegate = self
   composer.setToRecipients(["richard.klug@diabell.se"])
   composer.setSubject("Diabell App Help")
   composer.setMessageBody("Fyll i vad du behöver hjälp med", isHTML: false)
   present(composer, animated: true)
 }
   
 //Funktion Call Bottom Right
   
    @IBAction func btnCallClick(_ sender: UIButton) {
      let application = UIApplication.shared //<-
      if let phoneURL = URL(string: "tel://+46706106310") {
        if application.canOpenURL(phoneURL) { //<- `canOpenURL`, not `canOpenUrl`
          application.open(phoneURL, options: [:], completionHandler: nil) //<- `options:` needed
        } else {
          //...
        }
      }
    }
}


hmm now the app just crashes when i press the call button i think i still got something in the code?

I guess it's a problem of your storyboard setting.

From this part of the error message:
Code Block
-[Diabell_App.ViewController Call:]: unrecognized selector sent to instance

iOS is trying to call a method named Call(_:), not btnCallClick(_:).

I guess you first connected the action to method Call(_:) and then renamed it to btnCallClick(_:) only on the swift file.

Please remove the action connection on the storyboard and re-connect it to the right method.
In some cases, you may need to remove the button on the storyboard before re-connecting the action of it.
Looks like you have the same problem in another thread

btnCallClick() func was empty.

And you had callNumber() func not called anywhere

So you have probably messed up things.
To solve:
  • disconnect the button (in IB) : click on the small x in front of the IBAction name

  • reconnect to the IBAction btnCallClick

  • do an option-clean build folder to clean everything

Cannot find 'application' in scope
 
 
Q