简体   繁体   English

可以远程打开我的iOS应用程序吗?

[英]Is it possible to open my iOS app remotely?

I'm pretty sure I already know what the outcome of this attempt is going to be, but before I start going through a lot of effort for nothing, I should probably just ask someone about this. 我敢肯定,我已经知道这种尝试的结果会是什么,但是在我不费吹灰之力地开始工作之前,我可能应该问一下这件事。 Here's what I want to try: 这是我想尝试的方法:

I'm developing an iOS app in Swift, and I just wrote a PHP script to send a silent push notification to my device. 我正在用Swift开发一个iOS应用,我只是写了一个PHP脚本来向我的设备发送静默推送通知。 In this silent notification, I'm passing along instructions to have my app delegate open the app using the UIApplication.shared.openURL(url:) method. 在此静默通知中,我传递了使用UIApplication.shared.openURL(url:)方法让我的应用程序委托打开应用程序的说明。 This PHP script will only be run if a user taps a certain button on an online web page, which can only be accessed when the user is using Safari web browser on his iPhone device with my app already running in the background, so there's no chance that anyone will be able to trigger the PHP script from any other device than an iPhone which already has my app installed and running in the background. 仅当用户点击在线网页上的某个按钮时,才会运行此PHP脚本,只有当用户在其iPhone设备上使用Safari Web浏览器且我的应用程序已在后台运行时,才能访问该PHP脚本,因此没有机会这样任何人都可以从iPhone以外的任何其他设备触发PHP脚本,而iPhone已经安装了我的应用程序并在后台运行。 If you're wondering why I would use this workaround while I can also just use something as simple as URL schemes or deep linking, well, it's quite simple: first of all, deep linking requires the user to confirm that he wants my app to open before it actually opens. 如果您想知道为什么我要使用这种解决方法,同时我也可以只使用URL方案或深层链接之类的简单方法,那很简单:首先,深层链接需要用户确认他要我的应用程序在实际打开之前打开。 I don't want this, instead, I want my app to open automatically as soon as the button is tapped. 我不希望这样做,而是希望我的应用程序在点击按钮后立即自动打开。 Second, after the app is opened through deep linking or universal links, there's this really annoying breadcrumb button in the status bar, which shouldn't be there anymore after my user transitions from the web page to my app. 其次,在通过深层链接或通用链接打开应用程序之后,状态栏中会出现一个非常烦人的面包屑按钮,当用户从网页过渡到我的应用程序后,该按钮不再存在。 I've tried everything to get rid of the confirmation prompt and breadcrumb button, and this is the last thing I can come up with. 我已经尽一切努力摆脱了确认提示和面包屑按钮的困扰,这是我能想到的最后一件事。 Is it possible to trigger the openURL method using a remote notification, even when the app is already open and running in the background? 是否可以使用远程通知来触发openURL方法,即使该应用程序已经打开并在后台运行?

如果没有用户交互,您将无法打开/将应用程序置于前台

You can use silent push notification ( Pushkit ). 您可以使用静默推送通知(Pushkit)。

It will also work in background and with app terminated mode. 它还将在后台和应用终止模式下工作。

Once you receive silent push notification, you have to schedule local notification with interactive buttons in notification. 收到静默推送通知后,您必须在通知中安排带有交互式按钮的本地通知。

As per user tap, your app will be open and with tracking on didFinishLaunchingWithOption you can open specific URL 按用户点击,您的应用将打开,并在didFinishLaunchingWithOption上进行跟踪,您可以打开特定的URL

Note - Without user interaction, you would not be able to open specific URL in safari. 注意-如果没有用户交互,您将无法在野生动物园中打开特定的URL

You can refer below code for same. 您可以参考以下代码。

import UIKit
import PushKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,PKPushRegistryDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


        let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound]
        application.registerForRemoteNotificationTypes(types)

        self.PushKitRegistration()

        return true
    }

    //MARK: - PushKitRegistration

    func PushKitRegistration()
    {

        let mainQueue = dispatch_get_main_queue()
        // Create a push registry object
        if #available(iOS 8.0, *) {

            let voipRegistry: PKPushRegistry = PKPushRegistry(queue: mainQueue)

            // Set the registry's delegate to self

            voipRegistry.delegate = self

            // Set the push type to VoIP

            voipRegistry.desiredPushTypes = [PKPushTypeVoIP]

        } else {
            // Fallback on earlier versions
        }


    }


    @available(iOS 8.0, *)
    func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) {
        // Register VoIP push token (a property of PKPushCredentials) with server

        let hexString : String = UnsafeBufferPointer<UInt8>(start: UnsafePointer(credentials.token.bytes),
            count: credentials.token.length).map { String(format: "%02x", $0) }.joinWithSeparator("")

        print(hexString)


    }


    @available(iOS 8.0, *)
    func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) {
        // Process the received push


    }

}

Refer some more material for push kit integration. 请参考更多材料以实现推包集成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM