OC代码格式化

This commit is contained in:
v7lin
2019-11-20 12:26:38 +08:00
parent db0d20c975
commit e598cfe50d
2 changed files with 310 additions and 253 deletions

View File

@ -1,6 +1,6 @@
#import "TencentKitPlugin.h" #import "TencentKitPlugin.h"
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h> #import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>
enum TencentScene { enum TencentScene {
SCENE_QQ = 0, SCENE_QQ = 0,
@ -27,10 +27,11 @@ enum TencentRetCode {
} }
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar { + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
FlutterMethodChannel* channel = [FlutterMethodChannel FlutterMethodChannel *channel =
methodChannelWithName:@"v7lin.github.io/tencent_kit" [FlutterMethodChannel methodChannelWithName:@"v7lin.github.io/tencent_kit"
binaryMessenger:[registrar messenger]]; binaryMessenger:[registrar messenger]];
TencentKitPlugin* instance = [[TencentKitPlugin alloc] initWithChannel:channel]; TencentKitPlugin *instance =
[[TencentKitPlugin alloc] initWithChannel:channel];
[registrar addApplicationDelegate:instance]; [registrar addApplicationDelegate:instance];
[registrar addMethodCallDelegate:instance channel:channel]; [registrar addMethodCallDelegate:instance channel:channel];
} }
@ -78,12 +79,15 @@ static NSString* const SCHEME_FILE = @"file";
return self; return self;
} }
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { - (void)handleMethodCall:(FlutterMethodCall *)call
result:(FlutterResult)result {
if ([METHOD_REGISTERAPP isEqualToString:call.method]) { if ([METHOD_REGISTERAPP isEqualToString:call.method]) {
NSString *appId = call.arguments[ARGUMENT_KEY_APPID]; NSString *appId = call.arguments[ARGUMENT_KEY_APPID];
NSString *universalLink = call.arguments[ARGUMENT_KEY_UNIVERSALLINK]; NSString *universalLink = call.arguments[ARGUMENT_KEY_UNIVERSALLINK];
if (universalLink != nil) { if (universalLink != nil) {
_oauth = [[TencentOAuth alloc] initWithAppId:appId andUniversalLink:universalLink andDelegate:self]; _oauth = [[TencentOAuth alloc] initWithAppId:appId
andUniversalLink:universalLink
andDelegate:self];
} else { } else {
_oauth = [[TencentOAuth alloc] initWithAppId:appId andDelegate:self]; _oauth = [[TencentOAuth alloc] initWithAppId:appId andDelegate:self];
} }
@ -108,14 +112,18 @@ static NSString* const SCHEME_FILE = @"file";
} }
- (void)login:(FlutterMethodCall *)call result:(FlutterResult)result { - (void)login:(FlutterMethodCall *)call result:(FlutterResult)result {
if (_oauth != nil) {
NSString *scope = call.arguments[ARGUMENT_KEY_SCOPE]; NSString *scope = call.arguments[ARGUMENT_KEY_SCOPE];
NSArray *permissions = [scope componentsSeparatedByString:@","]; NSArray *permissions = [scope componentsSeparatedByString:@","];
[_oauth authorize:permissions]; [_oauth authorize:permissions];
}
result(nil); result(nil);
} }
- (void)logout:(FlutterMethodCall *)call result:(FlutterResult)result { - (void)logout:(FlutterMethodCall *)call result:(FlutterResult)result {
if (_oauth != nil) {
[_oauth logout:self]; [_oauth logout:self];
}
result(nil); result(nil);
} }
@ -135,11 +143,17 @@ static NSString* const SCHEME_FILE = @"file";
[imageDatas addObject:imageData]; [imageDatas addObject:imageData];
} }
} }
QQApiImageArrayForQZoneObject* object = [QQApiImageArrayForQZoneObject objectWithimageDataArray:imageDatas title:summary extMap:nil]; QQApiImageArrayForQZoneObject *object =
[QQApiImageArrayForQZoneObject objectWithimageDataArray:imageDatas
title:summary
extMap:nil];
SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:object]; SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:object];
[QQApiInterface sendReq:req]; [QQApiInterface sendReq:req];
} else { } else {
QQApiVideoForQZoneObject* object = [QQApiVideoForQZoneObject objectWithAssetURL:videoUri title:summary extMap:nil]; QQApiVideoForQZoneObject *object =
[QQApiVideoForQZoneObject objectWithAssetURL:videoUri
title:summary
extMap:nil];
SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:object]; SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:object];
[QQApiInterface sendReq:req]; [QQApiInterface sendReq:req];
} }
@ -156,7 +170,10 @@ static NSString* const SCHEME_FILE = @"file";
NSURL *imageUrl = [NSURL URLWithString:imageUri]; NSURL *imageUrl = [NSURL URLWithString:imageUri];
NSData *imageData = [NSData dataWithContentsOfFile:imageUrl.path]; NSData *imageData = [NSData dataWithContentsOfFile:imageUrl.path];
QQApiImageObject* object = [QQApiImageObject objectWithData:imageData previewImageData:nil title:nil description:nil]; QQApiImageObject *object = [QQApiImageObject objectWithData:imageData
previewImageData:nil
title:nil
description:nil];
SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:object]; SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:object];
[QQApiInterface sendReq:req]; [QQApiInterface sendReq:req];
} }
@ -177,9 +194,15 @@ static NSString* const SCHEME_FILE = @"file";
NSURL *imageUrl = [NSURL URLWithString:imageUri]; NSURL *imageUrl = [NSURL URLWithString:imageUri];
if ([SCHEME_FILE isEqualToString:imageUrl.scheme]) { if ([SCHEME_FILE isEqualToString:imageUrl.scheme]) {
NSData *imageData = [NSData dataWithContentsOfFile:imageUrl.path]; NSData *imageData = [NSData dataWithContentsOfFile:imageUrl.path];
object = [QQApiAudioObject objectWithURL:[NSURL URLWithString:targetUrl] title:title description:summary previewImageData:imageData]; object = [QQApiAudioObject objectWithURL:[NSURL URLWithString:targetUrl]
title:title
description:summary
previewImageData:imageData];
} else { } else {
object = [QQApiAudioObject objectWithURL:[NSURL URLWithString:targetUrl] title:title description:summary previewImageURL:imageUrl]; object = [QQApiAudioObject objectWithURL:[NSURL URLWithString:targetUrl]
title:title
description:summary
previewImageURL:imageUrl];
} }
object.flashURL = [NSURL URLWithString:musicUrl]; object.flashURL = [NSURL URLWithString:musicUrl];
SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:object]; SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:object];
@ -201,9 +224,15 @@ static NSString* const SCHEME_FILE = @"file";
NSURL *imageUrl = [NSURL URLWithString:imageUri]; NSURL *imageUrl = [NSURL URLWithString:imageUri];
if ([SCHEME_FILE isEqualToString:imageUrl.scheme]) { if ([SCHEME_FILE isEqualToString:imageUrl.scheme]) {
NSData *imageData = [NSData dataWithContentsOfFile:imageUrl.path]; NSData *imageData = [NSData dataWithContentsOfFile:imageUrl.path];
object = [QQApiNewsObject objectWithURL:[NSURL URLWithString:targetUrl] title:title description:summary previewImageData:imageData]; object = [QQApiNewsObject objectWithURL:[NSURL URLWithString:targetUrl]
title:title
description:summary
previewImageData:imageData];
} else { } else {
object = [QQApiNewsObject objectWithURL:[NSURL URLWithString:targetUrl] title:title description:summary previewImageURL:imageUrl]; object = [QQApiNewsObject objectWithURL:[NSURL URLWithString:targetUrl]
title:title
description:summary
previewImageURL:imageUrl];
} }
SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:object]; SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:object];
if (scene.intValue == SCENE_QQ) { if (scene.intValue == SCENE_QQ) {
@ -217,22 +246,39 @@ static NSString* const SCHEME_FILE = @"file";
#pragma mark - AppDelegate #pragma mark - AppDelegate
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [QQApiInterface handleOpenURL:url delegate:self] || ([TencentOAuth CanHandleOpenURL:url] && [TencentOAuth HandleOpenURL:url]); return
[QQApiInterface handleOpenURL:url delegate:self] ||
([TencentOAuth CanHandleOpenURL:url] && [TencentOAuth HandleOpenURL:url]);
} }
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { - (BOOL)application:(UIApplication *)application
return [QQApiInterface handleOpenURL:url delegate:self] || ([TencentOAuth CanHandleOpenURL:url] && [TencentOAuth HandleOpenURL:url]); openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return
[QQApiInterface handleOpenURL:url delegate:self] ||
([TencentOAuth CanHandleOpenURL:url] && [TencentOAuth HandleOpenURL:url]);
} }
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options { - (BOOL)application:(UIApplication *)application
return [QQApiInterface handleOpenURL:url delegate:self] || ([TencentOAuth CanHandleOpenURL:url] && [TencentOAuth HandleOpenURL:url]); openURL:(NSURL *)url
options:
(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
return
[QQApiInterface handleOpenURL:url delegate:self] ||
([TencentOAuth CanHandleOpenURL:url] && [TencentOAuth HandleOpenURL:url]);
} }
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nonnull))restorationHandler { - (BOOL)application:(UIApplication *)application
if([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) { continueUserActivity:(NSUserActivity *)userActivity
restorationHandler:(void (^)(NSArray *_Nonnull))restorationHandler {
if ([userActivity.activityType
isEqualToString:NSUserActivityTypeBrowsingWeb]) {
NSURL *url = userActivity.webpageURL; NSURL *url = userActivity.webpageURL;
if (url != nil) { if (url != nil) {
return [QQApiInterface handleOpenUniversallink:url delegate:self] || ([TencentOAuth CanHandleUniversalLink:url] && [TencentOAuth HandleUniversalLink:url]); return [QQApiInterface handleOpenUniversallink:url delegate:self] ||
([TencentOAuth CanHandleUniversalLink:url] &&
[TencentOAuth HandleUniversalLink:url]);
} }
} }
return NO; return NO;
@ -245,16 +291,21 @@ static NSString* const SCHEME_FILE = @"file";
if (_oauth.accessToken != nil && _oauth.accessToken.length > 0) { if (_oauth.accessToken != nil && _oauth.accessToken.length > 0) {
NSString *openId = _oauth.openId; NSString *openId = _oauth.openId;
NSString *accessToken = _oauth.accessToken; NSString *accessToken = _oauth.accessToken;
long long expiresIn = ceil(_oauth.expirationDate.timeIntervalSinceNow);// 向上取整 long long expiresIn =
ceil(_oauth.expirationDate.timeIntervalSinceNow); // 向上取整
long long createAt = [[NSDate date] timeIntervalSince1970] * 1000.0; long long createAt = [[NSDate date] timeIntervalSince1970] * 1000.0;
[dictionary setValue:[NSNumber numberWithInt:RET_SUCCESS] forKey:ARGUMENT_KEY_RESULT_RET]; [dictionary setValue:[NSNumber numberWithInt:RET_SUCCESS]
forKey:ARGUMENT_KEY_RESULT_RET];
[dictionary setValue:openId forKey:ARGUMENT_KEY_RESULT_OPENID]; [dictionary setValue:openId forKey:ARGUMENT_KEY_RESULT_OPENID];
[dictionary setValue:accessToken forKey:ARGUMENT_KEY_RESULT_ACCESS_TOKEN]; [dictionary setValue:accessToken forKey:ARGUMENT_KEY_RESULT_ACCESS_TOKEN];
[dictionary setValue:[NSNumber numberWithLongLong:expiresIn] forKey:ARGUMENT_KEY_RESULT_EXPIRES_IN]; [dictionary setValue:[NSNumber numberWithLongLong:expiresIn]
[dictionary setValue:[NSNumber numberWithLongLong:createAt] forKey:ARGUMENT_KEY_RESULT_CREATE_AT]; forKey:ARGUMENT_KEY_RESULT_EXPIRES_IN];
[dictionary setValue:[NSNumber numberWithLongLong:createAt]
forKey:ARGUMENT_KEY_RESULT_CREATE_AT];
} else { } else {
// 登录失败 // 登录失败
[dictionary setValue:[NSNumber numberWithInt:RET_COMMON] forKey:ARGUMENT_KEY_RESULT_RET]; [dictionary setValue:[NSNumber numberWithInt:RET_COMMON]
forKey:ARGUMENT_KEY_RESULT_RET];
} }
[_channel invokeMethod:METHOD_ONLOGINRESP arguments:dictionary]; [_channel invokeMethod:METHOD_ONLOGINRESP arguments:dictionary];
} }
@ -263,10 +314,12 @@ static NSString* const SCHEME_FILE = @"file";
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
if (cancelled) { if (cancelled) {
// 取消登录 // 取消登录
[dictionary setValue:[NSNumber numberWithInt:RET_USERCANCEL] forKey:ARGUMENT_KEY_RESULT_RET]; [dictionary setValue:[NSNumber numberWithInt:RET_USERCANCEL]
forKey:ARGUMENT_KEY_RESULT_RET];
} else { } else {
// 登录失败 // 登录失败
[dictionary setValue:[NSNumber numberWithInt:RET_COMMON] forKey:ARGUMENT_KEY_RESULT_RET]; [dictionary setValue:[NSNumber numberWithInt:RET_COMMON]
forKey:ARGUMENT_KEY_RESULT_RET];
} }
[_channel invokeMethod:METHOD_ONLOGINRESP arguments:dictionary]; [_channel invokeMethod:METHOD_ONLOGINRESP arguments:dictionary];
} }
@ -274,14 +327,14 @@ static NSString* const SCHEME_FILE = @"file";
- (void)tencentDidNotNetWork { - (void)tencentDidNotNetWork {
// 登录失败 // 登录失败
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[dictionary setValue:[NSNumber numberWithInt:RET_COMMON] forKey:ARGUMENT_KEY_RESULT_RET]; [dictionary setValue:[NSNumber numberWithInt:RET_COMMON]
forKey:ARGUMENT_KEY_RESULT_RET];
[_channel invokeMethod:METHOD_ONLOGINRESP arguments:dictionary]; [_channel invokeMethod:METHOD_ONLOGINRESP arguments:dictionary];
} }
#pragma mark - QQApiInterfaceDelegate #pragma mark - QQApiInterfaceDelegate
- (void)onReq:(QQBaseReq *)req { - (void)onReq:(QQBaseReq *)req {
} }
- (void)onResp:(QQBaseResp *)resp { - (void)onResp:(QQBaseResp *)resp {
@ -290,15 +343,20 @@ static NSString* const SCHEME_FILE = @"file";
switch (resp.result.intValue) { switch (resp.result.intValue) {
case 0: case 0:
// 分享成功 // 分享成功
[dictionary setValue:[NSNumber numberWithInt:RET_SUCCESS] forKey:ARGUMENT_KEY_RESULT_RET]; [dictionary setValue:[NSNumber numberWithInt:RET_SUCCESS]
forKey:ARGUMENT_KEY_RESULT_RET];
break; break;
case -4: case -4:
// 用户取消 // 用户取消
[dictionary setValue:[NSNumber numberWithInt:RET_USERCANCEL] forKey:ARGUMENT_KEY_RESULT_RET]; [dictionary setValue:[NSNumber numberWithInt:RET_USERCANCEL]
forKey:ARGUMENT_KEY_RESULT_RET];
break; break;
default: default:
[dictionary setValue:[NSNumber numberWithInt:RET_COMMON] forKey:ARGUMENT_KEY_RESULT_RET]; [dictionary setValue:[NSNumber numberWithInt:RET_COMMON]
NSString* errorMsg = [NSString stringWithFormat:@"result: %@, description: %@.", resp.result, resp.errorDescription]; forKey:ARGUMENT_KEY_RESULT_RET];
NSString *errorMsg =
[NSString stringWithFormat:@"result: %@, description: %@.",
resp.result, resp.errorDescription];
[dictionary setValue:errorMsg forKey:ARGUMENT_KEY_RESULT_MSG]; [dictionary setValue:errorMsg forKey:ARGUMENT_KEY_RESULT_MSG];
break; break;
} }
@ -307,7 +365,6 @@ static NSString* const SCHEME_FILE = @"file";
} }
- (void)isOnlineResponse:(NSDictionary *)response { - (void)isOnlineResponse:(NSDictionary *)response {
} }
@end @end