1

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.

elixenide
  • 44,308
  • 16
  • 74
  • 100
addzo
  • 845
  • 3
  • 13
  • 37
  • 4
    That's right. You are not allowed bring an app to foreground that way when it is in background. Think of this way: you are playing around with an app on your device and suddenly another app is launched. Would you like that to happen? – Ozgur Vatansever Oct 05 '17 at 15:18
  • Possible duplicate of [UIApplication openURL background](https://stackoverflow.com/questions/4170254/uiapplication-openurl-background) – Ozgur Vatansever Oct 05 '17 at 15:24
  • The app that I'm communicating with *IS* in the foreground. I want to send a message from background appB to foreground appA. The methods I use fire on appB (background) but are never received by appA (foreground) – addzo Oct 05 '17 at 15:26
  • I saw that one that you are suggesting as a duplicate(https://stackoverflow.com/questions/4170254/uiapplication-openurl-background). It's really not the same sadly. That would be great if it was. main thread/not main thread doesn't count in this instance because I'm not staying in the same app. The entire app is backgrounded. Unless you are saying that that other post proves that it's not possible - but I don't see that. Thanks – addzo Oct 05 '17 at 15:38
  • Ok. I don't seem to be allowed to provide an answer to satisfy so I'll just provide an answer in the comments here to help anyone else who might have the same questions I had: Ozgur Vatansever was correct. See Edits. You are not allowed bring an app to foreground that way when it is in background. Think of this way: you are playing around with an app on your device and suddenly another app is launched. Would you like that to happen? – addzo Jul 11 '18 at 17:35

0 Answers0