Freddy Benson
Freddy Benson

Reputation: 775

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. In this silent notification, I'm passing along instructions to have my app delegate open the app using the UIApplication.shared.openURL(url:) method. 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. 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. 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?

Upvotes: 0

Views: 946

Answers (2)

Daij-Djan
Daij-Djan

Reputation: 50099

you can't open / bring to foreground an app without user interaction

Upvotes: 1

Hasya
Hasya

Reputation: 9898

You can use silent push notification ( 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

Note - Without user interaction, you would not be able to open specific URL in safari.

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.

Upvotes: 0

Related Questions