我有一个应用程序,当点击一个uibutton时,我想打开另一个已安装的应用程序(即Waze)。我怎么能这样做?非常感谢。
答案 0 :(得分:13)
试试这个。例如,您想要打开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/")!)
}
答案 1 :(得分:5)
在SecondApp中
转到SecondApp的plist文件,你需要添加一个带有字符串iOSDevTips的URL Schemes(当然你可以写另一个字符串。这取决于你)。
2。在FirstApp
使用以下操作创建一个按钮:
- (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];
}
}
就是这样。现在,当您可以单击FirstApp中的按钮时,它应该打开SecondApp。
有关详细信息,请参阅here
答案 2 :(得分:2)
您可以查看Waze Community以供参考。
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代码段:
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")!)
}
答案 3 :(得分:1)
在swift4 waze中
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
{
}
}
}
}
如果您想使用谷歌地图
,请更换网址 let openUrl = URL(string: "comgooglemaps://?saddr=&daddr=\(String(describing: lat!)),\(String(describing: long!))&directionsmode=driving")!
答案 4 :(得分:0)
在Swift 4中,您可以使用:
if let url = URL(string: "\(myUrl)") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
答案 5 :(得分:0)
对于 Swift 4 / 5 / ...,你可以像这样正确地做:
if let url = URL(string: url), UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
} else {
// display error ?
}