Ios 为什么当这个调用的URL运行时,它会显示nil而不是my UITextField中的数字?

Ios 为什么当这个调用的URL运行时,它会显示nil而不是my UITextField中的数字?,ios,swift,swift3,Ios,Swift,Swift3,目前,我正在开发一个应用程序,当在UITextField中键入电话号码时,会在通过运行URL按通知操作后调用该号码。但问题是,它拨打的电话号码是645,而不是我输入的任何电话号码,我发现它使用的是645,因为它是一个默认为零的电话号码。如何修复此nil值,使其实际上是输入到UITextField中的电话号码?任何答案都会很棒。非常感谢。这是我的密码: AppDelegate.swift: import UIKit import UserNotifications @UIApplicationM

目前,我正在开发一个应用程序,当在UITextField中键入电话号码时,会在通过运行URL按通知操作后调用该号码。但问题是,它拨打的电话号码是645,而不是我输入的任何电话号码,我发现它使用的是645,因为它是一个默认为零的电话号码。如何修复此nil值,使其实际上是输入到UITextField中的电话号码?任何答案都会很棒。非常感谢。这是我的密码:

AppDelegate.swift:

import UIKit
import UserNotifications

@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {


//After notification AlertView (Sending into call)
func showAlertAppDelegate(title : String,message : String,buttonTitle1 : String, buttonTitle2: String, window: UIWindow){

    //AlertView
    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)

        //Action 'Go!'

        let alertActionGo = UIAlertAction(title: buttonTitle1, style: UIAlertActionStyle.default){
            UIAlertAction in

            let actualNumber = phoneNumbered?.text
            print(actualNumber as Any)

            if let url = URL(string: "tel://\(actualNumber)") {
                UIApplication.shared.open(url, options: [:])
            }

        }

        let alertActionCancel = UIAlertAction(title: buttonTitle2, style: UIAlertActionStyle.cancel){
            UIAlertAction in

        }

            alert.addAction(alertActionGo)
            alert.addAction(alertActionCancel)

    window.rootViewController?.present(alert, animated: true, completion: nil)
}




//Main Stuff
var window: UIWindow?






//Setting up notification view

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) {(accepted, error) in
        if !accepted {
            print("Notification access denied.")
        }
    }

    let action = UNNotificationAction(identifier: "call", title: "Enter Call", options: [.foreground])
    let category = UNNotificationCategory(identifier: "myCategory", actions: [action], intentIdentifiers: [], options: [])
    UNUserNotificationCenter.current().setNotificationCategories([category])

    return true
}




//Schedule notification

func scheduleNotification(at date: Date) {
    let calendar = Calendar(identifier: .gregorian)
    let components = calendar.dateComponents(in: .current, from: date)
    let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute)

    let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)

    let content = UNMutableNotificationContent()
    content.title = "Call"
    content.body = "Its time for your call!"
    content.sound = UNNotificationSound.default()
    content.categoryIdentifier = "myCategory"

    if let path = Bundle.main.path(forResource: "logo", ofType: "png") {
        let url = URL(fileURLWithPath: path)

        do {
            let attachment = try UNNotificationAttachment(identifier: "logo", url: url, options: nil)
            content.attachments = [attachment]
        } catch {
            print("The attachment was not loaded.")
        }
    }

    let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)

    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    UNUserNotificationCenter.current().add(request) {(error) in
        if let error = error {
            print("Uh oh! We had an error: \(error)")
        }
    }
}




}


let delegate = UIApplication.shared.delegate as? ViewController

var phoneNumbered = delegate?.phoneNumber



//If Notification Action is pressed
extension AppDelegate: UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter,     didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {


    if response.actionIdentifier == "call" {
        self.showAlertAppDelegate(title: "Enter Call", message: "Are you sure?", buttonTitle1: "Go", buttonTitle2: "Cancel", window: self.window!)
















    }


}
}
实际编号
的类型为
字符串?
。正如你所注意到的,它实际上是零。因此,当你插值时:

        if let url = URL(string: "tel://nil") {

电话号码“NIL”为。

您如何确认PhoneNumber包含showAlertAppDelegate中的完整号码?为什么它不是一个参数?是的,但要重复这个问题,您做了什么来确认它在指定给actualNumber的点上包含了您期望它包含的内容。例如,您是否在调试器中检查过它的值。那么,您如何设置它、如何填充它、如何共享它、如何在VC和AppDelegate之间访问它是不正确的。当您没有显示任何相关代码时,如何向您展示如何设置它?其余的都在哪里。@Mungbeans我将完整的AppDelegate.swift代码放入其中,那么如何修正这个nil值,使其等于我在UITextField中键入的值您当前的代码毫无意义。您对委托和phonenumberd的定义是循环的,几乎可以肯定为零。(你似乎试图将你的应用程序委托视为一个视图控制器,这是一个我以前从未见过的错误,因此很难对ViewController中可能存在的其他错误进行反向工程。)如果你试图读取应用程序委托中的文本字段,那么你已经遇到了一个大问题。我可能会重新开始,但要进行调试,请遍历代码并在任何地方使用?零分。
        if let url = URL(string: "tel://nil") {