How to get the default iOS browser name?

坚强是说给别人听的谎言 提交于 2020-01-02 23:21:00

问题


The Stack Overflow app detects the name of my jailbrokenly-set default browser (Chrome). How can I achieve the same thing in objective-c and swift?

(Just the name, not the ActivityView code)

Example:


Update: I went into Settings > Stack Exchange and found this:

It looks like the app defaults to Safari, but iff Chrome is installed then links will be sent to that browser. Chrome is most likely detected by the canOpenUrl method described in the answer below.


回答1:


I suspect that the Stack Exchange app isn't checking for your default browser specifically. After all, since Apple doesn't provide an easy way to change your default browser, I doubt the SDK provides an API to detect the default browser.

Instead, the Stack Exchange app may use the canOpenURL(_:) method on UIApplication to test if a predetermined set of common browser apps are installed. For each browser that is installed, the action sheet gets a new button. That approach could resemble the following code snippet.

let safariURL = NSURL(string: "http://stackoverflow.com")!
let chromeURL = NSURL(string: "googlechrome://stackoverflow.com")!
let operaURL  = NSURL(string: "opera-http://stackoverflow.com")!

let sharedApplication = UIApplication.sharedApplication() // convenience

if sharedApplication.canOpenURL(safariURL) {
    // add "Safari" button to action sheet
}

if sharedApplication.canOpenURL(chromeURL) {
    // add "Chrome" button to action sheet
}

if sharedApplication.canOpenURL(operaURL) {
    // add "Opera" button to action sheet
}

// display action sheet


来源:https://stackoverflow.com/questions/31365889/how-to-get-the-default-ios-browser-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!