refactor(ios): 将 ShutoApi 实现移至头文件并改用属性

重构 iOS 端的 ShutoApi 实现,将类定义移至头文件
使用属性替代实例变量以提高代码可读性
移除冗余的 isIonicReady 方法
This commit is contained in:
2026-01-22 10:04:31 +08:00
parent a54e1f7b04
commit a570be7cbe
3 changed files with 41 additions and 50 deletions

View File

@@ -22,6 +22,7 @@
<param name="ios-package" value="ShutoApi" />
</feature>
</config-file>
<source-file src="src/ios/ShutoApi.h" />
<source-file src="src/ios/ShutoApi.m" />
<source-file src="src/ios/ShutoEventManager.h" />
<source-file src="src/ios/ShutoEventManager.m" />

24
src/ios/ShutoApi.h Normal file
View File

@@ -0,0 +1,24 @@
#import <Cordova/CDV.h>
#import <WebKit/WebKit.h>
@protocol ShutoApiProtocol <NSObject>
- (void)fireEvent:(NSString*)type parameters:(NSDictionary*)parameters;
- (void)fireEventWithCallback:(NSString*)eventName parameters:(NSDictionary*)parameters callback:(void (^)(NSDictionary* result, NSError* error))callback;
@end
@interface ShutoApi : CDVPlugin <ShutoApiProtocol>
@property (nonatomic, copy) NSString* eventCallbackId;
@property (nonatomic, strong) NSDictionary* userInfo;
@property (nonatomic, strong) NSMutableDictionary* eventCallbacks;
@property (nonatomic, assign) BOOL isIonicReady;
- (void)close:(CDVInvokedUrlCommand*)command;
- (void)registerEvent:(CDVInvokedUrlCommand*)command;
- (void)getUserInfo:(CDVInvokedUrlCommand*)command;
- (void)setUserInfo:(NSDictionary*)userInfo;
- (void)navigateToRoute:(NSString*)route parameters:(NSDictionary*)parameters;
- (void)fireEvent:(NSString*)type parameters:(NSDictionary*)parameters;
- (void)fireEventWithCallback:(NSString*)eventName parameters:(NSDictionary*)parameters callback:(void (^)(NSDictionary* result, NSError* error))callback;
- (void)eventCallback:(CDVInvokedUrlCommand*)command;
- (void)ionicReady:(CDVInvokedUrlCommand*)command;
@end

View File

