gpt4 book ai didi

ios - 添加 http ://to NSURL if it's not there

转载 作者:可可西里 更新时间:2023-11-01 03:06:53 25 4
gpt4 key购买 nike

我在我的应用程序中使用 WebView ,从文本字段获取 URL。如果字符串以“http://”开头,它会起作用。我正在尝试修改代码,使其也能处理用户不输入“http://”或“https://”的情况

如何检查 URL 中是否没有“http://”?如何修改 URL 以在其中添加“http://”?

NSString *URLString = textField.text;
NSURL *URL = [NSURL URLWithString:URLString];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[self.webView loadRequest:request];

最佳答案

NSString *urlString = @"google.com";
NSURL *webpageUrl;

if ([urlString hasPrefix:@"http://"] || [urlString hasPrefix:@"https://"]) {
webpageUrl = [NSURL URLWithString:urlString];
} else {
webpageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", urlString]];
}

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:webpageUrl];
[self.myWebView loadRequest:urlRequest];

关于ios - 添加 http ://to NSURL if it's not there,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31883976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com