添加部分埋点
This commit is contained in:
39
Shared/KBMaiPointReporter.h
Normal file
39
Shared/KBMaiPointReporter.h
Normal file
@@ -0,0 +1,39 @@
|
||||
//
|
||||
// KBMaiPointReporter.h
|
||||
// keyBoard
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#ifndef KB_MAI_POINT_BASE_URL
|
||||
#define KB_MAI_POINT_BASE_URL @"http://192.168.1.188:35310/api"
|
||||
#endif
|
||||
|
||||
#ifndef KB_MAI_POINT_PATH_NEW_ACCOUNT
|
||||
#define KB_MAI_POINT_PATH_NEW_ACCOUNT @"/newAccount"
|
||||
#endif
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
extern NSString * const KBMaiPointErrorDomain;
|
||||
|
||||
typedef void (^KBMaiPointReportCompletion)(BOOL success, NSError * _Nullable error);
|
||||
|
||||
/// Lightweight reporter for Mai point tracking. Safe for app + extension.
|
||||
@interface KBMaiPointReporter : NSObject
|
||||
|
||||
+ (instancetype)sharedReporter;
|
||||
|
||||
/// POST /newAccount with type + account.
|
||||
- (void)reportNewAccountWithType:(NSString *)type
|
||||
account:(NSString *)account
|
||||
completion:(KBMaiPointReportCompletion _Nullable)completion;
|
||||
|
||||
/// Generic POST for future endpoints.
|
||||
- (void)postPath:(NSString *)path
|
||||
parameters:(NSDictionary *)parameters
|
||||
completion:(KBMaiPointReportCompletion _Nullable)completion;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
127
Shared/KBMaiPointReporter.m
Normal file
127
Shared/KBMaiPointReporter.m
Normal file
@@ -0,0 +1,127 @@
|
||||
//
|
||||
// KBMaiPointReporter.m
|
||||
// keyBoard
|
||||
//
|
||||
|
||||
#import "KBMaiPointReporter.h"
|
||||
|
||||
NSString * const KBMaiPointErrorDomain = @"KBMaiPointErrorDomain";
|
||||
|
||||
@implementation KBMaiPointReporter
|
||||
|
||||
+ (instancetype)sharedReporter {
|
||||
static KBMaiPointReporter *reporter = nil;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
reporter = [[KBMaiPointReporter alloc] init];
|
||||
});
|
||||
return reporter;
|
||||
}
|
||||
|
||||
- (void)reportNewAccountWithType:(NSString *)type
|
||||
account:(NSString *)account
|
||||
completion:(KBMaiPointReportCompletion)completion {
|
||||
NSString *trimmedType = [type stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
||||
NSString *trimmedAccount = [account stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
||||
if (trimmedType.length == 0 || trimmedAccount.length == 0) {
|
||||
NSError *error = [NSError errorWithDomain:KBMaiPointErrorDomain
|
||||
code:-1
|
||||
userInfo:@{NSLocalizedDescriptionKey: @"Invalid parameter"}];
|
||||
if (completion) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
completion(NO, error);
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
NSDictionary *params = @{
|
||||
@"type": trimmedType,
|
||||
@"account": trimmedAccount
|
||||
};
|
||||
[self postPath:KB_MAI_POINT_PATH_NEW_ACCOUNT parameters:params completion:completion];
|
||||
}
|
||||
|
||||
- (void)postPath:(NSString *)path
|
||||
parameters:(NSDictionary *)parameters
|
||||
completion:(KBMaiPointReportCompletion)completion {
|
||||
if (path.length == 0 || ![parameters isKindOfClass:[NSDictionary class]]) {
|
||||
NSError *error = [NSError errorWithDomain:KBMaiPointErrorDomain
|
||||
code:-1
|
||||
userInfo:@{NSLocalizedDescriptionKey: @"Invalid parameter"}];
|
||||
if (completion) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
completion(NO, error);
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
NSString *safePath = [path hasPrefix:@"/"] ? path : [@"/" stringByAppendingString:path];
|
||||
NSString *urlString = [NSString stringWithFormat:@"%@%@", KB_MAI_POINT_BASE_URL, safePath];
|
||||
NSURL *url = [NSURL URLWithString:urlString];
|
||||
if (!url) {
|
||||
NSError *error = [NSError errorWithDomain:KBMaiPointErrorDomain
|
||||
code:-2
|
||||
userInfo:@{NSLocalizedDescriptionKey: @"Invalid URL"}];
|
||||
if (completion) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
completion(NO, error);
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
NSError *jsonError = nil;
|
||||
NSData *body = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&jsonError];
|
||||
if (jsonError) {
|
||||
if (completion) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
completion(NO, jsonError);
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
|
||||
request.HTTPMethod = @"POST";
|
||||
request.timeoutInterval = 10.0;
|
||||
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
|
||||
request.HTTPBody = body;
|
||||
|
||||
NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
|
||||
config.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
|
||||
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
|
||||
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
|
||||
completionHandler:^(NSData *data,
|
||||
NSURLResponse *response,
|
||||
NSError *error) {
|
||||
BOOL success = NO;
|
||||
NSError *finalError = error;
|
||||
|
||||
if (!finalError) {
|
||||
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
|
||||
NSInteger statusCode = ((NSHTTPURLResponse *)response).statusCode;
|
||||
success = (statusCode >= 200 && statusCode < 300);
|
||||
if (!success) {
|
||||
finalError = [NSError errorWithDomain:KBMaiPointErrorDomain
|
||||
code:statusCode
|
||||
userInfo:@{NSLocalizedDescriptionKey: @"Invalid response"}];
|
||||
}
|
||||
} else {
|
||||
finalError = [NSError errorWithDomain:KBMaiPointErrorDomain
|
||||
code:-3
|
||||
userInfo:@{NSLocalizedDescriptionKey: @"Invalid response"}];
|
||||
}
|
||||
}
|
||||
|
||||
if (completion) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
completion(success, finalError);
|
||||
});
|
||||
}
|
||||
}];
|
||||
[task resume];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user