diff --git a/TdsDemo.xcodeproj/project.pbxproj b/TdsDemo.xcodeproj/project.pbxproj index 8f6a5e4..c758498 100644 --- a/TdsDemo.xcodeproj/project.pbxproj +++ b/TdsDemo.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 3F427CC9259B28450072677B /* TapDBViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3F427CC8259B28450072677B /* TapDBViewController.m */; }; 7B0662442591D3240000DE66 /* TapSDK.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7B0662432591D3240000DE66 /* TapSDK.framework */; }; 7B560EA525903D12006840E1 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 7B560EA425903D12006840E1 /* AppDelegate.m */; }; 7B560EA825903D12006840E1 /* SceneDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 7B560EA725903D12006840E1 /* SceneDelegate.m */; }; @@ -27,6 +28,8 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 3F427CC7259B28450072677B /* TapDBViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TapDBViewController.h; sourceTree = ""; }; + 3F427CC8259B28450072677B /* TapDBViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TapDBViewController.m; sourceTree = ""; }; 7B0662432591D3240000DE66 /* TapSDK.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = TapSDK.framework; sourceTree = ""; }; 7B560EA025903D12006840E1 /* TdsDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TdsDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 7B560EA325903D12006840E1 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; @@ -99,6 +102,8 @@ 7B560EA725903D12006840E1 /* SceneDelegate.m */, 7B560EA925903D12006840E1 /* ViewController.h */, 7B560EAA25903D12006840E1 /* ViewController.m */, + 3F427CC7259B28450072677B /* TapDBViewController.h */, + 3F427CC8259B28450072677B /* TapDBViewController.m */, 7B560EAC25903D12006840E1 /* Main.storyboard */, 7B560EAF25903D13006840E1 /* Assets.xcassets */, 7B560EB125903D13006840E1 /* LaunchScreen.storyboard */, @@ -194,6 +199,7 @@ buildActionMask = 2147483647; files = ( 7B560EAB25903D12006840E1 /* ViewController.m in Sources */, + 3F427CC9259B28450072677B /* TapDBViewController.m in Sources */, 7B560EA525903D12006840E1 /* AppDelegate.m in Sources */, 7B560EB625903D13006840E1 /* main.m in Sources */, 7B560EA825903D12006840E1 /* SceneDelegate.m in Sources */, diff --git a/TdsDemo/TapDBViewController.h b/TdsDemo/TapDBViewController.h new file mode 100644 index 0000000..c18fafd --- /dev/null +++ b/TdsDemo/TapDBViewController.h @@ -0,0 +1,16 @@ +// +// TapDBViewController.h +// TdsDemo +// +// Created by Bottle K on 2020/12/29. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface TapDBViewController : UIViewController + +@end + +NS_ASSUME_NONNULL_END diff --git a/TdsDemo/TapDBViewController.m b/TdsDemo/TapDBViewController.m new file mode 100644 index 0000000..92b6a84 --- /dev/null +++ b/TdsDemo/TapDBViewController.m @@ -0,0 +1,31 @@ +// +// TapDBViewController.m +// TdsDemo +// +// Created by Bottle K on 2020/12/29. +// + +#import "TapDBViewController.h" + +@interface TapDBViewController () + +@end + +@implementation TapDBViewController + +- (void)viewDidLoad { + [super viewDidLoad]; + +} + +/* +#pragma mark - Navigation + +// In a storyboard-based application, you will often want to do a little preparation before navigation +- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { + // Get the new view controller using [segue destinationViewController]. + // Pass the selected object to the new view controller. +} +*/ + +@end diff --git a/TdsDemo/ViewController.m b/TdsDemo/ViewController.m index 8335dce..26de5e8 100644 --- a/TdsDemo/ViewController.m +++ b/TdsDemo/ViewController.m @@ -6,6 +6,7 @@ // #import "ViewController.h" +#import "TapDBViewController.h" #import #import #import @@ -13,138 +14,154 @@ @interface ViewController () -- (void) initTapSDK; +- (void)initTapSDK; @end - @implementation ViewController - - (void)viewDidLoad { [super viewDidLoad]; - + UIButton *loginButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 50)]; loginButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; [loginButton setTitle:@"登录" forState:UIControlStateNormal]; [loginButton setTitleColor:UIColor.blackColor forState:UIControlStateNormal]; [loginButton addTarget:self action:@selector(taptapLogin:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:loginButton]; - - + UIButton *logoutButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 200, 100, 50)]; logoutButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; [logoutButton setTitle:@"登出" forState:UIControlStateNormal]; [logoutButton setTitleColor:UIColor.blackColor forState:UIControlStateNormal]; [logoutButton addTarget:self action:@selector(taptapLogout:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:logoutButton]; - - - - UIButton *momentButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 300, 100, 50)]; + + UIButton *profileButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 300, 300, 50)]; + profileButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; + [profileButton setTitle:@"获取用户最新信息" forState:UIControlStateNormal]; + [profileButton setTitleColor:UIColor.blackColor forState:UIControlStateNormal]; + [profileButton addTarget:self action:@selector(fetchProfile:) forControlEvents:UIControlEventTouchUpInside]; + [self.view addSubview:profileButton]; + + UIButton *momentButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 400, 100, 50)]; momentButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; [momentButton setTitle:@"打开动态" forState:UIControlStateNormal]; [momentButton setTitleColor:UIColor.blackColor forState:UIControlStateNormal]; [momentButton addTarget:self action:@selector(taptapMoment:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:momentButton]; - - - UIButton *momentRedPoint = [[UIButton alloc] initWithFrame:CGRectMake(100, 400, 300, 50)]; + + UIButton *momentRedPoint = [[UIButton alloc] initWithFrame:CGRectMake(100, 500, 300, 50)]; momentRedPoint.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; [momentRedPoint setTitle:@"获取动态未读数" forState:UIControlStateNormal]; [momentRedPoint setTitleColor:UIColor.blackColor forState:UIControlStateNormal]; [momentRedPoint addTarget:self action:@selector(taptapMomentRedPoint:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:momentRedPoint]; - + + UIButton *dbButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 600, 300, 50)]; + dbButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; + [dbButton setTitle:@"进入TapDB测试" forState:UIControlStateNormal]; + [dbButton setTitleColor:UIColor.blackColor forState:UIControlStateNormal]; + [dbButton addTarget:self action:@selector(toTapDB:) forControlEvents:UIControlEventTouchUpInside]; + [self.view addSubview:dbButton]; + [self initTapSDK]; } -- (void) initTapSDK { - +- (void)initTapSDK { TDSConfig *config = TDSConfig.new; config.clientId = @"FwFdCIr6u71WQDQwQN"; - + [TDSInitializer initWithConfig:config]; // 注册登录相关信息 [TapLoginHelper registerLoginResultDelegate:self]; - + // 开启 TapDB [TDSInitializer enableTapDBWithChannel:@"default" gameVersion:@"1.0.0"]; NSLog(@"current profile %@", [TTSDKProfile currentProfile].toJsonString); if ([TTSDKProfile currentProfile ] != nil) { NSLog(@"current has user %@", [TTSDKProfile currentProfile].openid); - [TapDB setUser:[TTSDKProfile currentProfile].openid loginType:TapDBLoginTypeTapTap]; + [TapDB setUser:[NSString stringWithFormat:@"%@_userid", [TTSDKProfile currentProfile].openid] loginType:TapDBLoginTypeTapTap]; } - + // 开启 动态 [TDSInitializer enableMoment]; [TDSMomentSdk setDelegate:self]; } - - #pragma mark - 登录相关 /** 登录 */ -- (void) taptapLogin:(UIButton *) button { - +- (void)taptapLogin:(UIButton *)button { [TapLoginHelper startTapLogin:@[@"public_profile"]]; } - /** 登出 */ -- (void) taptapLogout:(UIButton *) button { +- (void)taptapLogout:(UIButton *)button { [TapLoginHelper logout]; } +/** + 获取用户信息 + */ +- (void)fetchProfile:(UIButton *)button { + [TapLoginHelper fetchProfileForCurrentAccessToken:^(TTSDKProfile *_Nonnull profile, NSError *_Nonnull error) { + if (error) { + NSLog(@"获取用户信息失败 %@", error); + } else { + NSLog(@"获取用户信息成功 %@", profile.name); + } + }]; +} - (void)onLoginSuccess:(TTSDKAccessToken *)token { NSLog(@"Login success %@", [token toJsonString]); - + // set user [TapDB setUser:[TTSDKProfile currentProfile].openid loginType:TapDBLoginTypeTapTap]; } - (void)onLoginCancel { NSLog(@"Login cancel"); - } -- (void)onLoginError:(AccountGlobalError *)error{ - if (error != nil ) { - if ([LOGIN_ERROR_ACCESS_DENIED isEqualToString:error.error] || [LOGIN_ERROR_FORBIDDEN isEqual:error.error]) { +- (void)onLoginError:(AccountGlobalError *)error { + if (error != nil) { + if ([LOGIN_ERROR_ACCESS_DENIED isEqualToString:error.error] || [LOGIN_ERROR_FORBIDDEN isEqualToString:error.error] || [LOGIN_ERROR_INVALID_GRANT isEqualToString:error.error]) { NSLog(@"当前 TOKEN已经失效, 需要提示用户重新执行 TapTap 登录流程"); } } NSLog(@"Login error %@", [error toJsonString]); } -#pragma mark - #pragma mark - 动态相关 /** 打开动态 */ -- (void) taptapMoment:(UIButton *) button { +- (void)taptapMoment:(UIButton *)button { TDSMomentConfig *momentConfig = [[TDSMomentConfig alloc] init]; momentConfig.orientation = TDSMomentOrientationDefault; [TDSMomentSdk openTapMomentWithConfig:momentConfig]; } - -- (void) taptapMomentRedPoint:(UIButton*) button { +- (void)taptapMomentRedPoint:(UIButton *)button { NSLog(@"小红点请求"); [TDSMomentSdk fetchNewMessage]; } - - (void)didChangeResultCode:(NSInteger)code msg:(NSString *)msg { - NSLog(@"didChangeResultCode %@ ,%d", msg, code); + NSLog(@"didChangeResultCode %@ ,%ld", msg, (long)code); } -#pragma mark - +#pragma mark - DB相关 + +- (void)toTapDB:(UIButton *)button { + TapDBViewController *dbController = [TapDBViewController new]; + [self presentViewController:dbController animated:YES completion:nil]; +} @end