ecnepsnai
ecnepsnai

Reputation: 2026

Catch URL protocol to an app that isn't installed

I'm trying to create a URL protocol that links to my twitter profile in iOS. But I would like it to fall back to Safari if the user does not have the Twitter app installed. How would I do that? This is the code I am using right now:

NSString *stringURL = @"twitter://user?screen_name=ecnepsnai";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];

Upvotes: 2

Views: 336

Answers (2)

mttrb
mttrb

Reputation: 8345

You could use the -canOpenURL: method of UIApplication.

NSString *stringURL = @"twitter://user?screen_name=ecnepsnai";
NSURL *url = [NSURL URLWithString:stringURL];

if ([[UIApplication sharedApplication] canOpenURL:url]) {
    [[UIApplication sharedApplication] openURL:url];
} else {
    // Do something else
}

Upvotes: 2

houbysoft
houbysoft

Reputation: 33392

I believe you can test if the "twitter://" URL can be handled using:

if ([[UIApplication sharedApplication] canOpenURL:url]) {
    [[UIApplication sharedApplication] openURL:url];
} else {
    // fallback
}

Upvotes: 3

Related Questions