Reputation: 2026
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
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
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