【问题标题】:Unable to open redirected URL in UIWebview无法在 UIWebview 中打开重定向的 URL
【发布时间】:2014-04-22 13:12:58
【问题描述】:

我正在使用 google RSS 提要获取一些 URL:

https://www.google.com/url?q=http://www.mcphersonsentinel.com/article/20140422/SPORTS/140429875/10084/SPORTS&ct=ga&cd=CAIyAA&usg=AFQjCNG9pfoDddHHNV0gJaspzR1jv1ffKA

当我试图在 uiwebview 中打开它时,它永远不会加载页面。

如果我在 Safari 中打开它,它会说 Safari 无法打开页面,因为发生了太多重定向

请建议。

【问题讨论】:

  • 我可以在 Firfox 中打开相同的 url,但不能在 Chrome 中打开。完全奇怪!

标签: ios uiwebview nsurl


【解决方案1】:

可能是您的网址无效,请使用以下代码进行检查:

NSString *strURL = @"https://www.google.com/url?q=http://www.mcphersonsentinel.com/article/20140422/SPORTS/140429875/10084/SPORTS&ct=ga&cd=CAIyAA&usg=AFQjCNG9pfoDddHHNV0gJaspzR1jv1ffKA";    
NSString *urlRegEx =
@"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
NSPredicate *urlPredic = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
BOOL isValidURL = [urlPredic evaluateWithObject:strURL];

if(isValidURL)
{
    strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *websiteUrl = [NSURL URLWithString:strURL];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
    [self.webview loadRequest:urlRequest];
}
else
    NSLog(@"URL IS NOT VALID.");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多