@implementation NSString (URL)

本文详细介绍了如何使用Objective-C实现字符串的URL编码和UTF8编码,包括编码规则、实现方法及示例代码。

- (NSString*)URLencodeWithEncoding:(NSStringEncoding)stringEncoding {

//! @ $ & ( ) = + ~ ` ; ' : , / ?

//%21%40%24%26%28%29%3D%2B%7E%60%3B%27%3A%2C%2F%3F

NSArray *escapeChars = [NSArray arrayWithObjects:@";" , @"/" , @"?" , @":" ,

@"@" , @"&" , @"=" , @"+" , @"$" , @"," ,

@"!", @"'", @"(", @")", @"*", nil];

NSArray *replaceChars = [NSArray arrayWithObjects:@"%3B" , @"%2F", @"%3F" , @"%3A" ,

@"%40" , @"%26" , @"%3D" , @"%2B" , @"%24" , @"%2C" ,

@"%21", @"%27", @"%28", @"%29", @"%2A", nil];

int len = [escapeChars count];

NSMutableString *temp = [[self stringByAddingPercentEscapesUsingEncoding:stringEncoding] mutableCopy];

int i;

for (i = 0; i < len; i++) {

[temp replaceOccurrencesOfString:[escapeChars objectAtIndex:i]

withString:[replaceChars objectAtIndex:i]

options:NSLiteralSearch

range:NSMakeRange(0, [temp length])];

}

NSString *outStr = [NSString stringWithString: temp];

[temp release];

return outStr;

}

-(NSString*)URLencodeWithEncodingUTF8{

return [self URLencodeWithEncoding:NSUTF8StringEncoding];

}

