配置隐私协议 - 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);
    }
}

 

 

以上便是此次分享的内容,还望能对大家有所帮助,谢谢!

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Flutter中,如果你使用的是百度定位SDK的Flutter插件,则需要按照以下步骤执行: 1. 在iOS项目中打开Info.plist文件,添加以下内容: ```xml <key>NSLocationWhenInUseUsageDescription</key> <string>需要使用您的位置信息</string> <key>NSLocationAlwaysAndWhenInUseUsageDescription</key> <string>需要使用您的位置信息</string> <key>NSLocationAlwaysUsageDescription</key> <string>需要使用您的位置信息</string> <key>NSLocationUsageDescription</key> <string>需要使用您的位置信息</string> ``` 这是因为在iOS中需要获取用户位置信息时,需要获取用户的授权,而授权需要向用户显示授权提示框,而显示授权提示框需要在Info.plist文件中配置授权提示信息。 2. 在iOS项目的AppDelegate.swift文件中的application:didFinishLaunchingWithOptions:方法中添加以下内容: ```swift BMKLocationAuth.sharedInstance()?.checkPermision(withKey: "your_Baidu_Map_SDK_Key", authDelegate: self) ``` 这是因为在使用百度定位SDK之前,需要对SDK进行授权,而授权需要使用开发者申请的百度地图SDK的AK(即SDK Key)进行验证。 3. 在AppDelegate.swift文件中实现BMKLocationAuthDelegate协议,处理授权结果: ```swift class AppDelegate: UIResponder, UIApplicationDelegate, BMKLocationAuthDelegate { // ... func onCheckPermissionState(_ error: BMKLocationAuthErrorCode) { if error == BMKLocationAuthErrorCodeSuccess { // 授权成功 } else { // 授权失败 } } } ``` 这是因为在授权完成后,SDK会调用BMKLocationAuthDelegate协议中的onCheckPermissionState方法,通知开发者授权结果。 以上是在iOS中设置用户同意百度定位SDK的隐私协议的步骤,如果你使用的是其他的定位SDK或者插件,具体的操作可能会有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值