前言:
NSURLConnection
虽然已经废弃多年但是不能否定的是它带我从iOS的UI编程升级到了iOS的网络编程再重新敲这些代码的时候我我发现网络封装的思维在被逐渐培养
viewController.m文件
#import "ViewController.h"
#import "UIImageView+WebCache.h"
#import <MediaPlayer/MediaPlayer.h> //系统自带多媒体播放器 ;
//#import <AVKit/AVKit.h> //现在用来替换 MediaPlayer ;
static NSString *const cellID = @"video" ;
@interface ViewController ()
//videoArray:
@property (nonatomic, strong) NSArray *videoArray ;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *urlString = [NSURL URLWithString:@"http://120.25.226.186:32812/video"] ;
NSURLRequest *request = [NSURLRequest requestWithURL:urlString] ;
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil] ;
self.videoArray = dic[@"videos"] ;
NSLog(@"%zd" , self.videoArray.count) ;
[self.tableView reloadData] ;
NSLog(@"完成刷新表格任务") ;
}] ;
}
#pragma mark - <UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.videoArray.count ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID] ;
//解析:
NSDictionary *videoDic = self.videoArray[indexPath.row] ;
cell.textLabel.text = videoDic[@"name"] ;
//cell.detailTextLabel.text = videoDic[@"length"] ;
//NSNumber 不能直接当成字符串来用:
cell.detailTextLabel.text = [NSString stringWithFormat:@"时长:%zd" , videoDic[@"length"]] ;
NSString *imageString = [@"http://120.25.226.186:32812" stringByAppendingPathComponent:videoDic[@"image"]] ;
NSURL *url = [NSURL URLWithString:imageString] ;
[cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"00"]] ;
return cell ;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *videoDic = self.videoArray[indexPath.row] ;
NSString *urlStr = [@"http://120.25.226.186:32812" stringByAppendingPathComponent:videoDic[@"url"]] ;
//@interface MPMoviePlayerController : NSObject <MPMediaPlayback>
//继承自NSObject,是没有图像的概念的:
//@interface MPMoviePlayerViewController : UIViewController
//MPMoviePlayerViewController已经被废弃 ;
//NS_DEPRECATED_IOS(3_2, 9_0, "Use AVPlayerViewController in AVKit.")
//__TVOS_PROHIBITED
MPMoviePlayerViewController *vc = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:urlStr]] ;
[self presentViewController:vc animated:YES completion:^{
NSLog(@"模态视图跳转到MPMoviePlayerController视频播放器") ;
}] ;
}
@end
网友评论