来自后台应用程序的Swift UIApplication.shared.open无效

时间:2017-10-05 15:16:20

标签: ios swift background-process deep-linking

我正在尝试从另一个位于后台的应用中向应用(appA)发送消息(appB)。

似乎可以从他的Apple文档(https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html)那里得到它。

然而,我所看到的是,当appB位于前台时,我可以从appB启动appA。

当appB在后台时,方法会触发(我在Xcode中看到打印语句)但是

中没有收到它们
application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool

在appA的app委托中执行

这种行为是不允许的?

我甚至在plist中添加了appB的背景模式。

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.
}


}

感谢您的帮助。

0 个答案:

没有答案