Initial commit
This commit is contained in:
+17
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// NTESDemoFetchChatroomTask.h
|
||||
// NIM
|
||||
//
|
||||
// Created by amao on 1/20/16.
|
||||
// Copyright © 2016 Netease. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "NTESDemoServiceTask.h"
|
||||
|
||||
typedef void (^NTESChatroomListHandler)(NSError *error, NSArray<NIMChatroom *> *chatroom);
|
||||
|
||||
|
||||
@interface NTESDemoFetchChatroomTask : NSObject<NTESDemoServiceTask>
|
||||
@property (nonatomic,copy) NTESChatroomListHandler handler;
|
||||
@end
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
//
|
||||
// NTESDemoFetchChatroomTask.m
|
||||
// NIM
|
||||
//
|
||||
// Created by amao on 1/20/16.
|
||||
// Copyright © 2016 Netease. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NTESDemoFetchChatroomTask.h"
|
||||
#import "NTESDemoConfig.h"
|
||||
#import "NSDictionary+NTESJson.h"
|
||||
#import "NTESChatroomMaker.h"
|
||||
|
||||
@implementation NTESDemoFetchChatroomTask
|
||||
- (NSURLRequest *)taskRequest
|
||||
{
|
||||
NSString *urlString = [[[NTESDemoConfig sharedConfig] apiURL] stringByAppendingString:@"/chatroom/homeList"];
|
||||
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
|
||||
[request setHTTPMethod:@"GET"];
|
||||
[request addValue:[NTESDemoConfig sharedConfig].appKey forHTTPHeaderField:@"appkey"];
|
||||
[request addValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
|
||||
return request;
|
||||
|
||||
}
|
||||
|
||||
|
||||
- (void)onGetResponse:(id)jsonObject
|
||||
error:(NSError *)error
|
||||
{
|
||||
NSMutableArray *chatrooms = nil;
|
||||
NSError *resultError = error;
|
||||
|
||||
if (error == nil && [jsonObject isKindOfClass:[NSDictionary class]])
|
||||
{
|
||||
NSDictionary *dict = (NSDictionary *)jsonObject;
|
||||
NSInteger code = [dict jsonInteger:@"res"];
|
||||
resultError = code == 200 ? nil : [NSError errorWithDomain:@"ntes domain"
|
||||
code:code
|
||||
userInfo:nil];
|
||||
if (resultError == nil)
|
||||
{
|
||||
chatrooms = [NSMutableArray array];
|
||||
NSDictionary *msg = [dict jsonDict:@"msg"];
|
||||
NSArray *list = [msg jsonArray:@"list"];
|
||||
for (NSDictionary *item in list) {
|
||||
NIMChatroom *chatroom = [NTESChatroomMaker makeChatroom:item];
|
||||
if (chatroom)
|
||||
{
|
||||
[chatrooms addObject:chatroom];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (_handler) {
|
||||
_handler(resultError,chatrooms);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@end
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// NTESDemoRegisterTask.h
|
||||
// NIM
|
||||
//
|
||||
// Created by amao on 1/20/16.
|
||||
// Copyright © 2016 Netease. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "NTESDemoServiceTask.h"
|
||||
|
||||
typedef void(^NTESRegisterHandler)(NSError *error,NSString *errorMsg);
|
||||
|
||||
@interface NTESRegisterData : NSObject
|
||||
@property (nonatomic,copy) NSString *account;
|
||||
|
||||
@property (nonatomic,copy) NSString *token;
|
||||
|
||||
@property (nonatomic,copy) NSString *nickname;
|
||||
@end
|
||||
|
||||
@interface NTESDemoRegisterTask : NSObject<NTESDemoServiceTask>
|
||||
@property (nonatomic,strong) NTESRegisterData *data;
|
||||
@property (nonatomic,copy) NTESRegisterHandler handler;
|
||||
@end
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// NTESDemoRegisterTask.m
|
||||
// NIM
|
||||
//
|
||||
// Created by amao on 1/20/16.
|
||||
// Copyright © 2016 Netease. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NTESDemoRegisterTask.h"
|
||||
#import "NTESDemoConfig.h"
|
||||
#import "NSDictionary+NTESJson.h"
|
||||
|
||||
@implementation NTESRegisterData
|
||||
@end
|
||||
|
||||
@implementation NTESDemoRegisterTask
|
||||
- (NSURLRequest *)taskRequest
|
||||
{
|
||||
NSString *urlString = [[[NTESDemoConfig sharedConfig] apiURL] stringByAppendingString:@"/createDemoUser"];
|
||||
NSURL *url = [NSURL URLWithString:urlString];
|
||||
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url
|
||||
cachePolicy:NSURLRequestUseProtocolCachePolicy
|
||||
timeoutInterval:30];
|
||||
[request setHTTPMethod:@"Post"];
|
||||
|
||||
[request addValue:@"application/x-www-form-urlencoded;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
|
||||
[request addValue:@"nim_demo_ios" forHTTPHeaderField:@"User-Agent"];
|
||||
[request addValue:[[NIMSDK sharedSDK] appKey] forHTTPHeaderField:@"appkey"];
|
||||
|
||||
NSString *postData = [NSString stringWithFormat:@"username=%@&password=%@&nickname=%@",[_data account],[_data token],[_data nickname]];
|
||||
[request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];
|
||||
return request;
|
||||
}
|
||||
|
||||
- (void)onGetResponse:(id)jsonObject error:(NSError *)error
|
||||
{
|
||||
NSError *resultError = error;
|
||||
NSString *errMsg = @"unknown error";
|
||||
if (error == nil && [jsonObject isKindOfClass:[NSDictionary class]])
|
||||
{
|
||||
NSDictionary *dict = (NSDictionary *)jsonObject;
|
||||
NSInteger code = [dict jsonInteger:@"res"];
|
||||
resultError = code == 200 ? nil : [NSError errorWithDomain:@"ntes domain"
|
||||
code:code
|
||||
userInfo:nil];
|
||||
errMsg = dict[@"errmsg"];
|
||||
}
|
||||
if (_handler) {
|
||||
_handler(resultError,errMsg);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@end
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// NTESDemoService.h
|
||||
// NIM
|
||||
//
|
||||
// Created by amao on 1/20/16.
|
||||
// Copyright © 2016 Netease. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "NTESDemoRegisterTask.h"
|
||||
#import "NTESDemoFetchChatroomTask.h"
|
||||
|
||||
|
||||
|
||||
@interface NTESDemoService : NSObject
|
||||
+ (instancetype)sharedService;
|
||||
|
||||
- (void)registerUser:(NTESRegisterData *)data
|
||||
completion:(NTESRegisterHandler)completion;
|
||||
|
||||
- (void)fetchDemoChatrooms:(NTESChatroomListHandler)completion;
|
||||
@end
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
//
|
||||
// NTESDemoService.m
|
||||
// NIM
|
||||
//
|
||||
// Created by amao on 1/20/16.
|
||||
// Copyright © 2016 Netease. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NTESDemoService.h"
|
||||
|
||||
@implementation NTESDemoService
|
||||
+ (instancetype)sharedService
|
||||
{
|
||||
static id instance = nil;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
instance = [[[self class] alloc] init];
|
||||
});
|
||||
return instance;
|
||||
}
|
||||
|
||||
|
||||
- (void)registerUser:(NTESRegisterData *)data
|
||||
completion:(NTESRegisterHandler)completion
|
||||
{
|
||||
NTESDemoRegisterTask *task = [[NTESDemoRegisterTask alloc] init];
|
||||
task.data = data;
|
||||
task.handler = completion;
|
||||
[self runTask:task];
|
||||
}
|
||||
|
||||
- (void)fetchDemoChatrooms:(NTESChatroomListHandler)completion
|
||||
{
|
||||
NTESDemoFetchChatroomTask *task = [[NTESDemoFetchChatroomTask alloc] init];
|
||||
task.handler = completion;
|
||||
[self runTask:task];
|
||||
}
|
||||
|
||||
|
||||
- (void)runTask:(id<NTESDemoServiceTask>)task
|
||||
{
|
||||
if ([[NIMSDK sharedSDK] isUsingDemoAppKey])
|
||||
{
|
||||
NSURLRequest *request = [task taskRequest];
|
||||
[NSURLConnection sendAsynchronousRequest:request
|
||||
queue:[NSOperationQueue mainQueue]
|
||||
completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
|
||||
id jsonObject = nil;
|
||||
NSError *error = connectionError;
|
||||
if (connectionError == nil &&
|
||||
[response isKindOfClass:[NSHTTPURLResponse class]] &&
|
||||
[(NSHTTPURLResponse *)response statusCode] == 200)
|
||||
{
|
||||
if (data)
|
||||
{
|
||||
jsonObject = [NSJSONSerialization JSONObjectWithData:data
|
||||
options:0
|
||||
error:&error];
|
||||
}
|
||||
else
|
||||
{
|
||||
error = [NSError errorWithDomain:@"ntes domain"
|
||||
code:-1
|
||||
userInfo:@{@"description" : @"invalid data"}];
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[task onGetResponse:jsonObject
|
||||
error:error];
|
||||
|
||||
}];
|
||||
}
|
||||
else
|
||||
{
|
||||
//Demo Service中我们模拟了APP服务器所应该实现的部分功能,上层开发需要构建相应的APP服务器,而不是直接使用我们的DEMO服务器
|
||||
[task onGetResponse:nil
|
||||
error:[NSError errorWithDomain:@"ntes domain"
|
||||
code:-1
|
||||
userInfo:@{@"description" : @"use your own app server"}]];
|
||||
}
|
||||
}
|
||||
@end
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// NTESDemoServiceTask.h
|
||||
// NIM
|
||||
//
|
||||
// Created by amao on 1/20/16.
|
||||
// Copyright © 2016 Netease. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@protocol NTESDemoServiceTask <NSObject>
|
||||
- (NSURLRequest *)taskRequest;
|
||||
- (void)onGetResponse:(id)jsonObject
|
||||
error:(NSError *)error;
|
||||
@end
|
||||
Reference in New Issue
Block a user