如何在 iOS 中不指定 HTTP 或 HTTPS 的情况下打开 URL? [英] How can I open a URL without specifying HTTP or HTTPS in iOS?

查看:47
本文介绍了如何在 iOS 中不指定 HTTP 或 HTTPS 的情况下打开 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 iOS 应用中,我使用以下代码打开链接:

In my iOS app I am opening links using the code below:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",myurl]]];

链接是动态的,不提供 HTTP/HTTPS.

The links come dynamically and HTTP/HTTPS is not provided.

我无法对 HTTP 或 HTTPS 进行硬编码,因为我不知道该 URL 是否包含 HTTP 或 HTTPS.如何在不指定 HTTP 或 HTTPS 的情况下打开 URL?

I cannot hardcode HTTP or HTTPS because I don't know if the URL has HTTP or HTTPS. How can I open a URL without specifying HTTP or HTTPS?

推荐答案

Swift 的单线解决方案:

One-liner solution for Swift:

let urlString = "example.com"
let validUrlString = dataText.hasPrefix("http") ? urlString : "http://\(urlString)"

这篇关于如何在 iOS 中不指定 HTTP 或 HTTPS 的情况下打开 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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