我正在尝试让YouTube在我的iOS应用程序中运行。我已经成功检索了特定用户的视频列表,但是我正在努力实现didSelectRowAtIndexPath,以便将所选视频嵌入到单独的viewController中。这是我遇到麻烦的代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailController"];
GDataEntryBase *entry2 = [[feed entries] objectAtIndex:indexPath.row];
videoArray = [[(GDataEntryYouTubeVideo *)entry2 mediaGroup] mediaContents];
NSString *tempUrl = [videoArray objectAtIndex:0];
NSLog(@"The URL is:%@",tempUrl);
detailController.videoString = tempUrl;
[self.navigationController pushViewController:detailController animated:YES];
}
每次运行此命令时,都会在“detailController.videoString = tempUrl;”处得到一个断点。
我可以通过NSLog看到tempUrl字符串填充了以下内容:
The URL is:GDataMediaContent 0x69c76a0: {url:https://www.youtube.com/v/s36krFdPmYQ?version=3&f=user_uploads&app=youtube_gdata type:application/x-shockwave-flash medium:video isDefault:true expression:full duration:1913}
如何仅从GData API中提取视频网址?
预先感谢您的回复。
我进行了以下更改,现在获得了正确的URL字符串:
GDataEntryBase *entry2 = [[feed entries] objectAtIndex:indexPath.row];
NSArray *mediaContents = [[(GDataEntryYouTubeVideo *)entry2 mediaGroup] mediaContents];
GDataMediaContent *flashContent = [GDataUtilities firstObjectFromArray:mediaContents withValue:@"application/x-shockwave-flash" forKeyPath:@"type"];
NSLog(@"The URL is:%@",[flashContent URLString]);
detailController.videoString = [flashContent URLString];
这给了我来自NSLog的以下信息:
The URL is:https://www.youtube.com/v/s36krFdPmYQ?version=3&f=user_uploads&app=youtube_gdata
但是我仍然在'detailController.videoString = [flashContent URLString];
最佳答案
我能够解决这个问题。我在两个ViewController之间添加了Modal segue,并且还稍微修改了我的代码。这是添加segue后最终使用的代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailController"];
GDataEntryBase *entry2 = [[feed entries] objectAtIndex:indexPath.row];
NSString *title = [[entry2 title] stringValue];
NSArray *contents = [[(GDataEntryYouTubeVideo *)entry2 mediaGroup] mediaContents];
GDataMediaContent *flashContent = [GDataUtilities firstObjectFromArray:contents withValue:@"application/x-shockwave-flash" forKeyPath:@"type"];
NSString *tempURL = [flashContent URLString];
detailController.videoString = tempURL;
detailController.titleString = title;
[self.navigationController pushViewController:detailController animated:YES];
}
您可以在此处查看完整的YouTubeDemo应用程序代码:
https://gist.github.com/2501684