如何从iPhone应用程序启动Safari ?

时间:2021-08-18 07:31:30

This might be a rather obvious question, but can you launch the Safari browser from an iPhone app?

这可能是一个相当明显的问题,但你能从iPhone应用程序中启动Safari浏览器吗?

7 个解决方案

#1


201  

should be the following :

应如下:

NSURL *url = [NSURL URLWithString:@"http://www.*.com"];

if (![[UIApplication sharedApplication] openURL:url]) {
    NSLog(@"%@%@",@"Failed to open url:",[url description]);
}

#2


53  

UIApplication has a method called openURL:

UIApplication有一个方法叫做openURL:

example:

例子:

NSURL *url = [NSURL URLWithString:@"http://www.*.com"];

if (![[UIApplication sharedApplication] openURL:url]) {
  NSLog(@"%@%@",@"Failed to open url:",[url description]);
}

#3


16  

you can open the url in safari with this:

可以在safari中打开url:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.google.com"]];

#4


2  

Maybe someone can use the Swift version:

也许有人可以使用Swift版本:

In swift 2.2:

在斯威夫特2.2:

UIApplication.sharedApplication().openURL(NSURL(string: "https://www.google.com")!)

And 3.0:

和3.0:

UIApplication.shared().openURL(URL(string: "https://www.google.com")!)

#5


2  

With iOS 10 we have one different method with completion handler:

在iOS 10中,我们有一种不同的完成处理器方法:

ObjectiveC:

ObjectiveC:

NSDictionary *options = [[NSDictionary alloc] init];
//options can be empty
NSURL *url = [NSURL URLWithString:@"http://www.*.com"];
[[UIApplication sharedApplication] openURL:url options:options completionHandler:^(BOOL success){
}];

#6


1  

In Swift 3.0, you can use this class to help you communicate with. The framework maintainers have deprecated or removed previous answers.

在Swift 3.0中,您可以使用这个类来帮助您进行通信。框架维护人员已经弃用或删除了以前的答案。

import UIKit

class InterAppCommunication {
    static func openURI(_ URI: String) {
        UIApplication.shared.open(URL(string: URI)!, options: [:], completionHandler: { (succ: Bool) in print("Complete! Success? \(succ)") })
    }
}

#7


0  

In swift 4, as OpenURL is depreciated, an easy way of doing it would be just

在swift 4中,当OpenURL被贬值时,一种简单的方法是公正的

if let url = URL(string: "https://*.com") {
UIApplication.shared.open(url, options: [:]) }

#1


201  

should be the following :

应如下:

NSURL *url = [NSURL URLWithString:@"http://www.*.com"];

if (![[UIApplication sharedApplication] openURL:url]) {
    NSLog(@"%@%@",@"Failed to open url:",[url description]);
}

#2


53  

UIApplication has a method called openURL:

UIApplication有一个方法叫做openURL:

example:

例子:

NSURL *url = [NSURL URLWithString:@"http://www.*.com"];

if (![[UIApplication sharedApplication] openURL:url]) {
  NSLog(@"%@%@",@"Failed to open url:",[url description]);
}

#3


16  

you can open the url in safari with this:

可以在safari中打开url:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.google.com"]];

#4


2  

Maybe someone can use the Swift version:

也许有人可以使用Swift版本:

In swift 2.2:

在斯威夫特2.2:

UIApplication.sharedApplication().openURL(NSURL(string: "https://www.google.com")!)

And 3.0:

和3.0:

UIApplication.shared().openURL(URL(string: "https://www.google.com")!)

#5


2  

With iOS 10 we have one different method with completion handler:

在iOS 10中,我们有一种不同的完成处理器方法:

ObjectiveC:

ObjectiveC:

NSDictionary *options = [[NSDictionary alloc] init];
//options can be empty
NSURL *url = [NSURL URLWithString:@"http://www.*.com"];
[[UIApplication sharedApplication] openURL:url options:options completionHandler:^(BOOL success){
}];

#6


1  

In Swift 3.0, you can use this class to help you communicate with. The framework maintainers have deprecated or removed previous answers.

在Swift 3.0中,您可以使用这个类来帮助您进行通信。框架维护人员已经弃用或删除了以前的答案。

import UIKit

class InterAppCommunication {
    static func openURI(_ URI: String) {
        UIApplication.shared.open(URL(string: URI)!, options: [:], completionHandler: { (succ: Bool) in print("Complete! Success? \(succ)") })
    }
}

#7


0  

In swift 4, as OpenURL is depreciated, an easy way of doing it would be just

在swift 4中,当OpenURL被贬值时,一种简单的方法是公正的

if let url = URL(string: "https://*.com") {
UIApplication.shared.open(url, options: [:]) }