配置隐私协议 - iOS
根据苹果隐私协议新规的推出,要求所有应用包含隐私保护协议,故为此在 App 中添加了如下隐私协议模块.
首次安装 App 的情况下默认调用隐私协议模块展示相关信息一次,当用户点击同意按钮后,从此不再执行该模块方法.
具体 code 如下:
一.声明(.h)
/*
隐私协议
*/
#import <Foundation/Foundation.h> @interface PrivacyAgreement : NSObject + (instancetype)shareInstance; @end
二.实现(.m)
#import "PrivacyAgreement.h" /** 获取沙盒 Document 路径*/
#define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
#define kKeyWindow [UIApplication sharedApplication].keyWindow #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height) #define LocalName_ProjectConfig @"ProjectConfigInfo.plist" // 本地存储路径设置_文件名称
#define LocalPath_ProjectConfig @"Project/ProjectConfigInfo/" // 本地存储路径设置_文件路径
#define PrivacyAgreementState @"PrivacyAgreementState" @interface PrivacyAgreement () <WKNavigationDelegate, WKUIDelegate> @property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UIButton *btnAgree;
@property (nonatomic, strong) WKWebView *webView; @end @implementation PrivacyAgreement + (instancetype)shareInstance {
static PrivacyAgreement *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[PrivacyAgreement alloc] init];
}); return instance;
} - (instancetype)init {
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) { NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:[kDocumentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@", LocalPath_ProjectConfig, LocalName_ProjectConfig]]]) {
NSMutableDictionary *configInfo = [NSMutableDictionary dictionaryWithContentsOfFile:[kDocumentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@", LocalPath_ProjectConfig, LocalName_ProjectConfig]]];
if ([[configInfo objectForKey:@"PrivacyAgreementState"] isEqualToString:@"PrivacyAgreementState"]) {} else {
// Show Privacy AgreementState View
[self showPrivacyAgreementStateView];
}
}
else {
// Show Privacy AgreementState View
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self showPrivacyAgreementStateView];
});
}
}];
} return self;
} /**
渲染隐私协议视图
*/
- (void)showPrivacyAgreementStateView {
[kKeyWindow addSubview:self.backgroundView];
[self webView];
[self.backgroundView addSubview:self.btnAgree];
} #pragma mark - ************************************************ UI
- (UIView *)backgroundView {
if (!_backgroundView) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
view.backgroundColor = [UIColor whiteColor];
view.userInteractionEnabled = YES; _backgroundView = view;
}
return _backgroundView;
} /**
WebView 设置相关 其中包含加载方式(本地文件 & 网络请求)
@return 当前控件
*/
- (WKWebView *)webView {
if (!_webView) {
NSError *error;
// 本地 url 地址设置
NSURL *URLBase = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSString *URLAgreement = [[NSBundle mainBundle] pathForResource:@"agreement" ofType:@"html"];
NSString *html = [NSString stringWithContentsOfFile:URLAgreement
encoding:NSUTF8StringEncoding
error:&error]; WKWebViewConfiguration *webConfig = [[WKWebViewConfiguration alloc] init];
webConfig.preferences = [[WKPreferences alloc] init];
webConfig.preferences.javaScriptEnabled = YES;
webConfig.preferences.javaScriptCanOpenWindowsAutomatically = NO; WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(10, 70, SCREEN_WIDTH - 20, SCREEN_HEIGHT - 150)
configuration:webConfig];
webView.navigationDelegate = self;
webView.UIDelegate = self;
#pragma mark - 本地 html 文件加载方式
[webView loadHTMLString:html baseURL:URLBase];
#pragma mark - 网络请求加载方式
// NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@""]// 隐私协议的 url 地址
// cachePolicy:NSURLRequestReloadIgnoringCacheData
// timeoutInterval:3.0];
// [webView loadRequest:request]; [_backgroundView addSubview:webView]; _webView = webView;
}
return _webView;
} - (UIButton *)btnAgree {
if (!_btnAgree) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(CGRectGetMidX(_webView.frame) - 50, CGRectGetMaxY(_webView.frame) + 10, 100, 44);
btn.backgroundColor = [UIColor whiteColor];
[btn setTitle:@"同意" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; _btnAgree = btn;
}
return _btnAgree;
} - (void)btnClick {
NSMutableDictionary *configInfo = [NSMutableDictionary dictionaryWithContentsOfFile:[kDocumentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@", LocalPath_ProjectConfig, LocalName_ProjectConfig]]];
[configInfo setValue:PrivacyAgreementState forKey:@"PrivacyAgreementState"];
InsertObjectToLocalPlistFile(configInfo, LocalName_ProjectConfig, LocalPath_ProjectConfig);
[_backgroundView removeFromSuperview];
} @end
注:如上方法中使用的本地加载的方式,若需要使用网络请求的方式,详见具体 code 中的注释部分.
三.方法调用
在需要的地方引用该头文件并调用接口方法即可,一般在 appdelegate 中.
[PrivacyAgreement shareInstance];
四.方法类中相关封装的方法
4.1.点击事件中文件写入本地的方法
/**
插入对象至本地 plist 文件
@param dataSource 数据源
@param fileName 文件名称
@param filePath 文件路径
*/
void InsertObjectToLocalPlistFile(NSMutableDictionary *dataSource, NSString *fileName, NSString *filePath) {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docPath = [kDocumentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@", filePath, fileName]];
if ([fileManager fileExistsAtPath:docPath]) {// 文件存在
NSLog(@"本地 plist 文件 --- 存在");
[dataSource writeToFile:[[kDocumentPath stringByAppendingPathComponent:filePath] stringByAppendingPathComponent:fileName] atomically:YES];
}
else {// 文件不存在
NSLog(@"本地 plist 文件 --- 不存在");
CreateLocalPlistFile(dataSource, fileName, filePath);
}
}
以上便是此次分享的内容,还望能对大家有所帮助,谢谢!
配置隐私协议 - iOS的更多相关文章
- React Native之配置URL Scheme(iOS Android)
React Native之配置URL Scheme(iOS Android) 一,需求分析 1.1,需要在网站中打开/唤起app,或其他app中打开app,则需要设置URL Scheme.比如微信的是 ...
- iOS- Apple零配置网络协议Bonjour的使用?
1.前言 这段时间为了解决公司App的网络离线需求,做了个Apple推出的零配置网络协议Bonjour的Test,主要是为了解决iOS设备的IP获取,之前是可以使用socket的广播来实现,但是使用A ...
- 【转】Linux下nginx配置https协议访问的方法
一.配置nginx支持https协议访问,需要在编译安装nginx的时候添加相应的模块--with-http_ssl_module 查看nginx编译参数:/usr/local/nginx/sbin/ ...
- 在linux下的apache配置https协议,开启ssl连接
环境:linux 配置https协议,需要2大步骤: 一.生成服务器证书 1.安装openssl软件 yum install -y openssl mod_ssl 2.生成服务器私匙,生成server ...
- 单点登录CAS使用记(一):前期准备以及为CAS-Server配置SSL协议
知识点: SSO:单点登录(Single Sign On),是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. CAS:耶 ...
- tomcat配置https协议
tomcat配置https协议 1.找到本地jdk底下的bin目录,bin目录底下存在文件keytool.exe(在bin目录下空白处,按住shift右击,打开命令窗口,如下图) 2.在cmd的命令窗 ...
- Tomcat8配置Https协议,Tomcat配置Https安全访问,Tomcat Https配置
Tomcat8配置Https协议,Tomcat配置Https安全访问,Tomcat Https配置 ============================== ©Copyright 蕃薯耀 2017 ...
- 配置Tomcat使用https协议(配置SSL协议)
配置Tomcat使用https协议(配置SSL协议) 2014-01-20 16:38 58915人阅读 评论(3) 收藏 举报 转载地址:http://ln-ydc.iteye.com/blog/1 ...
- Tomcat配置https协议访问
Tomcat9配置https协议访问: https://blog.csdn.net/weixin_42273374/article/details/81010203 配置Tomcat使用https协议 ...
随机推荐
- continue break return
ontinue: 可以让程序跳过,continue关键字之后的语句,回到while循环的第一行命令 break: 让程序完全跳出循环,结束while循环的执行 return: 从查询或过程中无条件退出 ...
- JavaScript对象 创建对象(一)
创建对象 --以下内容来自JavaScript高级程序设计 工厂模式 用函数来封装以特定接口创建对象的细节. function createPerson(name, age, job){ var o ...
- 今天研究Unity Ioc 框架
今天研究Unity Ioc 框架,被自己坑了两个多小时. 运行就报错,反反复复检查了很多次,配置文件,代码都没有问题,也从新写了好几遍. 最后仔细看报错消息才知道,config文件没有生成到目录……… ...
- ubuntu下安装MySQL8.0
为了一劳永逸不每次都到处找资料,花了一下午时间做了这些.其中大部分是根据官方手册来的,后面部分谢谢大佬的帮助,超开心. 一.首先,将MySQL APT存储库添加到系统的软件存储库列表中 1.转到htt ...
- linux基础-linux和unix的区别
有时候我们对天天使用的Linux指令,只知道怎么用,却分不清概念用法区别,我觉得很有必要整理整理大家熟视无睹的一些linux概念区别. 首先说说unix和linux的区别: linux和unix的最大 ...
- stark——快速过滤list_filter
一.获取过滤字段 1.给自定义配置类配置list_filter app01/stark.py: class BookConfig(ModelStark): list_display = [" ...
- 【代码笔记】Java基础:Java的方法和类
面向过程与面向对象都是我们编程中,编写程序的一种思维方式.例如:公司打扫卫生(擦玻璃.扫地.拖地.倒垃圾等), 按照面向过程的程序设计方式会思考“打扫卫生我该怎么做,然后一件件的完成”,最后把公司卫生 ...
- Infor SyteLine创建一个数据维护窗口
上次有在SyteLine解决一个问题<匹配与显示中文说明> http://www.cnblogs.com/insus/p/3396541.html .这些数据需要数据库管理员在数据库才能维 ...
- 3D开源推荐:全球卫星地图 Esri-Satellite-Map
演示网址:http://richiecarmichael.github.io/sat/index.html 开源网址:https://github.com/richiecarmichael/Esri- ...
- wxpython 设置鼠标样式
鼠标指针被设置为放大镜样式.可用的鼠标指针样式有: wx.CURSOR_ARROWwx.CURSOR_RIGHT_ARROWwx.CURSOR_BLANKwx.CURSOR_BULLSEYEwx.CU ...