如何获取默认的iOS浏览器名称? [英] How to get the default iOS browser name?

查看:344
本文介绍了如何获取默认的iOS浏览器名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Stack Overflow应用程序检测我越狱设置的默认浏览器(Chrome)的名称。如何在objective-c和swift中实现同样的目标?

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?

(只是名称,而不是ActivityView代码)

(Just the name, not the ActivityView code)

示例:

更新:我进入了设置>堆栈交换,发现了这个:

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

看起来该应用默认为Safari,但 iff Chrome已安装,然后链接将发送到该浏览器。 Chrome最有可能通过以下答案中描述的 canOpenUrl 方法检测到。

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.

推荐答案

我怀疑Stack Exchange应用程序没有专门检查您的默认浏览器。毕竟,由于Apple没有提供更改默认浏览器的简便方法,我怀疑SDK提供了一个API来检测默认浏览器。

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.

相反,堆栈Exchange应用程序可以使用 UIApplication 上的 canOpenURL(_:)方法来测试一组预定的常见浏览器应用是否是安装。对于安装的每个浏览器,操作表都会获得一个新按钮。该方法可能类似于以下代码段。

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

这篇关于如何获取默认的iOS浏览器名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