```Objective-c
//版本 v2
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//store/pref/ASHPrefStore.h
@
interface ASHPrefStore : NSObject
+ (BOOL)haveReadBroadcastTip;
+ (void)setHaveReadBroadcastTip;
@
end//store/pref/ASHPrefStore.m
@
implementation ASHPrefStore
static NSString *const kHaveReadBroadcastTipKey = @"haveReadBroadcastTip";
+ (BOOL)haveReadBroadcastTip
{
return [[NSUserDefaults standardUserDefaults] boolForKey:kHaveReadBroadcastTipKey];
}
+ (void)setHaveReadBroadcastTip
{
[[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:kHaveReadBroadcastTipKey];
}
@
end//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//network/ASHNetworkAPI.h
@
interface ASHNetworkAPI : NSObject
+ (void)sendBroadcastRequestWithMsg:(NSString *)msg successBlock:(void(^)(void))successBlock failureBlock:(void(^)(NSError *error))failureBlock;
@
end//network/ASHNetworkAPI.m
@
implementation ASHNetworkAPI
+ (void)sendBroadcastRequestWithMsg:(NSString *)msg successBlock:(void(^)(void))successBlock failureBlock:(void(^)(NSError *error))failureBlock
{
//send request
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
!successBlock ?: successBlock();
});
}
@
end//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//utils/ui/ASHAlertUtils.h
@
interface ASHAlertUtils : NSObject
+ (void)showAlertWithController:(UIViewController *)controller msg:(NSString *)msg callback:(void(^)(void))callback;
+ (void)showEditAlertWithController:(UIViewController *)controller title:(NSString *)title placeholder:(NSString *)placeholder callback:(void(^)(NSString *content))callback;
@
end//utils/ui/ASHAlertUtils.m
@
implementation ASHAlertUtils
#pragma mark - utils
+ (void)showAlertWithController:(UIViewController *)controller msg:(NSString *)msg callback:(void(^)(void))callback
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:msg preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
!callback ?: callback();
}]];
[controller presentViewController:alert animated:YES completion:nil];
}
+ (void)showEditAlertWithController:(UIViewController *)controller title:(NSString *)title placeholder:(NSString *)placeholder callback:(void(^)(NSString *content))callback
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = placeholder;
}];
[alertController addAction:[UIAlertAction actionWithTitle:@"发送" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *content = [[alertController textFields][0] text];
!callback ?: callback(content);
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"Canelled");
}]];
[controller presentViewController:alertController animated:YES completion:nil];
}
@
end//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//macro.h
#ifndef weakify
#if __has_feature(objc_arc)
#define weakify( x ) autoreleasepool{} __weak __typeof__(x) __weak_##x##__ = x;
#else // #if __has_feature(objc_arc)
#define weakify( x ) autoreleasepool{} __block __typeof__(x) __block_##x##__ = x;
#endif // #if __has_feature(objc_arc)
#endif // #ifndef weakify
#ifndef normalize
#if __has_feature(objc_arc)
#define normalize( x ) try{} @
finally{} __typeof__(x) x = __weak_##x##__;
#else // #if __has_feature(objc_arc)
#define normalize( x ) try{} @
finally{} __typeof__(x) x = __block_##x##__;
#endif // #if __has_feature(objc_arc)
#endif // #ifndef @
normalize//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//controller/ASHTempViewController.m
@
implementation ASHTempViewController
#pragma mark - app logic
- (void)sendBroadcastWithSuccessBlock:(void (^)(void))successBlock
{
@
weakify(self)
void (^showSendBroadcast) (void) = ^{
[ASHAlertUtils showEditAlertWithController:self title:@"发送广播'" placeholder:@"说点什么..." callback:^(NSString *content) {
[ASHNetworkAPI sendBroadcastRequestWithMsg:content successBlock:successBlock failureBlock:^(NSError *error) {
@
normalize(self)
!self ?: [ASHAlertUtils showAlertWithController:self msg:error.localizedDescription callback:nil];
}];
}];
};
if ([ASHPrefStore haveReadBroadcastTip]) {
showSendBroadcast();
} else {
NSString *title = @"1、发布广播,全服玩家都可以看到;\n2、禁止发布反动、政治、色情、辱骂、广告等不良言论,否则将会遭到删除、封号处理。";
[ASHAlertUtils showAlertWithController:self msg:title callback:^{
[ASHPrefStore setHaveReadBroadcastTip];
showSendBroadcast();
}];
}
}
```