Call back url for iOS app?
Asked Answered
D

3

9

I am working on a project where I share video on Vimeo. In this My app open a video where user needs to press authorize button to authorize the app at Vimeo and to get access tokens. So, for this, my app opens safari and open Vimeo's site there. The user needs to press allow button then it has to come back again to the app. But I am not able to know what should be the call back url to make the Safari/Vimeo to come back to my app.

Please suggest your views regarding this.

Davy answered 9/7, 2013 at 5:6 Comment(2)
Can you open the url in UIWebView and not in Safari?Heilungkiang
@AviTsadok No, Because I am using Vimeo's API and it opens the url in Safari.Davy
A
20

You need to set a custom URL scheme for your app by editing your app's Info.plist. There's plenty of documentation about this on Apple's developer website. Here's an article that goes into detail: http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html

Then your website just needs to open a url that uses your app's url scheme (eg: myappscheme://do/something/cool?foo=bar). If your app cares about any data passed in to it via your website then implement the "application:openURL:sourceApplication:annotation:" method and inspect the NSURL passed in. You can read more about this in Apple's documentation: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html

Ambrosio answered 9/7, 2013 at 5:14 Comment(0)
H
3

You need to implement something called 'URL Scheme' to your app, which means register your app to a certain url, so it can be opened from.

1) You should add a row to your info.plist file. 2) You need to listen to the url in your app, and do what needed.

Google for more info...

Heilungkiang answered 9/7, 2013 at 5:14 Comment(0)
F
1

To support a custom URL scheme:

  1. Define the format for your app's URLs.
  2. Register your scheme so that the system directs appropriate URLs to your app.
  3. Handle the URLs that your app receives.

URLs must start with your custom scheme name. Add parameters for any options your app supports. For example, a photo library app might define a URL format that includes the name or index of a photo album to display.

an example is :

myphotoapp:albumname?name="foods"
myphotoapp:albumname?index=1

Register Your URL Scheme

enter image description here

  1. click on project target and goto info page
  2. in the info page expand URL Types section and hit the + button
  3. fill fields with appropriate values.

Handle Incoming URLs

The system delivers the URL to your app by calling your app delegate's application(_:open:options:)method. you can useNSURLComponents` APIs to extract the components. Obtain additional information about the URL, such as which app opened it, from the system-provided options dictionary.

func application(_ application: UIApplication,
                 open url: URL,
                 options: [UIApplicationOpenURLOptionsKey : Any] = [:] ) -> Bool {
    
    // Determine who sent the URL.
    let sendingAppID = options[.sourceApplication]
    print("source application = \(sendingAppID ?? "Unknown")")
    
    // Process the URL.
    guard let components = NSURLComponents(url: url, resolvingAgainstBaseURL: true),
        let albumPath = components.path,
        let params = components.queryItems else {
            print("Invalid URL or album path missing")
            return false
    }
    
    if let photoIndex = params.first(where: { $0.name == "index" })?.value {
        print("albumPath = \(albumPath)")
        print("photoIndex = \(photoIndex)")
        return true
    } else {
        print("Photo index missing")
        return false
    }
}

If your app has opted into Scenes, and your app is not running, the system delivers the URL to the scene(_:willConnectTo:options:) delegate method after launch, and to scene(_:openURLContexts:) when your app opens a URL while running or suspended in memory.

func scene(_ scene: UIScene, 
           willConnectTo session: UISceneSession, 
           options connectionOptions: UIScene.ConnectionOptions) {

    // Determine who sent the URL.
    if let urlContext = connectionOptions.urlContexts.first {
            
        let sendingAppID = urlContext.options.sourceApplication
        let url = urlContext.url
        print("source application = \(sendingAppID ?? "Unknown")")
        print("url = \(url)")
            
        // Process the URL similarly to the UIApplicationDelegate example.
    }
}

Fugue answered 27/2, 2021 at 8:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.