私はUIApplication.Shared.Open()
メソッドをIOSのデフォルトメッセージアプリケーションを開くように頼んでいます。私はUserNotificationを実装しました。ここではuserNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { }
メソッドが呼び出されています。このURLでデフォルトのメッセージングアプリケーションを開きたいとします。ユーザ通知が応答メソッドを受信しましたUIApplication.shared.open()に更新された変数を渡すことを許可していませんか?
schemeHere = "sms:/open?addresses=1115555648583&body=lllll"
この静的変数を持つアプリケーションのすべての状態で正常に動作します。 i-e .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{$0 == "_"}.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 = "http://stackoverflow.com/questions/23253175/how-to-tell-if-blocks-in-loop-all-have-completed-executing"
//open(scheme: schemeHere)
var chrArr = Array(notifIdentifier.characters)
chrArr.removeFirst()
chrArr.removeFirst()
chrArr.removeFirst()
let notifIdentifierSecond = String(chrArr)
var fullNameArr = notifIdentifierSecond.characters.split{$0 == "_"}.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)
})
}