The*_*666 1 iphone sdk objective-c ios
所以我使用下面的代码检查字符串是否以"http://"开头,然后我想添加"http://"以便我可以打开页面UIWebView.
NSString *firstString = [NSString stringWithFormat:@"%@", URL.text];
NSString *check = [NSString stringWithFormat:@"%@", @"http://"];
if (firstString != check) {
NSString *newString = [NSString stringWithFormat:@"http://%@", URL.text];
newString = [newString substringWithRange:NSMakeRange(7, newString.length - 7)];
URL.text = newString;
}
[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:URL.text]]];`
Run Code Online (Sandbox Code Playgroud)
由于某种原因,这不起作用.你知道为什么吗?
有人在我写作时发帖,但不管怎样,这是一个答案:
你这太难了.您只需使用hasPrefix来检查"http".作为一个例子,我将它用于我的统一搜索/ url栏.
- (IBAction)go:(id)sender {
NSString *inputString = [searchField stringValue];
NSString *outputString = [inputString stringByReplacingOccurrencesOfString:@" " withString:@"+"];
if ([inputString hasPrefix:@"http://"]) {
//Has Prefix
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:inputString]]];
}
else
{
//Does not have prefix. Do what you want here. I google it.
NSString *googleString = @"http://google.com/search?q=";
NSString *searchString = [NSString stringWithFormat:@"%@%@", googleString, outputString];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:searchString]]];
}
}
Run Code Online (Sandbox Code Playgroud)
这是谷歌搜索,你可以继续使用 NSString *newString = [NSString stringWithFormat:@"http://%@", URL.text];
如果您愿意,还可以添加一些检查.祝好运!
| 归档时间: |
|
| 查看次数: |
2583 次 |
| 最近记录: |