iOS 开发入门 - 快速编写一个验证密码弹框动态库 dylib

前言

前面的文章 iOS 开发入门 - 快速编写一个弹框动态库 dylib 已经写了安装 Xcode、Xcode 命令行工具、Homebrew、Idid、TheOS、MonkeyDev等、这次就跳过这些流程了、没看过的点击查看

首先打开Xcode 创建一个新项目

设置最低运行系统

暂时用不着、 直接注释 

创建新文件

打开新创建的文件 并导入 UIKit 框架 

#import <UIKit/UIKit.h>

使用 __attribute__((constructor)) 让程序加载时自动调用这个函数,这种构造函数在程序启动时会被自动执行,无需显式调用。

static void __attribute__((constructor)) tests(void) {

}

使用 Grand Central Dispatch (GCD) 在程序中将任务异步派发到主队列执行

dispatch_async(dispatch_get_main_queue(), ^{

});

创建一个 passwordAlert 静态变量来存储 UIAlertController 对象 

static UIAlertController *passwordAlert;

赋值 passwordAlert 静态变量创建一个新的弹框

passwordAlert = [UIAlertController alertControllerWithTitle:@"验证密码" message:@"请输入密码完成验证" preferredStyle:UIAlertControllerStyleAlert];
[passwordAlert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    textField.placeholder = @"请在此处输入密码后提交";
    textField.secureTextEntry = YES;
}];

添加一个取消按钮【关闭软件】

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"关闭软件" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
     exit(0);
}];
[passwordAlert addAction:cancelAction];

添加一个验证按钮【验证密码】

UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"验证密码" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    NSString *inputPassword = passwordAlert.textFields.firstObject.text;
    if ([inputPassword isEqualToString:@"123456"]) {
        [passwordAlert dismissViewControllerAnimated:YES completion:nil];
    } else {
        if (inputPassword.length == 0) {
            passwordAlert.message = @"请输入密码完成验证";
            UIViewController *rootViewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
            [rootViewController presentViewController:passwordAlert animated:YES completion:nil];
        } else {
            passwordAlert.message = @"您输入的密码不正确";
            UIViewController *rootViewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
            [rootViewController presentViewController:passwordAlert animated:YES completion:nil];
        }
    }
}];
[passwordAlert addAction:confirmAction];

 以上验证密码逻辑 当输入密码为 123456 时才会关闭弹框 反之提示

 最后 通过获取应用的根视图控制器来立即显示 passwordAlert 弹框

UIViewController *rootViewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
[rootViewController presentViewController:passwordAlert animated:YES completion:nil];

可能发生问题:在软件启动时立即弹框可能会出现不弹框的问题 出现此问题可以使用最简单粗暴的 dispatch_after 方法来延迟弹框呈现 比如延迟 2 秒 下面为例子

UIViewController *rootViewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [rootViewController presentViewController:passwordAlert animated:YES completion:nil];
});

 

完工、附带效果图*3 感谢您花费数分钟或数秒钟的时间观看此文章

补充一下

上篇文章 iOS 开发入门 - 快速编写一个弹框动态库 dylib 忘记写 Code Signing Identity 设置了

写这篇文章编译的时候比较有空、所以自己编译注入了下测试效果、结果发现了[dog head][狗头保命]

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
iOS软件弹窗dylib编写是一种在iOS平台上开发和使用动态链接库的方法。动态链接库(Dynamic Library)是一种共享库,可以被多个应用程序同时使用,以提供特定功能或服务。 编写iOS软件弹窗dylib库需要以下步骤: 1. 创建一个新的Xcode项目。选择"Framework & Library"中的"Dynamic Library"模板。 2. 在项目中添加弹窗逻辑的代码,可以使用Objective-C或Swift语言来编写。 3. 将编写的逻辑封装为一个独立的类或函数,并在适当的位置调用。 4. 将编写的代码编译为dylib库,这可以使用Xcode的"Build"功能自动完成。 5. 导出dylib库文件,可以将其复制到其他项目中使用。 使用iOS软件弹窗dylib库的方法是: 1. 在需要使用弹窗功能的iOS应用项目中,导入dylib库文件。可以通过将dylib库文件直接拖拽到Xcode项目中来实现。 2. 在应用程序的源代码中,添加对dylib库的引用和使用。可以使用Objective-C的`dlopen`函数或Swift的`dyld`模块来加载和使用dylib库。 3. 根据dylib库中提供的API,使用需要的弹窗功能。 需要注意的是,dylib库的使用需要满足苹果的开发者政策和审核要求。在使用dylib库时,应确保遵循相关规定,并尽可能保证库文件的安全性和稳定性。 总结来说,iOS软件弹窗dylib编写是一种在iOS平台上开发和使用独立动态库的方法。需要在Xcode中创建项目、编写弹窗逻辑、编译为dylib库,然后在其他应用项目中导入并使用该库。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

极速网络v-team.cn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值