I have 2 apps, which are meant for different purpose, where I should not allow user to use both apps in same device. How can I check whether other app installed or not? I t
You have 2 Apps.Now you want to open First App from the Second App.I will give you the step by step instruction please follow that.
My First application name is LinkAppSample and Second application Name is LinkSecondApp
STEP 1: Add the below things in first app LinkAppSample
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.example.com</string>
<key>CFBundleURLSchemes</key>
<array>
<string>chatapp</string>
</array>
</dict>
</array>
Then you need to add
<key>LSApplicationQueriesSchemes</key>
<array>
<string>chatapp</string>
</array>
See the below screenshot below
STEP 2: Go to Second App.My Second App Name is LinkSecondApp.Now click LinkSecondApp project.Click TARGETS and click info.Then Click URL Type and add or write or set name in URL Schemes Target->Info->URL types->Add url types
First Click Info of Targets
Then click URL Tpes.Once you click it shos you the + symbol.
Now click + symbol and give the name of the app in URL Schemes.You must set Rote as Viewer
NOTE:Your URL Schemes name must be same(First App Plist Name and Second App URL Schemes name is chatapp here).
STEP 3: Now you must add code in Second application LinkSecondApp for opening the First app.
LinkSecondApp.m
-(IBAction)actionOpenFirstApp:(id)sender
{
NSString *customURL = @"chatapp://";
UIApplication *application = [UIApplication sharedApplication];
NSURL *URL = [NSURL URLWithString:@"chatapp://"];
if ([application respondsToSelector:@selector(openURL:options:completionHandler:)])
{
[application openURL:URL options:@{}
completionHandler:^(BOOL success) {
NSLog(@"Open %@: %d",customURL,success);
}];
}
else {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error!" message:@"No Custom URL is defined" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:ok];
[self presentViewController:alertController animated:YES completion:nil];
}
}
To know whether an app is installed or not
-(BOOL) isAppInstalled:(NSString *)appUrl{
return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:appUrl]]; // @ is not needed as you are passing input of function as a parameter.
}
To know whether any app got installed or not, invoke this function
if( [self isAppInstalled:@"MyApp2://"]) {
NSLog("The second app is installed"); //Perform your actions
}
Remember to include the second app's url scheme in First Apps LSApplicationURLScheme in its Info.plist.
go to the Project > Target > Info > URL Types section in Xcode. Here we must add two URL schemes that the Sign-In SDK needs to be properly set up so as to properly work. Use the plus button, and in the URL Schemes field just type the app bundle ID value (e.g. com.admin.myApp).
Then in your code use this
if ([[UIApplication sharedApplication] canOpenURL:@"com.admin.myApp://"]) {
//App installed
}
else{
//App Not installed
}
I've just went thru this and found the accepted answer very confusing so I'm making mine.
You can do that in text directly in the app1.plist file OR via the visual info Editor (Target > Info > URLTypes)
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>CFBundleURLName</key>
<string>com.mycompany.myapp</string>
<key>CFBundleURLSchemes</key>
<array>
<string>myscheme</string>
</array>
</dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>myscheme</string>
</array>
- (void)callApp1
{
NSString *customURL = @"myscheme://";
UIApplication *application = [UIApplication sharedApplication];
NSURL *URL = [NSURL URLWithString:customURL];
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
[application openURL:URL options:@{}
completionHandler:^(BOOL success) {
NSLog(@"Open %@: %d",customURL,success);
}];
} else {
BOOL success = [application openURL:URL];
NSLog(@"Open %@: %d",customURL,success);
}
}
else {
if ([application canOpenURL:URL]) {
[application openURL:URL];
}
}
}
Of course the scheme (here myscheme must match in the three parts).
Pretty easy and to my knowledge it's all that's needed !
Swift 3.0 working Answer :
Everything remains same as Selected answer ( above answer which is answered by user3182143 as this causes few issue by other user so i answered this way , only code implementation is different rest follow same step as answered by above correct answer )
But code implementation is different now :
let kCustomURLScheme = "yourappscheme://"
func openCustomApp() {
if (UIApplication.shared.canOpenURL(URL(string:kCustomURLScheme)!)) {
print("true")
}
if openCustomURLScheme(customURLScheme: kCustomURLScheme) {
// app was opened successfully
} else {
// handle unable to open the app, perhaps redirect to the App Store
print("unable to open , go to itune store to download it ")
}
}
This function using APPSCHEME to check whether any installed app with specific APPSCHEME can be launched or not , if yes then it will launch else if you having application iTune link then it will open app link (above function calling below method for final launching ):
func openCustomURLScheme(customURLScheme: String) -> Bool {
let customURL = URL(string: customURLScheme)!
if UIApplication.shared.canOpenURL(customURL) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(customURL)
UIApplication.shared.open(URL(string: "https://itunes.apple.com/in/app/appname/id404249815?mt=8")!, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(customURL)
}
return true
}
return false
}
This code working fine for Swift3.0 and iOS 10 , feel free to share your feedback and comment .