在IOS中打开URL方案 [英] Open URL Schemes in IOS
问题描述
我有2个应用程序,用于不同目的,我不应该允许用户在同一设备中使用这两个应用程序。如何检查是否安装了其他应用程序?
我想我可以使用Open URL来实现它,如下所示,将app bundle id设置为无效,我坚持为我的应用程序获取url
EX:fb://
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 think I can do it using Open URL as following by putting app bundle id which is not working, I am stuck to get url for my app EX : "fb://"
if ([[UIApplication sharedApplication] canOpenURL:@"My App URL"]) {
//App installed
}
else{
//App Not installed
}
推荐答案
你有2个App。现在你想从第二个应用程序打开第一个应用程序。我会给你一步一步的指示,请关注。
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.
我的第一个应用程序名称是LinkAppSample,第二个应用程序名称是LinkSecondApp
步骤1:添加第一个应用中的以下内容 LinkAppSample
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.example.com</string>
<key>CFBundleURLSchemes</key>
<array>
<string>chatapp</string>
</array>
</dict>
</array>
然后你需要添加
<key>LSApplicationQueriesSchemes</key>
<array>
<string>chatapp</string>
</array>
请参阅下面的截图
< a href =https://i.stack.imgur.com/0Hivo.png =nofollow noreferrer>
第2步:转到第二个应用程序。我的第二个应用程序名称是LinkSecondApp.Now点击LinkSecondApp项目。单击TARGETS并单击
info.Then单击URL类型并在URL方案中添加或写入或设置名称
目标 - >信息 - > URL类型 - >添加URL类型
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
首先点击目标信息
然后点击URL Tpes。点击它就会显示+符号。
Then click URL Tpes.Once you click it shos you the + symbol.
现在点击+符号并在URL Schemes中提供应用程序的名称。您必须将Rote设置为Viewer
Now click + symbol and give the name of the app in URL Schemes.You must set Rote as Viewer
注意:您的网址方案名称必须相同(首先App Plist Name和Second App URL Schemes name is chatapp here。
第3步:现在你必须在第二个中添加代码应用程序LinkSecondApp for
打开第一个应用程序。
STEP 3: Now you must add code in Second application LinkSecondApp for opening the First app.
LinkSecondApp.m
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];
}
}
这篇关于在IOS中打开URL方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!