forked from jsonmodel/jsonmodel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYouTubeViewController.m
More file actions
96 lines (76 loc) · 2.57 KB
/
YouTubeViewController.m
File metadata and controls
96 lines (76 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// YouTubeViewController.m
// JSONModelDemo
//
// Created by Marin Todorov on 02/12/2012.
// Copyright (c) 2012 Underplot ltd. All rights reserved.
//
#import "YouTubeViewController.h"
#import "VideoModel.h"
#import "HUD.h"
@interface YouTubeViewController ()
{
IBOutlet UITableView *table;
NSArray* items;
}
@end
@implementation YouTubeViewController
-(void)viewDidAppear:(BOOL)animated
{
self.title = @"Youtube video search";
[HUD showUIBlockingIndicatorWithText:@"Fetching JSON"];
//1
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//code executed in the background
//2
NSData* ytData = [NSData dataWithContentsOfURL:
[NSURL URLWithString:@"http://gdata.youtube.com/feeds/api/videos?q=pomplamoose&max-results=50&alt=json"]
];
//3
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:ytData
options:kNilOptions
error:nil];
//4
dispatch_async(dispatch_get_main_queue(), ^{
//code executed on the main queue
//5
items = [VideoModel arrayOfModelsFromDictionaries:
json[@"feed"][@"entry"]
];
[HUD hideUIBlockingIndicator];
if (items) {
[table reloadData];
} else {
[HUD showAlertWithTitle:@"Error" text:@"Sorry, invalid JSON data"];
}
});
});
}
#pragma mark - table methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return items.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KivaCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"KivaCell"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
VideoModel* video = items[indexPath.row];
cell.textLabel.text = video.title.$t;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[table deselectRowAtIndexPath:indexPath animated:YES];
VideoModel* video = items[indexPath.row];
[[UIApplication sharedApplication] openURL: video.link.href];
}
@end