0

I use this javascript in a UIWebvView to get the URL of a YouTube video.

getURL = [self.webView stringByEvaluatingJavaScriptFromString:@"function getURL() {
    var player = document.getElementById('player'); 
    var video = player.getElementsByTagName('video')[0]; 
    return video.getAttribute('src');
    } 
    getURL();"
];

It works fine on iPhone, but fails on ipad and returns @"". Both devices load the m.youtube.com website. Could anybody help me?

jfriend00
  • 683,504
  • 96
  • 985
  • 979
JonasG
  • 9,274
  • 12
  • 59
  • 88

1 Answers1

0

The iPad and the iPhone have different HTTP header information, specifically the user agent string. It is possible that YouTube is sending down different javascript depending on which device the user has.

You might try overriding the "user agent" HTTP Header in your request. I think this Stack Overflow answer may work for that: Change user agent on UIWebView

If not, you might try using a set of third party classes like AISHTTPRequest to spoof the user agent information to access the youtube URL from the iPad while making YouTube think it is an iPhone.

Community
  • 1
  • 1
Hivebrain
  • 784
  • 1
  • 8
  • 11
  • I'm already using ASIHTTPRequest, could you give me more details on changing the user agent in ASIHTTPRequest? thanks – JonasG Dec 29 '11 at 00:07
  • Your answer is right, the websites have different header infos. There was no way to make the javascript work on iPad. I had to use a completely different method. – JonasG Jan 09 '12 at 23:03
  • That's too bad. Sorry for the lack of response to your last question. I am pretty sure you just create a new request and then set the user agent. A quick google serach for "ASIHTTPRequest setting user agent" turns up this page at the top result: http://allseeing-i.com/ASIHTTPRequest/How-to-use Looks like you just need to call [request setUserAgent:@"MyApp 1.0"] and replace the "MyApp 1.0" with whatever the agent name is for the iPhone. You should get youtube's iPhone response then. What method did you use? If you post details here other people might benefit from how you solved the problem. – Hivebrain Jan 10 '12 at 20:23