为什么iOS UIWebView视频无法发送cookie?

时间:2014-11-21 10:47:23

标签: javascript ios iphone xcode ipad

我有一个UIWebView,并从我的网站加载一个html页面:

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [webView loadRequest:nsUrlRequest];
}

然后我想从webview加载一些图像/视频:

- (void)webViewDidFinishLoad:(UIWebView *)myView
{
        // this line I got video url
        NSString* videoUrl = XXXXX;

         // cookie is all right in the page
         [myView stringByEvaluatingJavaScriptFromString:@"alert(document.cookie)"];

        // image load request is correct with cookie
        [myView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.createElement('img').src='/Images/logo.png'"]];

        // but video request no cookie !!!!!
        [myView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var video = document.createElement('video');video.src = '%@';video.autoPlay = true;", videoUrl]];

}

请帮帮我!!救救我!!!

1 个答案:

答案 0 :(得分:0)

确保您已在iPhone中为webview启用了Cookie 尝试使用:

[NSHTTPCookieStorage sharedHTTPCookieStorage].cookieAcceptPolicy = 
    NSHTTPCookieAcceptPolicyAlways;

要发送cookie,请查看此堆栈链接:https://stackoverflow.com/a/21253352/2098690