繁体   English   中英

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

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

我敢肯定,我已经知道这种尝试的结果会是什么,但是在我不费吹灰之力地开始工作之前,我可能应该问一下这件事。 这是我想尝试的方法:

我正在用Swift开发一个iOS应用,我只是写了一个PHP脚本来向我的设备发送静默推送通知。 在此静默通知中,我传递了使用UIApplication.shared.openURL(url:)方法让我的应用程序委托打开应用程序的说明。 仅当用户点击在线网页上的某个按钮时,才会运行此PHP脚本,只有当用户在其iPhone设备上使用Safari Web浏览器且我的应用程序已在后台运行时,才能访问该PHP脚本,因此没有机会这样任何人都可以从iPhone以外的任何其他设备触发PHP脚本,而iPhone已经安装了我的应用程序并在后台运行。 如果您想知道为什么我要使用这种解决方法,同时我也可以只使用URL方案或深层链接之类的简单方法,那很简单:首先,深层链接需要用户确认他要我的应用程序在实际打开之前打开。 我不希望这样做,而是希望我的应用程序在点击按钮后立即自动打开。 其次,在通过深层链接或通用链接打开应用程序之后,状态栏中会出现一个非常烦人的面包屑按钮,当用户从网页过渡到我的应用程序后,该按钮不再存在。 我已经尽一切努力摆脱了确认提示和面包屑按钮的困扰,这是我能想到的最后一件事。 是否可以使用远程通知来触发openURL方法,即使该应用程序已经打开并在后台运行?

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

您可以使用静默推送通知(Pushkit)。

它还将在后台和应用终止模式下工作。

收到静默推送通知后,您必须在通知中安排带有交互式按钮的本地通知。

按用户点击,您的应用将打开,并在didFinishLaunchingWithOption上进行跟踪,您可以打开特定的URL

注意-如果没有用户交互,您将无法在野生动物园中打开特定的URL

您可以参考以下代码。

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


    }

}

请参考更多材料以实现推包集成。

暂无
暂无

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

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