【问题标题】:iOS UIWebView or WKWebView URL redirectiOS UIWebView 或 WKWebView URL 重定向
【发布时间】:2017-03-29 12:36:47
【问题描述】:

我有一个 URL:http://listen.streamonomy.com/cooljazzflorida(例如)。这是我在 PLS 文件(由 SHOUTcast 收音机提供)中解析的用于流式音频的 URL。当我从浏览器(Safari 或 Google Chrome)打开它时,此 URL 会重定向到 http://streaming.shoutcast.com/cooljazzflorida?lang=fr-fr,我可以播放该流。

但是,在我的 iOS 应用程序中,使用 UIWebView 或 WKWebView,重定向的 URL 是 http://streaming.shoutcast.com/cooljazzflorida_64?lang=fr-fr(然后是 _64),与浏览器不同,并且出现错误“找不到您需要的文件”。

有什么想法吗?谢谢。

编辑:说明问题的示例代码:

https://github.com/iDevelopper/RedirectURLSample.git

#import "ViewController.h"

@interface ViewController () <UIWebViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
    webView.delegate = self;
    [self.view addSubview:webView];

    NSString *urlString = @"http://listen.streamonomy.com/cooljazzflorida";
    /* If you enter this url using Safari browser, the redirected url is:
     http://streaming.shoutcast.com/cooljazzflorida?lang=fr-fr
     */
    NSURL *urlToPlay = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:urlToPlay];
    [webView loadRequest:request];
}

#pragma mark - UIWebView Delegate

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSURL *url = [webView.request mainDocumentURL];
    NSLog(@"DID FINISH The Redirected URL is: %@", url);
    /*
     The redirected url is: (not the same returned by the browser and this url is not found)
     http://streaming.shoutcast.com/cooljazzflorida_64?lang=en-us
    */


    // Set AVPlayerItem with the redirected URL
    // ...
}

@end

【问题讨论】:

  • 使用AVPlayerViewController 代替UIWebview 流式传输音频/视频
  • 我不使用 UIWebView 播放音频,但使用 AVPlayer 并且效果很好。我只是使用 UIWebViewDelegate 方法来知道要播放的重定向 URL 并设置 AVPlayerItem url。
  • 那么请用您的代码更新您的问题
  • 用示例代码编辑,谢谢!

标签: ios uiwebview


【解决方案1】:

_64 地址确实不存在,Streamonomy 的错还没有注意到它并修复了它们的重定向逻辑。不同的重定向目标基于您请求中的 User-Agent 标头。我怀疑 Streamonomy 试图猜测请求设备并查询不同的比特率。我给你找了两个例子:

Mozilla/5.0 (iPad; CPU OS 10_2 like Mac OS X) AppleWebKit/602.3.12 (KHTML, like Gecko) Version/10.0 Mobile/14C89 Safari/602.1

重定向到现有网址

Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1

重定向到不存在的_64

弄清楚确切的阈值是多少 :-) 同样值得注意的是,设置一个完全虚假的 UA,如 ignoremeidontneedyourhelp 会重定向到“正确的”(现有)URL。但如果您打算将该 UI/WKWebView 用于正常的公共 Web 浏览,这不是一个有用的策略。网站可能会被奇怪地呈现或以其他方式损坏。

【讨论】:

  • 谢谢,我会尝试使用: - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)redirectResponse newRequest:(NSURLRequest *)request completionHandler: (void (^)(NSURLRequest *))completionHandler { NSURLRequest *newRequest = request; // 播放 // ... completionHandler(nil);但不知道当app在后台时是否有效
  • 我以为你想修复 UIWebView/WKWebView 给你错误的重定向。现在你会采取错误的重定向,尝试理解它并“纠正”它吗?当 Streamonomy 更改其重定向方案时(将重定向到您的“更正”黑客所期望的其他 URL),这将中断。您应该通过更改 User-Agent 来让 Streamonomy 相信您的浏览器是桌面浏览器。直接从服务器获取正确的重定向,防止不必要的黑客攻击。
猜你喜欢
  • 2018-01-18
  • 2017-11-25
  • 1970-01-01
  • 1970-01-01
  • 2019-03-12
  • 2016-03-12
  • 1970-01-01
  • 2010-10-18
  • 1970-01-01
相关资源
最近更新 更多