首先修改 App Transport Security Settings
Allow Arbitrary Loads
Bundle name等
导入SDWebImage
AppDelegate.m中
#import "AppDelegate.h"
#import "ViewController.h"
#import "NewsViewController.h"
#import "DetailViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSUserDefaults *uf = [NSUserDefaults standardUserDefaults];
NSString *version = [uf objectForKey:@"systemVersion"];
NSDictionary *dic = [[NSBundle mainBundle] infoDictionary];
if ([[dic objectForKey:@"CFBundleShortVersionString"] isEqualToString:version]) {
UITabBarController *tbc = [[UITabBarController alloc]init];
NewsViewController *nvc = [[NewsViewController alloc]init];
nvc.title = @"新闻播报";
nvc.tabBarItem.image = [UIImage imageNamed:@"1.png"];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:nvc];
DetailViewController *dvc = [[DetailViewController alloc]init];
dvc.title = @"我的收藏";
dvc.tabBarItem.image = [UIImage imageNamed:@"1.png"];
UINavigationController *dnav = [[UINavigationController alloc]initWithRootViewController:dvc];
tbc.viewControllers = @[nav,dnav];
self.window.rootViewController = tbc;
}else{
ViewController *vc = [[ViewController alloc]init];
self.window.rootViewController = vc;
[uf setObject:[dic objectForKey:@"CFBundleShortVersionString"] forKey:@"systemVersion"];
}
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
在LoadData.h中
#import
@interface LoadData : NSObject
+ (instancetype)shareLoadData;
- (void)getData;
@end
在LoadData.m中
#import "LoadData.h"
#import "Model.h"
static LoadData *ld;
@implementation LoadData
+ (instancetype)shareLoadData
{
if (!ld) {
ld = [[LoadData alloc]init];
}
return ld;
}
- (void)getData
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://api.jisuapi.com/news/get"]];
request.HTTPMethod = @"POST";
NSString *str = [NSString stringWithFormat:@"channel=%@&start=%d&num=%d&appkey=%@",@"头条",0,20,@"de394933e1a3e2db"];
NSData *bodyData = [str dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody = bodyData;
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSDictionary *dict = [dic objectForKey:@"result"];
NSArray *arr = [dict objectForKey:@"list"];
NSMutableArray *mArr = [NSMutableArray array];
for (NSDictionary *dictionary in arr) {
Model *m = [[Model alloc]init];
m.title = dictionary[@"title"];
m.pic = dictionary[@"pic"];
m.weburl = dictionary[@"weburl"];
[mArr addObject:m];
}
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter]postNotificationName:@"data" object:mArr];
});
}];
[task resume];
}
@end
在ViewController.m中
#import "ViewController.h"
#import "AppDelegate.h"
#import "NewsViewController.h"
#import "DetailViewController.h"
@interface ViewController ()
{
UIScrollView *sv;
UIPageControl *pc;
NSArray *marr;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
marr = @[@"2",@"4",@"9"];
sv = [[UIScrollView alloc]initWithFrame:self.view.bounds];
sv.pagingEnabled = YES;
sv.bounces = NO;
sv.showsHorizontalScrollIndicator = YES;
sv.contentSize = CGSizeMake(self.view.frame.size.width*3, self.view.frame.size.height);
sv.delegate = self;
[self.view addSubview:sv];
for (int i = 0,x = 0; i < 3; i ++) {
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(x, 0, self.view.frame.size.width, self.view.frame.size.height)];
img.image = [UIImage imageNamed:marr[i]];
[sv addSubview:img];
x += self.view.frame.size.width;
}
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width*2, 450, self.view.frame.size.width, 44)];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btn setTitle:@"立即体验" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[sv addSubview:btn];
pc = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 500, self.view.frame.size.width, 44)];
pc.numberOfPages = 3;
[pc addTarget:self action:@selector(pageValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pc];
}
- (void)btnClicked
{
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
UITabBarController *tbc = [[UITabBarController alloc]init];
NewsViewController *nvc = [[NewsViewController alloc]init];
nvc.title = @"新闻播报";
nvc.tabBarItem.image = [UIImage imageNamed:@"1.png"];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:nvc];
DetailViewController *dvc = [[DetailViewController alloc]init];
dvc.title = @"我的收藏";
dvc.tabBarItem.image = [UIImage imageNamed:@"1.png"];
UINavigationController *dnav = [[UINavigationController alloc]initWithRootViewController:dvc];
tbc.viewControllers = @[nav,dnav];
app.window.rootViewController = tbc;
}
- (void)pageValueChanged:(UIPageControl *)sender
{
sv.contentOffset = CGPointMake(sender.currentPage*self.view.frame.size.width, 0);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
pc.currentPage = sv.contentOffset.x/self.view.frame.size.width;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
在NewsViewController.m中
#import "NewsViewController.h"
#import "LoadData.h"
#import "Model.h"
#import "UIImageView+WebCache.h"
#import "DetailViewController.h"
@interface NewsViewController ()
{
NSArray *arr;
UITableView *table;
}
@end
@implementation NewsViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notifiUpdate:) name:@"data" object:nil];
[[LoadData shareLoadData]getData];
table = [[UITableView alloc]initWithFrame:self.view.bounds];
table.dataSource = self;
table.delegate = self;
[table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellid"];
[self.view addSubview:table];
}
- (void)notifiUpdate:(NSNotification *)notifi
{
arr = [notifi.object copy];
[table reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellid"];
Model *m = arr[indexPath.row];
cell.textLabel.text = m.title;
// [cell.imageView setImageWithURL:[NSURL URLWithString:m.pic]];
// cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"m.pic"]]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Model *m = arr[indexPath.row];
DetailViewController *dvc = [[DetailViewController alloc]init];
dvc.weburl = m.weburl;
[self.navigationController pushViewController:dvc animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
在DetailViewController.h中
#import
@interface DetailViewController : UIViewController
@property (nonatomic,strong)NSString *weburl;
@end
在DetailViewController.m中
#import "DetailViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
//
UIWebView *wv = [[UIWebView alloc]initWithFrame:self.view.bounds];
[wv loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.weburl]]];
[self.view addSubview:wv];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
在Model.h中
#import
@interface Model : NSObject
@property (nonatomic,strong)NSString *title,*pic,*weburl;
@end
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
@synthesize executing=_executing;
@synthesize finished=_finished;