Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch MonoTouch iPhone App with Custom Url/Protocol?

Is it possible to register a custom Url scheme or Protocol, like myapp:123, with MonoTouch? I'd like my MonoTouch app to launch when the user clicks this link in a web page, mail or calendar description, and pick upp the url "parameters", in this case "123".

In short I want the same functionality as for the Spotify app in iPhone with its spotify:track:123123 Can it be done?

like image 883
Johan Danforth Avatar asked Aug 30 '12 09:08

Johan Danforth


People also ask

Do iOS apps have URLs?

Nearly all iOS apps assign themselves one or more URL scheme names. Many also code in various path, parameter, anchor, query, and fragment components for specific requests (sometimes called “deep links”).

What is URL scheme in iOS?

URL schema is used as an identifier in launching applications and performing a set of commands in iOS devices. The schema name of a URL is the first part of a URL. (e.g. schemaname:// ). For web pages, the schemas are usually http or https.

How do I find my iOS app URL?

Find app link via App Store on iOS devices Step 1: Go to the Apple App Store on your iOS device. Step 2: Search for your app and go to the app page. Step 3: Tap the Share Sheet button on the top right of the screen, then choose Copy Link.


2 Answers

Yes it is and here's what you need to do.

Add in Info.Plist the following somewhere within the dict tags:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>com.companyname.com.receiver</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>myapp</string>
        </array>
    </dict>
</array>

Then in AppDelegate.cs override the HandleOpenURL method:

public override bool HandleOpenURL (UIApplication application, NSUrl url)
{
    if (url == null) {
        return false;
    }

    var uri = new Uri(url.ToString()); // I prefer working with the Uri class.

    // Your logic here

    return true;
}
like image 167
Candide Avatar answered Sep 23 '22 06:09

Candide


Yes, I've done it in a couple of my MonoTouch applications. It is actually required when you use the Facebook SDK for logging into your app.

In general, follow the same instructions you would for a regular Objective-C app: http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/AdvancedAppTricks/AdvancedAppTricks.html (look under "Communicating with other apps")

In your AppDelegate there is a HandleOpenURL method to override. Beyond that there are just some settings in your Info.plist to change.

like image 43
jonathanpeppers Avatar answered Sep 23 '22 06:09

jonathanpeppers