1、新建工程
2、导入Cocoapods框架
操作命令:打开终端操作步骤:A: cd 工程 B :vim podfile C:pod install
3、AppKey生成链接:http://bbs.mob.com/forum.php?mod=viewthread&tid=8212&extra=page%3D1
4、友盟SDK集成网址(只需要把SDK拖到工程):https://developer.umeng.com/sdk
5、新浪微博登录操作链接:http://wiki.mob.com/快速集成/
(特别提示:友盟SDK集成和新浪微博SDK,下载群里我分享的SDK压缩包)
备注:podfile导入文件参考
target ‘工程名’ do
pod ‘UMCCommon’
pod ‘UMCAnalytics’
pod ’
举个例子
导入
#import "DataManager.h"
#import "AppDelegate.h"
#import "Model.h"
@interface DataManager(){
//声明一个APPdelegate对象属性,调用里面的被管理对象上下文 保存方法
AppDelegate *myDelegate;
}
@end
@implementation DataManager
+(DataManager *)shareManager{
static DataManager *manager;
if (!manager) {
manager = [[DataManager alloc] init];
}
return manager;
}
-(void)insert:(NSDictionary *)dic{
//coreData 数据的插入
myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//创建实体描述对象
NSEntityDescription * description = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:myDelegate.persistentContainer.viewContext];
//创建一个模型对象
Student * student = [[Student alloc]initWithEntity:description insertIntoManagedObjectContext:myDelegate.persistentContainer.viewContext];
student.name = dic[@"name"];
student.create_time = dic[@"create_time"];
student.describe = dic[@"describe"] ;
student.start_time = dic[@"start_time"];
student.type = dic[@"type"];
//对数据管理器的更改进行永久保存
[myDelegate saveContext];
}
-(NSArray *)select{
myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//查询数据
// (1) 设置需要抓取的实体类
NSEntityDescription *selectEneity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:myDelegate.persistentContainer.viewContext];
// (2)实例化抓取对象
NSFetchRequest *req = [[NSFetchRequest alloc] init];
// (3)设置抓取
[req setEntity:selectEneity];
// (4)获取抓取结果
NSArray *res = [myDelegate.persistentContainer.viewContext executeFetchRequest:req error:nil];
return res;
}
-(void)update:(Student *)student{
myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[myDelegate saveContext];
}
-(void)deleteData:(Student *)student{
myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[myDelegate.persistentContainer.viewContext deleteObject:student];
[myDelegate saveContext];
}
@end
管理器.h
#import <Foundation/Foundation.h>
#import “Student+CoreDataClass.h”
#import “Model.h”
NS_ASSUME_NONNULL_BEGIN
@interface DataManager : NSObject
+(DataManager *)shareManager;
-(void)insert:(NSDictionary *)dic;
-(void)update:(Student *)student;
-(void)deleteData:(Student *)student;
-(NSArray *)select;
@end
NS_ASSUME_NONNULL_END
- (void)setvaluesModel:(Model *)model;
- (void)setvaluesModel:(Model *)model{
if (model) {
self.label1.text = model.name;
self.label2.text = model.describe;
self.label3.text = model.start_time;
}
}
#import "ViewController.h"
#import "AFHTTPSessionManager.h"
#import "TableViewCell.h"
#import "Model.h"
#import "DataManager.h"
#import "Student+CoreDataClass.h"
#import "MJExtension.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{
NSArray *arr;
}
@property(nonatomic , strong)UITableView *ojtable;
@property(nonatomic , strong)NSMutableArray *array;
@end
@implementation ViewController
-(UITableView *)ojtable{
if (!_ojtable) {
_ojtable = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
_ojtable.delegate = self;
_ojtable.dataSource = self;
}
[_ojtable registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
return _ojtable;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.array = [[NSMutableArray alloc]init];
arr = @[@"月考",@"常规考试"];
[self.array addObjectsFromArray: [[DataManager shareManager] select]];
if (![[DataManager shareManager] select]) {
NSLog(@"数据库没东西");
}else{
NSLog(@"数据库有东西");
}
[self Load];
[self.view addSubview:self.ojtable];
}
- (void)Load{
AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];
[manager GET:@"http://127.0.0.1/first.json" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// for (NSDictionary *dic in responseObject) {
// Model *model = [Model new];
// [model setValuesForKeysWithDictionary:dic];
// [self.array addObject:model];
// [[DataManager shareManager]insert:@{
// @"name":model.name,
// @"create_time":model.create_time,
// @"student.describe":model.description,
// @"start_time":model.start_time,
// @"type":model.type
// }];
// }
self.array = [Model mj_objectArrayWithKeyValuesArray:responseObject];
for (NSDictionary *dic in responseObject) {
[[DataManager shareManager]insert:dic];
}
[self.ojtable reloadData];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return arr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section == 0) {
return 1;
}else{
return self.array.count;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (self.array.count>0) {
Model *model = self.array[indexPath.row];
[cell setvaluesModel:model];
[cell.Btn addTarget:self action:@selector(StartBtn:)forControlEvents:UIControlEventTouchUpInside];
[cell.Btn setTitle:@"开始考试" forState:UIControlStateNormal];
cell.Btn.tag = indexPath.row;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 150;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return arr[section];
}
- (void)StartBtn:(UIButton *)btn{
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
Student *model = self.array[indexPath.row];
[[DataManager shareManager]deleteData:model];
[self.array removeObject:self.array[indexPath.row]];
[self.ojtable reloadData];
}
@end