2012-11-07 170 views

回答

4

將YouTube視頻加載到webView後,我創建了一個按鈕,用戶可以點擊該按鈕將URL複製到同一頁面上的文本字段。訣竅是獲取頁面的innerHTML,然後查找YouTube ID。通過這個ID,我可以將它放到完整的網址中,以便用戶可以保存鏈接並在以後播放視頻,而無需在YouTube網站上再次查看該視頻。 (順便說一句,我有我自己的displayAlert,是以給定的消息,並顯示給用戶時,有沒有在UIWebView中加載視頻或有裝載許多視頻)這裏是代碼:

-(IBAction)copyURLAction { 
    if (webView) { 
     NSString* htmlString = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML"]; 
     if (htmlString) { 
      NSString *youTubeID = @"data-youtube-id=\""; 
      int position = [htmlString rangeOfString:youTubeID].location + [youTubeID length]; 
      if (position > 1) { 
       htmlString = [htmlString substringFromIndex:position]; 
       position = [htmlString rangeOfString:@"\""].location; 
       htmlString = [htmlString substringToIndex:position]; 
       NSString* tempPrefix = @"http://m.youtube.com/watch?v="; 
       NSString *youTubeString = [tempPrefix stringByAppendingString:htmlString]; 
       textFieldView.text = youTubeString; 
      } else { 
       [[SharedObjectsSingleton sharedInstance] displayAlert:@"Can't Copy URL" :@"Can only copy the URL when a single video is being displayed."]; 
      } 
     } else { 
      [[SharedObjectsSingleton sharedInstance] displayAlert:@"Can't get URL" :@"Nothing to copy."]; 
     } 
    } 
} 
+1

此代碼替換 @"data-youtube-id=\""不起作用任何more..i認爲他們已經改變了HTML –

1

的上面使用的代碼的工作,但我認爲YouTube的爲了改劇本所以現在爲它工作,你需要用@"i.ytimg.com/vi/"