#import "AppDelegate.h"
#import "WeChatSDK1.8.3_NoPay/WXApi.h"
@interface AppDelegate ()<WXApiDelegate>
@end
@implementation AppDelegate
-(void) onResp:(BaseResp*)resp{
}
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
[WXApi registerApp:@"wx324a10d04bd2cd5c"];
return YES;
}
- (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url {
return [WXApi handleOpenURL:url delegate:self];
}
- (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation {
return [WXApi handleOpenURL:url delegate:self];
#import "ViewController.h"
#import "GoldTableViewCell.h"
#import "MBProgressHUD/MBProgressHUD.h"
#import "MJRefresh/MJRefresh.h"
#import"Gold.h"
#import "WeChatSDK1.8.3_NoPay/WXApi.h"
#define SCR_W [UIScreen mainScreen].bounds.size.width
#define SCR_H [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
NSString* _shareString;
}
@property (weak, nonatomic) IBOutlet UITableView *table;
@property (nonatomic,strong) NSMutableArray * tableDataArr;
@property (nonatomic,strong) UIView * shareView;
@property (nonatomic,strong)MJRefreshHeaderView * mjHeadderView;
@end
@implementation ViewController
-(void)hideShareView:(id)sender{
[UIView animateWithDuration:0.235 animations:^{
self.shareView.frame=CGRectMake(0,SCR_H,SCR_W,200);
}];
}
-(UIView*)shareView{
if (!_shareView) {
_shareView =[[UIView alloc]initWithFrame:CGRectMake(0, SCR_H, SCR_W, 200)];
_shareView.backgroundColor =[UIColor lightGrayColor];
// 单击手势
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideShareView:)];
[_shareView addGestureRecognizer:tap];
NSArray* btnTitles =@[@"微信",@"朋友圈",@"qq",@"微博"];
CGFloatbtnW =60;
CGFloatdis = (SCR_W-btnW * btnTitles.count) / (btnTitles.count+1);
for(inti =0; i < btnTitles.count; i++) {
UIButton * btn =[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame =CGRectMake(dis + (dis +btnW) * i,70, btnW, btnW);
btn.clipsToBounds=YES;
btn.layer.cornerRadius= btnW /2;
[btnsetTitle:btnTitles[i] forState:UIControlStateNormal];
btn.backgroundColor =[UIColor greenColor];
[btnsetTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
btn.tag=100+ i;
[btnaddTarget:self action:@selector(shareBtnDidPress:) forControlEvents:UIControlEventTouchUpInside];
[_shareViewaddSubview:btn];
}
}
return _shareView;
}
-(void)shareBtnDidPress:(UIButton*)sender{
switch(sender.tag) {
case100:
{
NSLog(@"微信");
SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
req.bText=YES;
req.text=_shareString;
req.scene=WXSceneSession;
[WXApisendReq:req];
}
break;
case101:
{
NSLog(@"朋友圈");
SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
req.bText=YES;
req.text=_shareString;
req.scene = WXSceneTimeline;
[WXApisendReq:req];
}
break;
case102:
{
NSLog(@"qq");
}
break;
case103:
{
NSLog(@"微博");
}
break;
default:
break;
}
}
-(void)showMBAlertwithMessage :(NSString*)msg{
MBProgressHUD * hud =[[MBProgressHUD alloc]initWithView:self.view];
hud.mode = MBProgressHUDModeText;
hud.labelText= msg;
hud.removeFromSuperViewOnHide =YES;
[self.view addSubview:hud];
[hudshow:YES];
[hudhide:YES afterDelay:3.0];
}
-(void)getHttpData{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSString * urlStr= [NSString stringWithFormat:@"http://web.juhe.cn:8080/finance/gold/shgold?key=%@&v=1",@"1b9f570e367ce24e51135d69eac7d04e"];
urlStr =[urlStrstringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
NSURL * url =[NSURL URLWithString:urlStr];
NSURLRequest * req =[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:3.0];
[[[NSURLSession sharedSession]dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[self.mjHeadderView endRefreshing];
});
if(error !=nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[self showMBAlertwithMessage:@"服务器错误"];
});
return;
}
NSError* jsonError =nil;
id jsonData =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonError];
if(jsonError !=nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[self showMBAlertwithMessage:@"网络数据错误"];
});
return;
}
intresultcode = [jsonData[@"resultcode"]intValue];
//200 cuowu
if(resultcode !=200) {
dispatch_async(dispatch_get_main_queue(), ^{
[selfshowMBAlertwithMessage:jsonData[@"reason"]];
});
return;
}
//获取数组
NSArray* resultArr = jsonData[@"result"];
//获取大字典
NSDictionary* allDic = [resultArrlastObject];
NSMutableArray * modelArr =[[NSMutableArray alloc]init];
//遍历大字典中所有字典
for(NSDictionary* dicinallDic.allValues) {
Gold* g =[[Goldalloc]init];
[gsetValuesForKeysWithDictionary:dic];
[modelArraddObject:g];
// g.variety =dic[@"variety"];
}
//将model数组fuzhi
dispatch_async(dispatch_get_main_queue(), ^{
self->_tableDataArr=[modelArrcopy];
[self.tablereloadData];
});
}]resume];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.table.rowHeight=93;
self.mjHeadderView =[[MJRefreshHeaderView alloc]initWithScrollView:self.table];
__weaktypeof(self) weakSelf =self;
self.mjHeadderView.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
[weakSelfgetHttpData];
};
[self.table registerNib:[UINib nibWithNibName:@"GoldTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"goldCell"];
[self.view addSubview:self.shareView];
[self getHttpData];
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
return _tableDataArr.count;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
GoldTableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"goldCell" forIndexPath:indexPath];
Gold* gold =_tableDataArr[indexPath.row];
cell.varietyLabel.text = gold.variety;
cell.latestPriLabel.text =gold.latestpri;
cell.openPrilabel.text =gold.openpri;
cell.maxPriLabel.text=gold.maxpri;
cell.minPriLabel.text=gold.minpri;
returncell;
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
Gold* gold =[_tableDataArrobjectAtIndex:indexPath.row];
// fenxiang fuzhi
_shareString=[NSStringstringWithFormat:@"%@,最新%@元,最高%@元,最低%@元",gold.variety,gold.latestpri,gold.maxpri,gold.minpri];
[UIView animateWithDuration:0.235 animations:^{
self.shareView.frame=CGRectMake(0,SCR_H-200,SCR_W,200);
}];
}
@end
#import
NS_ASSUME_NONNULL_BEGIN
@interface Gold : NSObject
@property(nonatomic,strong)NSString * variety;
@property(nonatomic,strong)NSString * latestpri;
@property(nonatomic,strong)NSString * openpri;
@property(nonatomic,strong)NSString * maxpri;
@property(nonatomic,strong)NSString * minpri;
@property(nonatomic,strong)NSString * limit;
@property(nonatomic,strong)NSString * yespri;
@property(nonatomic,strong)NSString * totalvol;
@property(nonatomic,strong)NSString * time;
@end
NS_ASSUME_NONNULL_END
#import
NS_ASSUME_NONNULL_BEGIN
@interfaceGoldTableViewCell :UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *varietyLabel;
@property (weak, nonatomic) IBOutlet UILabel *latestPriLabel;
@property (weak, nonatomic) IBOutlet UILabel *openPrilabel;
@property (weak, nonatomic) IBOutlet UILabel *maxPriLabel;
@property (weak, nonatomic) IBOutlet UILabel *minPriLabel;
@end
NS_ASSUME_NONNULL_END