目前我正在处理一个应用程序,当我在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!)
}
}
}
答案 0 :(得分:0)
let actualNumber = phoneNumbered?.text
actualNumber
的类型为String?
。如你所知,它实际上是零。所以当你插入那个:
if let url = URL(string: "tel://nil") {
电话号码“NIL”为645。