简体   繁体   English

iOS UIWebView或WKWebView URL重定向

[英]iOS UIWebView or WKWebView URL redirect

I have an URL: http://listen.streamonomy.com/cooljazzflorida (for example). 我有一个网址: http : //listen.streamonomy.com/cooljazzflorida (例如)。 This is an URL I parse in a PLS file (provided by SHOUTcast radios) for streaming audio. 这是我在PLS文件(由SHOUTcast广播提供)中解析的URL,用于流音频。 When I open it from a browser (Safari or Google Chrome), this URL is redirected to http://streaming.shoutcast.com/cooljazzflorida?lang=fr-fr and I can play the stream. 当我从浏览器(Safari或Google Chrome)打开它时,此URL被重定向到http://streaming.shoutcast.com/cooljazzflorida?lang=fr-fr ,我可以播放该流。

However, in my iOS application, using UIWebView or WKWebView, the redirected URL is http://streaming.shoutcast.com/cooljazzflorida_64?lang=fr-fr (then with _64), not the same from the browser, and I have the error "The file you require cannot be found". 但是,在我的iOS应用程序中,使用UIWebView或WKWebView,重定向的URL为http://streaming.shoutcast.com/cooljazzflorida_64?lang=fr-fr (然后为_64),与浏览器的URL不同,我拥有错误“找不到所需的文件”。

Any ideas please ? 有什么想法吗? Thanks. 谢谢。

EDIT: A sample code to illustrate the problem: 编辑:示例代码来说明问题:

https://github.com/iDevelopper/RedirectURLSample.git 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

The _64 address really does not exist and it's Streamonomy's fault to not noticing it yet and fixing their redirection logic. _64地址确实不存在,Streamonomy的错是尚未注意到它并修复其重定向逻辑。 The different redirection target is based on User-Agent header in your request. 不同的重定向目标基于请求中的User-Agent标头。 I suspect that Streamonomy is trying to guess the requesting device and query different bitrates. 我怀疑Streamonomy正在尝试猜测请求的设备并查询不同的比特率。 I found you two examples: 我发现了两个例子:

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

redirects to existing URL 重定向到现有的URL

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

redirects to the nonexistent _64 重定向到不存在的_64

Go figure what is the exact threshold :-) Also noticeably, setting a totally bogus UA like ignoremeidontneedyourhelp redirects to the "correct" (existing) URL. 弄清楚确切的阈值是什么:-)同样值得注意的是,设置一个完全虚假的UA,例如ignoremeidontneedyourhelp重定向到“正确的”(现有)URL。 But it's not an useful strategy if you intend to use that UI/WKWebView for normal public web browsing. 但是,如果您打算使用该UI / WKWebView进行常规的公共Web浏览,则这不是有用的策略。 Websites could be strangely rendered or otherwise broken. 网站可能会被奇怪地渲染或以其他方式破坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM