无法在UIWebview中打开重定向的URL

时间:2014-04-22 13:12:58

标签: ios uiwebview nsurl

我正在使用google RSS feed,我正在获取一些网址:

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无法打开该页面,因为发生了太多重定向

请建议。

1 个答案:

答案 0 :(得分:0)

可能是您的URL无效,请查看以下代码:

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.");