@@ -1,42 +1,13 @@
/********* ShutoApi.m Cordova Plugin Implementation *******/
#import <Cordova/CDV.h>
#import <WebKit/WebKit.h>
#import "ShutoApi.h"
#import "SGGC_Log.h"
#import "ShutoEventManager.h"
@protocol ShutoApiProtocol <NSObject>
- (void)fireEvent:(NSString*)type parameters:(NSDictionary*)parameters;
- (void)fireEventWithCallback:(NSString*)eventName parameters:(NSDictionary*)parameters callback:(void (^)(NSDictionary* result, NSError* error))callback;
@end
@interface ShutoApi : CDVPlugin <ShutoApiProtocol> {
// ID
NSString* _eventCallbackId;
//
NSDictionary* _userInfo;
// ID
NSMutableDictionary* _eventCallbacks;
//
BOOL _isIonicReady;
}
- (void)close:(CDVInvokedUrlCommand*)command;
- (void)registerEvent:(CDVInvokedUrlCommand*)command;
- (void)getUserInfo:(CDVInvokedUrlCommand*)command;
- (void)setUserInfo:(CDVInvokedUrlCommand*)command;
- (void)navigateToRoute:(NSString*)route parameters:(NSDictionary*)parameters;
- (void)fireEvent:(NSString*)type parameters:(NSDictionary*)parameters;
- (void)fireEventWithCallback:(NSString*)eventName parameters:(NSDictionary*)parameters callback:(void (^)(NSDictionary* result, NSError* error))callback;
- (void)eventCallback:(CDVInvokedUrlCommand*)command;
- (void)ionicReady:(CDVInvokedUrlCommand*)command;
- (BOOL)isIonicReady;
@end
@implementation ShutoApi
- (void)pluginInitialize {
_eventCallbacks = [[NSMutableDictionary alloc] init];
self.eventCallbacks = [[NSMutableDictionary alloc] init];
}
- (void)close:(CDVInvokedUrlCommand*)command
@@ -81,7 +52,7 @@
- (void)registerEvent:(CDVInvokedUrlCommand*)command
{
// ID便
_eventCallbackId = command.callbackId;
self.eventCallbackId = command.callbackId;
//
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
@@ -93,8 +64,8 @@
{
//
CDVPluginResult* pluginResult;
if (_userInfo) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:_userInfo];
if (self.userInfo) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:self.userInfo];
} else {
//
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:nil];
@@ -106,7 +77,7 @@
{
//
if (userInfo) {
_userInfo = userInfo;
self.userInfo = userInfo;
[SGGC_Log log:@"SGGC_CDVFile" message:(@"ShutoApi: User info set successfully: %@", userInfo)];
} else {
[SGGC_Log log:@"SGGC_CDVFile" message:(@"ShutoApi: Invalid user info provided")];
@@ -127,7 +98,7 @@
//
- (void)fireEvent:(NSString*)type parameters:(NSDictionary*)parameters {
if (!_eventCallbackId) {
if (!self.eventCallbackId) {
[SGGC_Log log:@"SGGC_CDVFile" message:(@"ShutoApi: No event callback registered")];
return;
}
@@ -144,14 +115,14 @@
//
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:eventData];
[pluginResult setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:pluginResult callbackId:_eventCallbackId];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.eventCallbackId];
}
//
- (void)fireEventWithCallback:(NSString*)eventName parameters:(NSDictionary*)parameters callback:(void (^)(NSDictionary* result, NSError* error))callback {
[SGGC_Log log:@"SGGC_CDVFile" message:[NSString stringWithFormat:@"ShutoApi: fireEventWithCallback called, eventName=%@, parameters=%@", eventName, parameters]];
if (!_eventCallbackId) {
if (!self.eventCallbackId) {
[SGGC_Log log:@"SGGC_CDVFile" message:@"ShutoApi: No event callback registered"];
if (callback) callback(nil, [NSError errorWithDomain:@"ShutoApi" code:-1 userInfo:@{NSLocalizedDescriptionKey:@"No event callback registered"}]);
return;
@@ -161,8 +132,8 @@
[SGGC_Log log:@"SGGC_CDVFile" message:[NSString stringWithFormat:@"ShutoApi: Generated callbackId=%@", callbackId]];
if (callback) {
_eventCallbacks[callbackId] = callback;
[SGGC_Log log:@"SGGC_CDVFile" message:[NSString stringWithFormat:@"ShutoApi: Callback saved, _eventCallbacks count=%lu", (unsigned long)_eventCallbacks.count]];
self.eventCallbacks[callbackId] = callback;
[SGGC_Log log:@"SGGC_CDVFile" message:[NSString stringWithFormat:@"ShutoApi: Callback saved, eventCallbacks count=%lu", (unsigned long)self.eventCallbacks.count]];
}
NSMutableDictionary* eventData = [NSMutableDictionary dictionary];
@@ -177,7 +148,7 @@
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:eventData];
[pluginResult setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:pluginResult callbackId:_eventCallbackId];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.eventCallbackId];
}
//
@@ -189,9 +160,9 @@
NSString* errorMessage = command.arguments[2];
[SGGC_Log log:@"SGGC_CDVFile" message:[NSString stringWithFormat:@"ShutoApi: callbackId=%@, result=%@, errorMessage=%@", callbackId, result, errorMessage]];
[SGGC_Log log:@"SGGC_CDVFile" message:[NSString stringWithFormat:@"ShutoApi: _eventCallbacks count=%lu", (unsigned long)_eventCallbacks.count]];
[SGGC_Log log:@"SGGC_CDVFile" message:[NSString stringWithFormat:@"ShutoApi: eventCallbacks count=%lu", (unsigned long)self.eventCallbacks.count]];
void (^callback)(NSDictionary* result, NSError* error) = _eventCallbacks[callbackId];
void (^callback)(NSDictionary* result, NSError* error) = self.eventCallbacks[callbackId];
if (callback) {
[SGGC_Log log:@"SGGC_CDVFile" message:[NSString stringWithFormat:@"ShutoApi: Found callback for callbackId=%@", callbackId]];
@@ -203,7 +174,7 @@
callback(result, error);
[_eventCallbacks removeObjectForKey:callbackId];
[self.eventCallbacks removeObjectForKey:callbackId];
[SGGC_Log log:@"SGGC_CDVFile" message:@"ShutoApi: Callback executed and removed"];
} else {
@@ -216,7 +187,7 @@
- (void)ionicReady:(CDVInvokedUrlCommand*)command
{
_isIonicReady = YES;
self.isIonicReady = YES;
[SGGC_Log log:@"SGGC_CDVFile" message:@"ShutoApi: Ionic is ready"];
[[ShutoEventManager sharedInstance] fireEvent:@"ionicReady" data:nil];
@@ -225,9 +196,4 @@
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (BOOL)isIonicReady
{
return _isIonicReady;
}
@end