How can I launch Safari from an iPhone app?

2019-01-03 23:25发布

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

7条回答
做自己的国王
2楼-- · 2019-01-03 23:49

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

import UIKit

class InterAppCommunication {
    static func openURI(_ URI: String) {
        UIApplication.shared.open(URL(string: URI)!, options: [:], completionHandler: { (succ: Bool) in print("Complete! Success? \(succ)") })
    }
}
查看更多
神经病院院长
3楼-- · 2019-01-03 23:50

you can open the url in safari with this:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.google.com"]];
查看更多
看我几分像从前
4楼-- · 2019-01-03 23:55

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

ObjectiveC:

NSDictionary *options = [[NSDictionary alloc] init];
//options can be empty
NSURL *url = [NSURL URLWithString:@"http://www.stackoverflow.com"];
[[UIApplication sharedApplication] openURL:url options:options completionHandler:^(BOOL success){
}];
查看更多
Explosion°爆炸
5楼-- · 2019-01-03 23:56

should be the following :

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

if (![[UIApplication sharedApplication] openURL:url]) {
    NSLog(@"%@%@",@"Failed to open url:",[url description]);
}
查看更多
成全新的幸福
6楼-- · 2019-01-03 23:57

Maybe someone can use the Swift version:

In swift 2.2:

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

And 3.0:

UIApplication.shared().openURL(URL(string: "https://www.google.com")!)
查看更多
一夜七次
7楼-- · 2019-01-04 00:08

UIApplication has a method called openURL:

example:

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

if (![[UIApplication sharedApplication] openURL:url]) {
  NSLog(@"%@%@",@"Failed to open url:",[url description]);
}
查看更多
登录 后发表回答