Guest User

Untitled

a guest
Oct 4th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. //
  2. // MasterViewController.m
  3. // youtubedemo
  4. //
  5. // Created by Your Name on 4/24/12.
  6. // Copyright (c) 2012 Your Company. All rights reserved.
  7. //
  8.  
  9. #import "MasterViewController.h"
  10. #import "DetailViewController.h"
  11.  
  12.  
  13. @interface MasterViewController (PrivateMethods)
  14. - (GDataServiceGoogleYouTube *)youTubeService;
  15. @end
  16.  
  17. @implementation MasterViewController
  18. @synthesize feed;
  19.  
  20.  
  21. - (void)awakeFromNib
  22. {
  23. [super awakeFromNib];
  24. }
  25.  
  26. - (void)viewDidLoad
  27. {
  28. NSLog(@"Loading");
  29.  
  30. GDataServiceGoogleYouTube *service = [self youTubeService];
  31.  
  32. NSString *uploadsID = kGDataYouTubeUserFeedIDUploads;
  33. NSURL *feedURL = [GDataServiceGoogleYouTube youTubeURLForUserID:@"JMEveryday"
  34. userFeedID:uploadsID];
  35.  
  36. [service fetchFeedWithURL:feedURL
  37. delegate:self
  38. didFinishSelector:@selector(request:finishedWithFeed:error:)];
  39.  
  40. [super viewDidLoad];
  41. // Do any additional setup after loading the view, typically from a nib.
  42.  
  43. }
  44.  
  45. - (void)request:(GDataServiceTicket *)ticket
  46. finishedWithFeed:(GDataFeedBase *)aFeed
  47. error:(NSError *)error {
  48.  
  49. self.feed = (GDataFeedYouTubeVideo *)aFeed;
  50.  
  51. [self.tableView reloadData];
  52. }
  53.  
  54. - (void)viewDidUnload
  55. {
  56. [super viewDidUnload];
  57. // Release any retained subviews of the main view.
  58. }
  59.  
  60. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  61. {
  62. return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  63. }
  64.  
  65.  
  66. #pragma mark - Table View
  67.  
  68. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  69. {
  70. return 1;
  71. }
  72.  
  73. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  74. {
  75. return [[feed entries] count];
  76. }
  77.  
  78. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  79. return 70.0f;
  80. }
  81.  
  82. /*- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  83. {
  84. static NSString *CellIdentifier = @"Cell";
  85.  
  86. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  87. if (cell == nil) {
  88. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  89. }
  90.  
  91. //Configure the cell
  92. GDataEntryBase *entry = [[feed entries] objectAtIndex:indexPath.row];
  93. NSString *title = [[entry title] stringValue];
  94. NSArray *thumbnails = [[(GDataEntryYouTubeVideo *)entry mediaGroup] mediaThumbnails];
  95.  
  96. cell.textLabel.text = title;
  97.  
  98. NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[thumbnails objectAtIndex:0] URLString]]];
  99. cell.imageView.image = [UIImage imageWithData:data];
  100.  
  101. return cell;
  102. }
  103. */
  104.  
  105. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  106. {
  107. static NSString *CellIdentifier = @"Cell";
  108. UITableViewCell *cell = nil;
  109.  
  110. cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  111. if (cell == nil)
  112. {
  113. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  114. }
  115.  
  116. // Configure the cell.
  117. GDataEntryBase *entry = [[feed entries] objectAtIndex:indexPath.row];
  118. NSString *title = [[entry title] stringValue];
  119. NSArray *thumbnails = [[(GDataEntryYouTubeVideo *)entry mediaGroup] mediaThumbnails];
  120.  
  121. cell.textLabel.text = title;
  122. // Load the image with an GCD block executed in another thread
  123. dispatch_queue_t downloadQueue = dispatch_queue_create("image downloader", NULL);
  124. dispatch_async(downloadQueue, ^{
  125. NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[thumbnails objectAtIndex:0] URLString]]];
  126. UIImage * image = [UIImage imageWithData:data];
  127.  
  128. dispatch_async(dispatch_get_main_queue(), ^{
  129. cell.imageView.image = image;
  130. cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
  131. [cell setNeedsLayout];
  132. });
  133. });
  134. dispatch_release(downloadQueue);
  135.  
  136. return cell;
  137. }
  138.  
  139. - (GDataServiceGoogleYouTube *)youTubeService {
  140. static GDataServiceGoogleYouTube* _service = nil;
  141.  
  142. if (!_service) {
  143. _service = [[GDataServiceGoogleYouTube alloc] init];
  144.  
  145. [_service setUserAgent:@"AppWhirl-UserApp-1.0"];
  146. //[_service setShouldCacheDatedData:YES];
  147. [_service setServiceShouldFollowNextLinks:NO];
  148. }
  149.  
  150. // fetch unauthenticated
  151. [_service setUserCredentialsWithUsername:nil
  152. password:nil];
  153.  
  154. return _service;
  155. }
  156. /*
  157. // Override to support rearranging the table view.
  158. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
  159. {
  160. }
  161. */
  162.  
  163. /*
  164. // Override to support conditional rearranging of the table view.
  165. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
  166. {
  167. // Return NO if you do not want the item to be re-orderable.
  168. return YES;
  169. }
  170. */
  171.  
  172. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  173. DetailViewController *detailController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailController"];
  174.  
  175.  
  176. GDataEntryBase *entry2 = [[feed entries] objectAtIndex:indexPath.row];
  177. NSString *title = [[entry2 title] stringValue];
  178. NSArray *contents = [[(GDataEntryYouTubeVideo *)entry2 mediaGroup] mediaContents];
  179. GDataMediaContent *flashContent = [GDataUtilities firstObjectFromArray:contents withValue:@"application/x-shockwave-flash" forKeyPath:@"type"];
  180. NSString *tempURL = [flashContent URLString];
  181. //NSLog(@"The URL is:%@",tempURL);
  182. //NSString *test = @"This is a test";
  183. detailController.videoString = tempURL;
  184. detailController.titleString = title;
  185.  
  186. // [self embedYouTube:[flashContent URLString] frame:CGRectMake(70, 100, 200, 200)];
  187. [self.navigationController pushViewController:detailController animated:YES];
  188.  
  189. }
  190.  
  191. /*- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame {
  192. NSString *embedHTML = @"<html><head> <meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 200\"/></head> <body style=\"background:#F00;margin-top:0px;margin-left:0px\"> <div><object width=\"200\" height=\"200\"> <param name=\"movie\" value=\"%@\"></param> <param name=\"wmode\" value=\"transparent\"></param> <embed src=\"%@\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"200\" height=\"200\"></embed> </object></div></body></html>";
  193.  
  194. NSString *html =[NSString stringWithFormat:embedHTML, urlString, urlString, frame.size.width, frame.size.height];
  195. [webView loadHTMLString:html baseURL:nil];
  196. } */
  197.  
  198. @end
Add Comment
Please, Sign In to add comment