I'm trying to send a message to an app (appA) from another app that is in the background (appB).
Seems like from he Apple docs (https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html) that it's possible.
What I am seeing, however, is that I can launch appA from appB when appB is in the foreground.
When appB is in the background the methods fire (I see the print statements in Xcode) but they are not received in the
application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool
function in the app delegate of appA
Is this behavior not allowed?
I have even added the background modes for appB in the plist.
class ViewController: UIViewController {
let kUrlSceme = "customScheme://?hello"
@IBOutlet weak var logTxtView: UITextView!
var runningLog: String = ""
var executionTimer: Timer!
var backgroundTask: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid
override func viewDidLoad() {
super.viewDidLoad()
self.openOtherApp()
executionTimer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(self.openOtherAppFromTimer), userInfo: nil, repeats: true)
registerBackgroundTask()
}
func registerBackgroundTask() {
let str: String = "Background task started."
print(str)
runningLog.append(str)
logTxtView.text = runningLog
backgroundTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
[unowned self] in
self.endBackgroundTask()
})
assert(backgroundTask != UIBackgroundTaskInvalid)
}
func endBackgroundTask() {
let str: String = "Background task ended."
print(str)
runningLog.append(str)
logTxtView.text = runningLog
// UIApplication.shared.endBackgroundTask(backgroundTask)
// backgroundTask = UIBackgroundTaskInvalid
}
@objc func openOtherAppFromTimer() {
// executionTimer.invalidate()
var str: String = "Attempting to call fromt he timer."
runningLog.append(str)
logTxtView.text = runningLog
//
// self.openOtherApp()
let theURLSceme = URL(string: kUrlSceme)!
if UIApplication.shared.canOpenURL(theURLSceme) {
UIApplication.shared.open(theURLSceme)
str = "We have permission to open the URL (\(kUrlSceme)).\n"
print(str)
runningLog.append(str)
logTxtView.text = runningLog
} else {
str = "Looks like we don't have permission to open \(kUrlSceme).\n"
print(str)
runningLog.append(str)
logTxtView.text = str
}
}
func openOtherApp() {
var str: String = ""
if openDeepLinkScheme(customURLScheme: kUrlSceme) {
str = "app was opened with the URL: \(kUrlSceme)\n\n"
print(str)
runningLog.append(str)
logTxtView.text = runningLog
} else {
str = "app was not opened\n"
print(str)
runningLog.append(str)
logTxtView.text = runningLog
}
}
func openDeepLinkScheme(customURLScheme: String) -> Bool {
let theURLSceme = URL(string: customURLScheme)!
var str: String = ""
if UIApplication.shared.canOpenURL(theURLSceme) {
UIApplication.shared.open(theURLSceme)
str = "We have permission to open the URL (\(kUrlSceme)).\n"
print(str)
runningLog.append(str)
logTxtView.text = runningLog
return true
}
str = "Looks like we don't have permission to open \(kUrlSceme).\n"
print(str)
runningLog.append(str)
logTxtView.text = str
return false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Thanks for the help.