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

问题描述:

我很确定我已经知道这次尝试的结果会是什么,但在我开始经历很多努力之前,我应该向某人提出这个问题。以下是我想尝试的:是否可以远程打开我的iOS应用程序?

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

+1

无声推送通知可以在你的情况下,一个解决方案检查该教程http://hayageek.com/ios-silent-push -notifications/ –

+0

Safari扩展程序? - 但在我看来,这是一个更高级的用户。 –

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

它也可以在后台和app终止模式下工作。

一旦您收到静默推送通知,您必须使用通知中的交互式按钮安排本地通知。

由于每个用户的点击,你的应用程序将是开放的,与didFinishLaunchingWithOption跟踪你可以打开特定URL

注 - 无需用户交互,你将无法在Safari中打开特定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 


    } 

} 

Refer推送套件整合的一些材料。

+1

如果您决定走此路线,请不要忘记在项目的功能部分启用背景模式 – HavenB3

可以不开/带到前景中的应用,而无需用户交互