用户通知是否收到响应方法不允许我将更新的变量传递给 UIApplication.shared.open() ?
User-Notification Did Receive Response Method Does not allow me to Pass Updated Variable to UIApplication.shared.open() ?
我要求 UIApplication.Shared.Open()
方法在 IOS 中打开默认消息应用程序。我已经实现了 UserNotification,其中调用了 userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { }
方法,我想用这个 URL.
打开默认的消息应用程序
schemeHere = "sms:/open?addresses=1115555648583&body=lllll"
使用这个静态变量,它适用于应用程序的所有状态。即 .active .background .inactive
.
当我试图通过某种逻辑为每个通知提供 schemeHere
的更新值时,问题就出现了。
程序不接受更新后的值。
这是我的完整偏好代码。
// For handling tap and user actions
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
self.reloadEasy()
//let networkInfo = CTTelephonyNetworkInfo()
//let carrier = networkInfo.subscriberCellularProvider
// Get carrier name
var countryCodeH = ""
if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
print(countryCode)
countryCodeH = self.phoneCodes[countryCode]!
}
//var numbersH = ""
//var messageH = ""
//var schemeHere = ""
let notifIdentifier = response.notification.request.identifier
print(notifIdentifier)
if notifIdentifier.contains("sms")
{
//open(scheme: schemeHere)
/*
var chrArr = Array(notifIdentifier.characters)
chrArr.removeFirst()
chrArr.removeFirst()
chrArr.removeFirst()
notifIdentifier = String(chrArr)
var fullNameArr = notifIdentifier.characters.split{[=12=] == "_"}.map(String.init)
let numbers = fullNameArr[1]
messageH = fullNameArr[2]
numbersH.removeAll()
for c in numbers.characters
{
if c != " "
{
numbersH.append(c)
}
}
print(messageH)
print(numbersH)
// "sms:/open?addresses=1-408-555-1212,1-408-555-2121,1-408-555-1221&body=/*message*/"
//sms:/open?addresses=(555)564-8583&body=/*No Bofy*/
schemeHere = "sms:/open?addresses=1" + numbersH + "&body=" + messageH
//schemeHere1-408-555-1212,1-408-555-2121,1-408-555-1221, = "sms:/open?addresses=542-342-3423,23232323&body=/*No Bofy*/"
schemeHere = "sms:/open?addresses=5555648583&body=lllll"
*/
let state: UIApplicationState = UIApplication.shared.applicationState
if state == .active {
self.showMessage(message: "Active \(countryCodeH)")
//let schemeHere = "sms:/open?addresses=1-408-555-1212,1-408-555-2121,1-408-555-1221,92-315-5849537&body=/*message*/"
self.open(notifIdentifier: notifIdentifier)
}
else if state == .background
{
self.showMessage(message: "Background \(countryCodeH)")
self.open(notifIdentifier: notifIdentifier)
}
else if state == .inactive
{
self.showMessage(message: "In-Active \(countryCodeH)")
//let schemeHere = "sms:/open?addresses=14085551212,14085552121,14085551221,923155849537&body=/*message*/"
self.open(notifIdentifier: notifIdentifier)
}
else
{
self.showMessage(message: "Un-defined State \(countryCodeH)")
//let schemeHere = "sms:/open?addresses=14085551212,14085552121,14085551221,923155849537&body=/*message*/"
self.open(notifIdentifier: notifIdentifier)
}
}
}
这是被调用的函数。
func open(notifIdentifier: String) {
DispatchQueue.main.async(execute: { () -> Void in
var numbersH = ""
var messageH = ""
var schemeHere = ""
//open(scheme: schemeHere)
var chrArr = Array(notifIdentifier.characters)
chrArr.removeFirst()
chrArr.removeFirst()
chrArr.removeFirst()
let notifIdentifierSecond = String(chrArr)
var fullNameArr = notifIdentifierSecond.characters.split{[=13=] == "_"}.map(String.init)
let numbers = fullNameArr[1]
messageH = fullNameArr[2]
numbersH.removeAll()
for c in numbers.characters
{
if c != " "
{
numbersH.append(c)
}
}
print(messageH)
print(numbersH)
// "sms:/open?addresses=1-408-555-1212,1-408-555-2121,1-408-555-1221&body=/*message*/"
//sms:/open?addresses=(555)564-8583&body=/*No Bofy*/
schemeHere = "sms:/open?addresses=1" + numbersH + "&body=" + "Wanted to Inform You!"
//schemeHere1-408-555-1212,1-408-555-2121,1-408-555-1221, = "sms:/open?addresses=542-342-3423,23232323&body=/*No Bofy*/"
//sms:/open?addresses=15555648583&body=Wanted to Inform You!
//schemeHere = "sms:/open?addresses=1115555648583&body=lllll"
let deadlineTime = DispatchTime.now() + .seconds(1)
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
print(schemeHere)
let when = DispatchTime.now() + 2 // change 2 to desired number of seconds
DispatchQueue.main.asyncAfter(deadline: when) {
// Your code with delay
if let url = URL(string: schemeHere) {
print("Inside URL")
if #available(iOS 10, *) {
print("Now Doing in IOS 10.")
UIApplication.shared.open(url, options: [:],
completionHandler: {
(success) in
print("Open \(schemeHere): \(success)")
})
} else {
let success = UIApplication.shared.openURL(url)
print("Open \(schemeHere): \(success)")
}
}
else
{
print("Inside Scheme!")
print(schemeHere)
}
}
print("test")
}
// UIApplication.shared.open(NSURL(string: schemeHere) as! URL, options: [:], completionHandler: nil)
})
}
经过一段时间的演示,我可以通过以下方式澄清这一点。
首先,之前调用了函数 open(URL),因此需要将我的代码放在依赖块中,即使用 IOS GCD(Grand Central Dispatch)。
我创建了两个函数并将它们放在 dispatch_Group
.
这是我的代码:
// Function A
func functionA_With_Dispatch(identif: String) -> String
{
dispatch_group.enter()
if identif.contains("sms")
{
var numbersH = ""
var messageH = ""
var schemeHere = ""
var chrArr = Array(identif.characters)
chrArr.removeFirst()
chrArr.removeFirst()
chrArr.removeFirst()
let notifIdentifierSecond = String(chrArr)
var fullNameArr = notifIdentifierSecond.characters.split{[=10=] == "_"}.map(String.init)
let numbers = fullNameArr[1]
messageH = fullNameArr[2]
numbersH.removeAll()
for c in numbers.characters
{
if c != " "
{
numbersH.append(c)
}
}
print(messageH)
print(numbersH)
// "sms:/open?addresses=1-408-555-1212,1-408-555-2121,1-408-555-1221&body=/*message*/"
//sms:/open?addresses=(555)564-8583&body=/*No Bofy*/
schemeHere = "sms:/open?addresses=1" + numbersH + "&body=" + "Wanted to Inform You!"
//schemeHere1-408-555-1212,1-408-555-2121,1-408-555-1221, = "sms:/open?addresses=542-342-3423,23232323&body=/*No Bofy*/"
//sms:/open?addresses=15555648583&body=Wanted to Inform You!
//Open sms:/open?addresses=1115555648583&body=lllll: true
schemeHere = "sms:/open?addresses=\(numbersH)&body=\(messageH)"
print(schemeHere)
dispatch_group.leave()
return schemeHere
}
dispatch_group.leave()
return "empty"
}
打开URL的其他功能。
func open_URL_With_Dispatch(schemeHere : String)
{
dispatch_group.enter()
if let url = URL(string: schemeHere) {
print("Inside URL")
if #available(iOS 10, *) {
print("Now Doing in IOS 10.")
UIApplication.shared.open(url, options: [:],
completionHandler: {
(success) in
print("Open \(schemeHere): \(success)")
self.dispatch_group.leave()
})
} else {
let success = UIApplication.shared.openURL(url)
print("Open \(schemeHere): \(success)")
dispatch_group.leave()
}
}
else
{
print("Inside Scheme!")
print(schemeHere)
dispatch_group.leave()
}
}
现在 userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { }
,工作在这里。
注意:dispatch_group
在 viewController 的顶部声明。
dispatch_group = DispatchGroup()
let notifIdentifier = response.notification.request.identifier
self.reloadEasy()
let schemeHere = self.functionA_With_Dispatch(identif: notifIdentifier)
dispatch_group.wait()
/*
switch response.actionIdentifier {
case "action1":
if MFMessageComposeViewController.canSendText() {
print("SMS services are available")
self.sendMessage(recipients: ["243243"], message: "ewhrewuew")
}
else
{
self.showMessage(message: "Can't send sms!")
print("Can't send sms!")
}
default:
if MFMessageComposeViewController.canSendText() {
print("SMS services are available")
self.sendMessage(recipients: ["243243"], message: "ewhrewuew")
}
else
{
self.showMessage(message: "Can't send sms!")
print("Can't send sms!")
}
} // Message Switch
*/
let state: UIApplicationState = UIApplication.shared.applicationState
if state == .active {
self.showMessage(message: "Active")
//let schemeHere = "sms:/open?addresses=1-408-555-1212,1-408-555-2121,1-408-555-1221,92-315-5849537&body=/*message*/"
//self.open(notifIdentifier: notifIdentifier)
self.open_URL_With_Dispatch(schemeHere: schemeHere)
}
else if state == .background
{
self.showMessage(message: "Background")
//self.open(notifIdentifier: notifIdentifier)
self.open_URL_With_Dispatch(schemeHere: schemeHere)
}
else if state == .inactive
{
self.showMessage(message: "In-Active")
//let schemeHere = "sms:/open?addresses=14085551212,14085552121,14085551221,923155849537&body=/*message*/"
//self.open(notifIdentifier: notifIdentifier)
self.open_URL_With_Dispatch(schemeHere: schemeHere)
}
else
{
self.showMessage(message: "Un-defined State")
//let schemeHere = "sms:/open?addresses=14085551212,14085552121,14085551221,923155849537&body=/*message*/"
//self.open(notifIdentifier: notifIdentifier)
self.open_URL_With_Dispatch(schemeHere: schemeHere)
}
其次,这样做之后我发现了另一个问题,我在 [=40] 中传递了 spaces 和 newLines =] 字符串,但现在我将按以下方式为整个 URL.
替换它们
for c in messageHere.characters
{
if c == " "
{
appendedMessage.append("%20")
}
else if c == "\n"
{
appendedMessage.append("%0A")
}
else
{
appendedMessage.append(c)
}
}
我要求 UIApplication.Shared.Open()
方法在 IOS 中打开默认消息应用程序。我已经实现了 UserNotification,其中调用了 userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { }
方法,我想用这个 URL.
schemeHere = "sms:/open?addresses=1115555648583&body=lllll"
使用这个静态变量,它适用于应用程序的所有状态。即 .active .background .inactive
.
当我试图通过某种逻辑为每个通知提供 schemeHere
的更新值时,问题就出现了。
程序不接受更新后的值。 这是我的完整偏好代码。
// For handling tap and user actions
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
self.reloadEasy()
//let networkInfo = CTTelephonyNetworkInfo()
//let carrier = networkInfo.subscriberCellularProvider
// Get carrier name
var countryCodeH = ""
if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
print(countryCode)
countryCodeH = self.phoneCodes[countryCode]!
}
//var numbersH = ""
//var messageH = ""
//var schemeHere = ""
let notifIdentifier = response.notification.request.identifier
print(notifIdentifier)
if notifIdentifier.contains("sms")
{
//open(scheme: schemeHere)
/*
var chrArr = Array(notifIdentifier.characters)
chrArr.removeFirst()
chrArr.removeFirst()
chrArr.removeFirst()
notifIdentifier = String(chrArr)
var fullNameArr = notifIdentifier.characters.split{[=12=] == "_"}.map(String.init)
let numbers = fullNameArr[1]
messageH = fullNameArr[2]
numbersH.removeAll()
for c in numbers.characters
{
if c != " "
{
numbersH.append(c)
}
}
print(messageH)
print(numbersH)
// "sms:/open?addresses=1-408-555-1212,1-408-555-2121,1-408-555-1221&body=/*message*/"
//sms:/open?addresses=(555)564-8583&body=/*No Bofy*/
schemeHere = "sms:/open?addresses=1" + numbersH + "&body=" + messageH
//schemeHere1-408-555-1212,1-408-555-2121,1-408-555-1221, = "sms:/open?addresses=542-342-3423,23232323&body=/*No Bofy*/"
schemeHere = "sms:/open?addresses=5555648583&body=lllll"
*/
let state: UIApplicationState = UIApplication.shared.applicationState
if state == .active {
self.showMessage(message: "Active \(countryCodeH)")
//let schemeHere = "sms:/open?addresses=1-408-555-1212,1-408-555-2121,1-408-555-1221,92-315-5849537&body=/*message*/"
self.open(notifIdentifier: notifIdentifier)
}
else if state == .background
{
self.showMessage(message: "Background \(countryCodeH)")
self.open(notifIdentifier: notifIdentifier)
}
else if state == .inactive
{
self.showMessage(message: "In-Active \(countryCodeH)")
//let schemeHere = "sms:/open?addresses=14085551212,14085552121,14085551221,923155849537&body=/*message*/"
self.open(notifIdentifier: notifIdentifier)
}
else
{
self.showMessage(message: "Un-defined State \(countryCodeH)")
//let schemeHere = "sms:/open?addresses=14085551212,14085552121,14085551221,923155849537&body=/*message*/"
self.open(notifIdentifier: notifIdentifier)
}
}
}
这是被调用的函数。
func open(notifIdentifier: String) {
DispatchQueue.main.async(execute: { () -> Void in
var numbersH = ""
var messageH = ""
var schemeHere = ""
//open(scheme: schemeHere)
var chrArr = Array(notifIdentifier.characters)
chrArr.removeFirst()
chrArr.removeFirst()
chrArr.removeFirst()
let notifIdentifierSecond = String(chrArr)
var fullNameArr = notifIdentifierSecond.characters.split{[=13=] == "_"}.map(String.init)
let numbers = fullNameArr[1]
messageH = fullNameArr[2]
numbersH.removeAll()
for c in numbers.characters
{
if c != " "
{
numbersH.append(c)
}
}
print(messageH)
print(numbersH)
// "sms:/open?addresses=1-408-555-1212,1-408-555-2121,1-408-555-1221&body=/*message*/"
//sms:/open?addresses=(555)564-8583&body=/*No Bofy*/
schemeHere = "sms:/open?addresses=1" + numbersH + "&body=" + "Wanted to Inform You!"
//schemeHere1-408-555-1212,1-408-555-2121,1-408-555-1221, = "sms:/open?addresses=542-342-3423,23232323&body=/*No Bofy*/"
//sms:/open?addresses=15555648583&body=Wanted to Inform You!
//schemeHere = "sms:/open?addresses=1115555648583&body=lllll"
let deadlineTime = DispatchTime.now() + .seconds(1)
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
print(schemeHere)
let when = DispatchTime.now() + 2 // change 2 to desired number of seconds
DispatchQueue.main.asyncAfter(deadline: when) {
// Your code with delay
if let url = URL(string: schemeHere) {
print("Inside URL")
if #available(iOS 10, *) {
print("Now Doing in IOS 10.")
UIApplication.shared.open(url, options: [:],
completionHandler: {
(success) in
print("Open \(schemeHere): \(success)")
})
} else {
let success = UIApplication.shared.openURL(url)
print("Open \(schemeHere): \(success)")
}
}
else
{
print("Inside Scheme!")
print(schemeHere)
}
}
print("test")
}
// UIApplication.shared.open(NSURL(string: schemeHere) as! URL, options: [:], completionHandler: nil)
})
}
经过一段时间的演示,我可以通过以下方式澄清这一点。
首先,之前调用了函数 open(URL),因此需要将我的代码放在依赖块中,即使用 IOS GCD(Grand Central Dispatch)。
我创建了两个函数并将它们放在 dispatch_Group
.
这是我的代码:
// Function A
func functionA_With_Dispatch(identif: String) -> String
{
dispatch_group.enter()
if identif.contains("sms")
{
var numbersH = ""
var messageH = ""
var schemeHere = ""
var chrArr = Array(identif.characters)
chrArr.removeFirst()
chrArr.removeFirst()
chrArr.removeFirst()
let notifIdentifierSecond = String(chrArr)
var fullNameArr = notifIdentifierSecond.characters.split{[=10=] == "_"}.map(String.init)
let numbers = fullNameArr[1]
messageH = fullNameArr[2]
numbersH.removeAll()
for c in numbers.characters
{
if c != " "
{
numbersH.append(c)
}
}
print(messageH)
print(numbersH)
// "sms:/open?addresses=1-408-555-1212,1-408-555-2121,1-408-555-1221&body=/*message*/"
//sms:/open?addresses=(555)564-8583&body=/*No Bofy*/
schemeHere = "sms:/open?addresses=1" + numbersH + "&body=" + "Wanted to Inform You!"
//schemeHere1-408-555-1212,1-408-555-2121,1-408-555-1221, = "sms:/open?addresses=542-342-3423,23232323&body=/*No Bofy*/"
//sms:/open?addresses=15555648583&body=Wanted to Inform You!
//Open sms:/open?addresses=1115555648583&body=lllll: true
schemeHere = "sms:/open?addresses=\(numbersH)&body=\(messageH)"
print(schemeHere)
dispatch_group.leave()
return schemeHere
}
dispatch_group.leave()
return "empty"
}
打开URL的其他功能。
func open_URL_With_Dispatch(schemeHere : String)
{
dispatch_group.enter()
if let url = URL(string: schemeHere) {
print("Inside URL")
if #available(iOS 10, *) {
print("Now Doing in IOS 10.")
UIApplication.shared.open(url, options: [:],
completionHandler: {
(success) in
print("Open \(schemeHere): \(success)")
self.dispatch_group.leave()
})
} else {
let success = UIApplication.shared.openURL(url)
print("Open \(schemeHere): \(success)")
dispatch_group.leave()
}
}
else
{
print("Inside Scheme!")
print(schemeHere)
dispatch_group.leave()
}
}
现在 userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { }
,工作在这里。
注意:dispatch_group
在 viewController 的顶部声明。
dispatch_group = DispatchGroup()
let notifIdentifier = response.notification.request.identifier
self.reloadEasy()
let schemeHere = self.functionA_With_Dispatch(identif: notifIdentifier)
dispatch_group.wait()
/*
switch response.actionIdentifier {
case "action1":
if MFMessageComposeViewController.canSendText() {
print("SMS services are available")
self.sendMessage(recipients: ["243243"], message: "ewhrewuew")
}
else
{
self.showMessage(message: "Can't send sms!")
print("Can't send sms!")
}
default:
if MFMessageComposeViewController.canSendText() {
print("SMS services are available")
self.sendMessage(recipients: ["243243"], message: "ewhrewuew")
}
else
{
self.showMessage(message: "Can't send sms!")
print("Can't send sms!")
}
} // Message Switch
*/
let state: UIApplicationState = UIApplication.shared.applicationState
if state == .active {
self.showMessage(message: "Active")
//let schemeHere = "sms:/open?addresses=1-408-555-1212,1-408-555-2121,1-408-555-1221,92-315-5849537&body=/*message*/"
//self.open(notifIdentifier: notifIdentifier)
self.open_URL_With_Dispatch(schemeHere: schemeHere)
}
else if state == .background
{
self.showMessage(message: "Background")
//self.open(notifIdentifier: notifIdentifier)
self.open_URL_With_Dispatch(schemeHere: schemeHere)
}
else if state == .inactive
{
self.showMessage(message: "In-Active")
//let schemeHere = "sms:/open?addresses=14085551212,14085552121,14085551221,923155849537&body=/*message*/"
//self.open(notifIdentifier: notifIdentifier)
self.open_URL_With_Dispatch(schemeHere: schemeHere)
}
else
{
self.showMessage(message: "Un-defined State")
//let schemeHere = "sms:/open?addresses=14085551212,14085552121,14085551221,923155849537&body=/*message*/"
//self.open(notifIdentifier: notifIdentifier)
self.open_URL_With_Dispatch(schemeHere: schemeHere)
}
其次,这样做之后我发现了另一个问题,我在 [=40] 中传递了 spaces 和 newLines =] 字符串,但现在我将按以下方式为整个 URL.
替换它们 for c in messageHere.characters
{
if c == " "
{
appendedMessage.append("%20")
}
else if c == "\n"
{
appendedMessage.append("%0A")
}
else
{
appendedMessage.append(c)
}
}