#import <UIKit/UIKit.h> #import <ReactiveObjC/ReactiveObjC.h> NS_ASSUME_NONNULL_BEGIN #pragma mark - Model @interface StoryScene : NSObject @property (nonatomic, copy) NSString *sceneTitle; @property (nonatomic, copy) NSString *visualDescription; @property (nonatomic, strong) NSArray<NSString *> *symbolicElements; @end @implementation StoryScene @end @interface DigitalTool : NSObject @property (nonatomic, copy) NSString *toolName; @property (nonatomic, copy) NSString *usageDescription; @property (nonatomic, strong) NSArray<NSString *> *appliedScenes; @end @implementation DigitalTool @end @interface InteractionPlan : NSObject @property (nonatomic, copy) NSString *platform; @property (nonatomic, strong) NSArray<NSString *> *engagementMethods; @end @implementation InteractionPlan @end #pragma mark - ViewModel @interface StoryViewModel : NSObject @property (nonatomic, strong) NSArray<StoryScene *> *scenes; @property (nonatomic, strong) RACCommand *loadScenesCommand; @end @implementation StoryViewModel - (instancetype)init { self = [super init]; if (self) { @weakify(self); _loadScenesCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id _) { @strongify(self); return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [self fetchScenesFromService]; [subscriber sendCompleted]; return nil; }]; }]; } return self; } - (void)fetchScenesFromService { StoryScene *scene1 = [StoryScene new]; scene1.sceneTitle = @"黑暗中的微光"; scene1.visualDescription = @"黑白基调牢房,霉变米饭特写"; StoryScene *scene2 = [StoryScene new]; scene2.sceneTitle = @"铁窗下的课堂"; scene2.symbolicElements = @[@"树枝笔", @"棉灰墨", @"半截铅笔"]; _scenes = @[scene1, scene2]; } @end @interface ToolViewModel : NSObject @property (nonatomic, strong) NSArray<DigitalTool *> *tools; @property (nonatomic, strong) RACSubject *toolSelectedSubject; @end @implementation ToolViewModel - (instancetype)init { self = [super init]; if (self) { _toolSelectedSubject = [RACSubject subject]; [self setupTools]; } return self; } - (void)setupTools { DigitalTool *tool1 = [DigitalTool new]; tool1.toolName = @"PikaLabs"; tool1.usageDescription = @"水墨晕染风格动画"; DigitalTool *tool2 = [DigitalTool new]; tool2.toolName = @"Runway Gen-3"; tool2.usageDescription = @"动态分镜特效"; _tools = @[tool1, tool2]; } @end #pragma mark - View @interface SceneCell : UICollectionViewCell @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UIImageView *symbolImageView; - (void)configureWithScene:(StoryScene *)scene; @end @implementation SceneCell - (void)configureWithScene:(StoryScene *)scene { _titleLabel.text = scene.sceneTitle; _symbolImageView.image = [UIImage imageNamed:scene.symbolicElements.firstObject]; } @end @interface MainViewController : UIViewController @property (nonatomic, strong) StoryViewModel *storyVM; @property (nonatomic, strong) ToolViewModel *toolVM; @property (nonatomic, strong) UICollectionView *collectionView; @end @implementation MainViewController - (void)viewDidLoad { [super viewDidLoad]; [self setupCollectionView]; [self bindViewModels]; } - (void)setupCollectionView { UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new]; _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout]; [_collectionView registerClass:SceneCell.class forCellWithReuseIdentifier:@"SceneCell"]; [self.view addSubview:_collectionView]; } - (void)bindViewModels { @weakify(self); [[_storyVM.loadScenesCommand execute:nil] subscribeCompleted:^{ @strongify(self); [self.collectionView reloadData]; }]; [_toolVM.toolSelectedSubject subscribeNext:^(DigitalTool *tool) { NSLog(@"Selected tool: %@", tool.toolName); }]; } #pragma mark - UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return _storyVM.scenes.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { SceneCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SceneCell" forIndexPath:indexPath]; [cell configureWithScene:_storyVM.scenes[indexPath.item]]; return cell; } @end #pragma mark - Coordinator @interface AppCoordinator : NSObject @property (nonatomic, strong) UINavigationController *navigationController; - (void)start; @end @implementation AppCoordinator - (void)start { MainViewController *mainVC = [MainViewController new]; mainVC.storyVM = [StoryViewModel new]; mainVC.toolVM = [ToolViewModel new]; _navigationController = [[UINavigationController alloc] initWithRootViewController:mainVC]; } @end #pragma mark - AppDelegate @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (nonatomic, strong) UIWindow *window; @property (nonatomic, strong) AppCoordinator *coordinator; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { _window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds]; _coordinator = [AppCoordinator new]; [_coordinator start]; _window.rootViewController = _coordinator.navigationController; [_window makeKeyAndVisible]; return YES; } @end NS_ASSUME_NONNULL_END
08-23
@implementation AppDelegate - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options { BOOL handled; handled = [GIDSignIn.sharedInstance handleURL:url]; if (handled) { return YES; } // Handle other custom URL types. // If not handled by this app, return NO. return NO; } #pragma mark - GIDSignInDelegate - (void)sign_in:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error { if (error == nil) { NSString *googleIdToken = user.idToken; NSString *googleAccessToken = user.accessToken; // 回调 Unity 中的方法 [self sendLoginSuccessToUnity:googleIdToken accessToken:googleAccessToken]; } else { NSLog(@"Google 登录失败:%@", error.localizedDescription); } } - (void)sendLoginSuccessToUnity:(NSString *)googleIdToken accessToken:(NSString *)googleAccessToken { const char *idTokenCStr = [googleIdToken cStringUsingEncoding:NSUTF8StringEncoding]; const char *accessTokenCStr = [googleAccessToken cStringUsingEncoding:NSUTF8StringEncoding]; CallGoogleSignInMethod(idTokenCStr, accessTokenCStr); } void CallGoogleSignInMethod(const char* googleIdToken, const char* googleAccessToken) { UnitySendMessage("CallObj", "GoogleIosSgins", googleIdToken); // 第一次调用传递 googleIdToken UnitySendMessage("CallObj", "GoogleIosSgins", googleAccessToken); // 第二次调用传递 googleAccessToken } void UnityCallMethod() { GIDSignIn *signIn = [GIDSignIn sharedInstance]; signIn.delegate = self; // 确保当前类实现了 GIDSignInDelegate 协议[^2] signIn.uiDelegate = self; // 确保当前类实现了 GIDSignInUIDelegate 协议[^2] [signIn signIn]; // 调用 Google 登录界面 } @end如何修改
06-18
// // TSSUserDefaults.m // TPSecureStorage // // Created by CaiXin on 2018/4/27. // Copyright © 2018年 CaiXin. All rights reserved. // #import "TSSUserDefaults.h" #import "TSSAESCrypt.h" static TSSUserDefaults* lUserDefaults = nil; static dispatch_once_t onceToken; NSString* const TSSUserDefaultsEncryptedObjectKeyPrefix = @"[TSSUserDefaults-Encrypted]-["; NSString* const TSSUserDefaultsEncryptedObjectKeySuffix = @"]-[TSSUserDefaults-Encrypted]"; NSString* const TSSUserDefaultsUnencryptedObjectKeyPrefix = @"[TSSUserDefaults-Unencrypted]-["; NSString* const TSSUserDefaultsUnencryptedObjectKeySuffix = @"]-[TSSUserDefaults-Unencrypted]"; @interface TSSUserDefaults () @property (nonatomic, readwrite, strong) NSUserDefaults* userDefaults; @end @implementation TSSUserDefaults + (instancetype)standardUserDefaults { dispatch_once(&onceToken, ^{ lUserDefaults = [[self alloc] initWithUserDefaults:[NSUserDefaults standardUserDefaults]]; }); return lUserDefaults; } - (instancetype)initWithUserDefaults:(NSUserDefaults*)userDefaults { if (userDefaults == nil) { return nil; } self = [super init]; if (self) { self.userDefaults = userDefaults; } return self; } - (BOOL)synchronize { return [self.userDefaults synchronize]; } - (NSString*)encryptedObjectKeyForKey:(NSString*)key { return [NSString stringWithFormat:@"%@%@%@", TSSUserDefaultsEncryptedObjectKeyPrefix, key, TSSUserDefaultsEncryptedObjectKeySuffix]; } - (NSString*)unencryptedObjectKeyForKey:(NSString*)key { return [NSString stringWithFormat:@"%@%@%@", TSSUserDefaultsUnencryptedObjectKeyPrefix, key, TSSUserDefaultsUnencryptedObjectKeySuffix]; } - (id)objectForKey:(NSString *)defaultName { if (defaultName == nil) { return nil; } id obj = [self.userDefaults objectForKey:defaultName]; if (obj) { [self setObject:obj forKey:defaultName]; } else { NSData* encryptedData = [self.userDefaults objectForKey:[self encryptedObjectKeyForKey:defaultName]]; //有加密value if (encryptedData) { obj = [[TSSAESCrypt defaultCrypt] objectDecryptFromData:encryptedData]; } else { obj = [self.userDefaults objectForKey:[self unencryptedObjectKeyForKey:defaultName]]; } } return obj; } - (void)setObject:(id)value forKey:(NSString *)defaultName { [self setObject:value forKey:defaultName encrypted:YES]; } - (void)setObject:(nullable id)value forKey:(NSString *)defaultName encrypted:(BOOL)encrypted { if (defaultName == nil) { return; } NSString* newDefaultName = encrypted ? [self encryptedObjectKeyForKey:defaultName] : [self unencryptedObjectKeyForKey:defaultName]; NSString *keyToRemove = encrypted ? [self unencryptedObjectKeyForKey:defaultName] : [self encryptedObjectKeyForKey:defaultName]; [self.userDefaults removeObjectForKey:keyToRemove]; [self.userDefaults removeObjectForKey:defaultName]; if (value == nil) { [self.userDefaults removeObjectForKey:newDefaultName]; return; } if (encrypted) { NSData* encryptedData = [[TSSAESCrypt defaultCrypt] encryptWithObject:value]; if (encryptedData) { [self.userDefaults setObject:encryptedData forKey:newDefaultName]; } } else { [self.userDefaults setObject:value forKey:newDefaultName]; } } - (void)removeObjectForKey:(NSString *)defaultName { [self setObject:nil forKey:defaultName]; } @end @implementation TSSUserDefaults (Extend) - (NSInteger)integerForKey:(NSString *)defaultName { NSObject* obj = [self objectForKey:defaultName]; if ([obj isKindOfClass:[NSNumber class]]) { return ((NSNumber*)obj).integerValue; } else if ([obj isKindOfClass:[NSString class]]) { return ((NSString*)obj).integerValue; } return 0; } - (float)floatForKey:(NSString *)defaultName { NSNumber* num = [self objectForKey:defaultName]; if ([num isKindOfClass:[NSNumber class]]) { return num.floatValue; } return 0; } - (double)doubleForKey:(NSString *)defaultName { NSNumber* num = [self objectForKey:defaultName]; if ([num isKindOfClass:[NSNumber class]]) { return num.doubleValue; } return 0; } - (BOOL)boolForKey:(NSString *)defaultName { NSNumber* num = [self objectForKey:defaultName]; if ([num isKindOfClass:[NSNumber class]]) { return num.boolValue; } return 0; } - (NSString *)stringForKey:(NSString *)defaultName { NSString* str = [self objectForKey:defaultName]; if ([str isKindOfClass:[NSString class]]) { return str; } return nil; } - (NSData *)dataForKey:(NSString *)defaultName { NSData* obj = [self objectForKey:defaultName]; if ([obj isKindOfClass:[NSData class]]) { return obj; } return nil; } - (NSArray *)arrayForKey:(NSString *)defaultName { NSArray* obj = [self objectForKey:defaultName]; if ([obj isKindOfClass:[NSArray class]]) { return obj; } return nil; } - (NSDictionary *)dictionaryForKey:(NSString *)defaultName { NSDictionary* obj = [self objectForKey:defaultName]; if ([obj isKindOfClass:[NSDictionary class]]) { return obj; } return nil; } - (NSURL *)URLForKey:(NSString *)defaultName { if (defaultName == nil) { return nil; } NSURL* obj = [self.userDefaults URLForKey:defaultName]; if (obj) { [self setObject:obj forKey:defaultName]; } else { obj = [self objectForKey:defaultName]; if (![obj isKindOfClass:[NSURL class]]) { obj = nil; } } return obj; } - (void)setInteger:(NSInteger)value forKey:(NSString *)defaultName { [self setInteger:value forKey:defaultName encrypted:YES]; } - (void)setInteger:(NSInteger)value forKey:(NSString *)defaultName encrypted:(BOOL)encrypted { [self setObject:@(value) forKey:defaultName encrypted:encrypted]; } - (void)setFloat:(float)value forKey:(NSString *)defaultName { [self setFloat:value forKey:defaultName encrypted:YES]; } - (void)setFloat:(float)value forKey:(NSString *)defaultName encrypted:(BOOL)encrypted { [self setObject:@(value) forKey:defaultName encrypted:encrypted]; } - (void)setDouble:(double)value forKey:(NSString *)defaultName { [self setDouble:value forKey:defaultName encrypted:YES]; } - (void)setDouble:(double)value forKey:(NSString *)defaultName encrypted:(BOOL)encrypted { [self setObject:@(value) forKey:defaultName encrypted:encrypted]; } - (void)setBool:(BOOL)value forKey:(NSString *)defaultName { [self setBool:value forKey:defaultName encrypted:YES]; } - (void)setBool:(BOOL)value forKey:(NSString *)defaultName encrypted:(BOOL)encrypted { [self setObject:@(value) forKey:defaultName encrypted:encrypted]; } - (void)setURL:(NSURL *)value forKey:(NSString *)defaultName { [self setURL:value forKey:defaultName encrypted:YES]; } - (void)setURL:(NSURL *)value forKey:(NSString *)defaultName encrypted:(BOOL)encrypted { [self setObject:value.copy forKey:defaultName encrypted:encrypted]; } @end extension TSSUserDefaults { func optionalBoolForKey(_ key: String) -> Bool? { let value = self.string(forKey: key) if value == nil { return nil } else if value == optionalBoolValueOn { return true } else { return false } } func setOptionalBool(key: String, value: Bool?) { if let value = value { self.setObject(value ? optionalBoolValueOn : optionalBoolValueOff, forKey: key) } else { self.setObject(nil, forKey: key) } } } 这是userdefaults的所有代码
最新发布
09-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值