配置隱私協(xié)議 - iOS

根據(jù)蘋果隱私協(xié)議新規(guī)的推出,要求所有應(yīng)用包含隱私保護協(xié)議,故為此在 App 中添加了如下隱私協(xié)議模塊.

首次安裝 App 的情況下默認調(diào)用隱私協(xié)議模塊展示相關(guān)信息一次,當(dāng)用戶點擊同意按鈕后,從此不再執(zhí)行該模塊方法.

GitHub Demo Link:
https://github.com/survivorsfyh/YHSampleCode/tree/master/YHPrivacyAgreementDemo

具體 code 如下:

一.聲明(.h)

/*
 隱私協(xié)議
 */
#import <Foundation/Foundation.h>
 
@interface PrivacyAgreement : NSObject
 
+ (instancetype)shareInstance;
 
@end

二.實現(xiàn)(.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" // 本地存儲路徑設(shè)置_文件名稱
#define LocalPath_ProjectConfig        @"Project/ProjectConfigInfo/" // 本地存儲路徑設(shè)置_文件路徑
#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;
}
 
 
 
/**
 渲染隱私協(xié)議視圖
 */
- (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 設(shè)置相關(guān)
 
 其中包含加載方式(本地文件 & 網(wǎng)絡(luò)請求)
 @return 當(dāng)前控件
 */
- (WKWebView *)webView {
    if (!_webView) {
        NSError *error;
        // 本地 url 地址設(shè)置
        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 - 網(wǎng)絡(luò)請求加載方式
//        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@""]// 隱私協(xié)議的 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

注:如上方法中使用的本地加載的方式,若需要使用網(wǎng)絡(luò)請求的方式,詳見具體 code 中的注釋部分.

三.方法調(diào)用
在需要的地方引用該頭文件并調(diào)用接口方法即可,一般在 appdelegate 中.

[PrivacyAgreement shareInstance];

四.方法類中相關(guān)封裝的方法
4.1.點擊事件中文件寫入本地的方法

/**
 插入對象至本地 plist 文件
 @param dataSource  數(shù)據(jù)源
 @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);
    }
}

以上便是此次分享的內(nèi)容,還望能對大家有所幫助,謝謝!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末茁瘦,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子储笑,更是在濱河造成了極大的恐慌甜熔,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件突倍,死亡現(xiàn)場離奇詭異腔稀,居然都是意外死亡,警方通過查閱死者的電腦和手機羽历,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門焊虏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人秕磷,你說我怎么就攤上這事诵闭。” “怎么了澎嚣?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵疏尿,是天一觀的道長。 經(jīng)常有香客問我币叹,道長润歉,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任颈抚,我火速辦了婚禮踩衩,結(jié)果婚禮上嚼鹉,老公的妹妹穿的比我還像新娘。我一直安慰自己驱富,他們只是感情好锚赤,可當(dāng)我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著褐鸥,像睡著了一般线脚。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上叫榕,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天浑侥,我揣著相機與錄音,去河邊找鬼晰绎。 笑死寓落,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的荞下。 我是一名探鬼主播伶选,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼尖昏!你這毒婦竟也來了仰税?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤抽诉,失蹤者是張志新(化名)和其女友劉穎陨簇,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體迹淌,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡塞帐,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了巍沙。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片葵姥。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖句携,靈堂內(nèi)的尸體忽然破棺而出榔幸,到底是詐尸還是另有隱情,我是刑警寧澤矮嫉,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布削咆,位于F島的核電站,受9級特大地震影響蠢笋,放射性物質(zhì)發(fā)生泄漏拨齐。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一昨寞、第九天 我趴在偏房一處隱蔽的房頂上張望瞻惋。 院中可真熱鬧厦滤,春花似錦、人聲如沸歼狼。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽羽峰。三九已至趟咆,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間梅屉,已是汗流浹背值纱。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留坯汤,地道東北人计雌。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像玫霎,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子妈橄,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容

  • 2017.8.27 我還是太懶了叉钥,周圍的誘惑太大了。好好培養(yǎng)自己的抗外界干擾能力吧篙贸。
    ice_bj閱讀 182評論 0 0
  • 假如我是田螺姑娘投队,一定會重新審視我的天賦,開開心心地默默幫助他人爵川,造福弱勢群體而不是因此換來愛情敷鸦;而且我也會重新審...
    掬楓閱讀 1,086評論 0 0