ios Swift:如何在点击 UIButton 时打开新应用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33932303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift: How to open a new app when UIButton is tapped
提问by thedansaps
I have an app and when a uibutton is clicked, I want to open another app that is already installed (i.e. Waze). How can I do such? Big thanks.
我有一个应用程序,当单击 uibutton 时,我想打开另一个已安装的应用程序(即 Waze)。我怎么能这样做?十分感谢。
回答by Orkhan Alizade
Try this. For example you want to open an Instagram app:
尝试这个。例如你想打开一个 Instagram 应用程序:
var instagramHooks = "instagram://user?username=johndoe"
var instagramUrl = NSURL(string: instagramHooks)
if UIApplication.sharedApplication().canOpenURL(instagramUrl!)
{
UIApplication.sharedApplication().openURL(instagramUrl!)
} else {
//redirect to safari because the user doesn't have Instagram
UIApplication.sharedApplication().openURL(NSURL(string: "http://instagram.com/")!)
}
回答by PK20
In SecondApp
在第二个应用程序中
Go to the plist file of SecondApp and you need to add a URL Schemes with a string iOSDevTips(of course you can write another string.it's up to you).
转到SecondApp的plist文件,你需要添加一个带有字符串iOSDevTips的URL Schemes(当然你可以写另一个字符串,这取决于你)。
2 . In FirstApp
2 . 在第一应用
Create a button with the below action:
使用以下操作创建一个按钮:
- (void)buttonPressed:(UIButton *)button
{
NSString *customURL = @"iOSDevTips://";
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:customURL]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:customURL]];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"URL error"
message:[NSString stringWithFormat:@"No custom URL defined for %@", customURL]
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
}
}
That's it. Now when you can click the button in the FirstApp it should open the SecondApp.
就是这样。现在,当您可以单击 FirstApp 中的按钮时,它应该会打开 SecondApp。
For more info Refer here
有关更多信息,请参阅此处
回答by 0xa6a
You can look up Waze Communityfor reference.
您可以查找位智社区以供参考。
Objective-C code snippet:
Objective-C 代码片段:
if ([[UIApplication sharedApplication]
canOpenURL:[NSURL URLWithString:@"waze://"]]) {
// Waze is installed. Launch Waze and start navigation
NSString *urlStr =
[NSString stringWithFormat:@"waze://?ll=%f,%f&navigate=yes",
latitude, longitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
} else {
// Waze is not installed. Launch AppStore to install Waze app
[[UIApplication sharedApplication] openURL:[NSURL
URLWithString:@"http://itunes.apple.com/us/app/id323229106"]];
}
Swift code snippet:
Swift 代码片段:
if UIApplication.shared.canOpenURL(URL(string: "waze://")!) {
// Waze is installed. Launch Waze and start navigation
let urlStr = String(format: "waze://?ll=%f, %f&navigate=yes", latitude, longitude)
UIApplication.shared.openURL(URL(string: urlStr)!)
} else {
// Waze is not installed. Launch AppStore to install Waze app
UIApplication.shared.openURL(URL(string: "http://itunes.apple.com/us/app/id323229106")!)
}
回答by u.gen
in swift4 waze
在 swift4 位智中
class FullMapVC: UIViewController {
var lat:CLLocationDegrees?
var long:CLLocationDegrees?
func wazeMaps()
{
let openUrl = URL(string: "waze://?ll=\(String(describing: lat!)),\(String(describing: long!))&navigate=yes")!
UIApplication.shared.open(openUrl , options:[:]) { (success) in
if !success
{
}
}
}
}
replace url with if you wanna use google maps
如果您想使用谷歌地图,请将 url 替换为
let openUrl = URL(string: "comgooglemaps://?saddr=&daddr=\(String(describing: lat!)),\(String(describing: long!))&directionsmode=driving")!
回答by Mohammad Mirzakhani
In Swift 4 you can use:
在 Swift 4 中,您可以使用:
if let url = URL(string: "\(myUrl)") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}