本文介绍了如何从 iOS 应用程序启动 Safari 并打开 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设置页面上,我想包含三个链接

  • 我的应用支持网站
  • YouTube 应用教程
  • 我的主要网站(即:链接到由 Dale Dietrich 创建"标签.)

我搜索了这个网站、网络和我的文档,但没有发现任何明显的东西.

注意:我不想在我的应用中打开网页.我只想将链接发送到 Safari,然后在那里打开该链接.我已经看到许多应用在其设置"页面中做同样的事情,所以这一定是可能的.

解决方案

这是我所做的:

  1. 我在头文件 .h 中创建了一个 IBAction,如下所示:

     - (IBAction)openDaleDietrichDotCom:(id)sender;

  2. 我在设置"页面上添加了一个 UIButton,其中包含我想要链接到的文本.

  3. 我将按钮适当地连接到文件所有者中的 IBAction.

  4. 然后执行以下操作:

目标 C

- (IBAction)openDaleDietrichDotCom:(id)sender {[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.daledietrich.com"]];}

迅捷

(viewController 中的 IBAction,而不是头文件)

if let link = URL(string: "https://yoursite.com") {UIApplication.shared.open(link)}

注意我们不需要转义字符串和/或地址,例如:

让 myNormalString = "https://example.com";让 myEscapedString = myNormalString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!

实际上,转义可能会导致打开失败.

On the settings page, I want to include three links to

  • My app support site
  • YouTube app tutorial
  • My primary site (ie: linked to a 'Created by Dale Dietrich' label.)

I've searched this site and the web and my documentation and I've found nothing that is obvious.

NOTE: I don't want to open web pages within my app. I just want to send the link to Safari and that link be open there. I've seen a number of apps doing the same thing in their Settings page, so it must be possible.

解决方案

Here's what I did:

  1. I created an IBAction in the header .h files as follows:

     - (IBAction)openDaleDietrichDotCom:(id)sender;
    

  2. I added a UIButton on the Settings page containing the text that I want to link to.

  3. I connected the button to IBAction in File Owner appropriately.

  4. Then implement the following:

Objective-C

- (IBAction)openDaleDietrichDotCom:(id)sender {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.daledietrich.com"]];
}

Swift

(IBAction in viewController, rather than header file)

if let link = URL(string: "https://yoursite.com") {
  UIApplication.shared.open(link)
}

这篇关于如何从 iOS 应用程序启动 Safari 并打开 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 23:47