iOS隐私安全:用户协议及隐私政策弹框(包含超链接属性、demo支持中英文切换)

2022-08-22 11:00:40 浏览数 (2)

前言

熟悉监管要求,掌握合规操作流程,避免App被降级或者下架。需要确保App有《隐私政策》,并且在用户首次启动App时就弹出《隐私政策》取得用户同意。

登录界面弹用户协议及隐私政策时,如果用户点击不同意,不能停留在弹框界面,需要隐藏弹框,否则无法通过OPPO安卓应用市场。

《用户协议及隐私政策》 弹框的实现步骤:

1、自定义TextView,采用富文本属性进行内容设置attributedText(包括下划线NSUnderlineStyleSingle、超链接NSLinkAttributeName 、颜色NSForegroundColorAttributeName 等信息) 2、实现代理方法textView:shouldInteractWithURL:inRange,处理点击超链接的回调(打开对应URL Webview)

  • 效果图(点击demo的右上架文字进行中英文切换
  • 文本框信息对应的中英文key,用于本地化
代码语言:javascript复制

"Explain3" = "向您说明,在使用我们的服务时,我们如何收集、使用、储存和分享这些信息,以及我们为您提供的访问、更新、控制和保护这些信息的方式。本";
"Wemaycollect1"="您在使用我们的服务时,我们可能会收集和使用您的相关信息。我们希望通过本";
"then_click_Agree" = " ,希望您仔细阅读,充分理解协议中的内容后再点击同意。";


"Wemaycollect1"="We may collect and use information about you when you use our services. We hope to pass this";
"Explain3"= "Explain to you how we collect, use, store and share this information and how we provide you with access, update, control and protection when using our services. this";
"then_click_Agree"= "  , I hope you read it carefully, fully understand the content of the agreement, and then click Agree.";

从csdn资源下载demo源码:https://download.csdn.net/download/u011018979/14026773

资源下载

I、 自定义TextView:QCTTextViewHyperLink

采用富文本属性进行内容设置attributedText

从csdn资源下载demo源码:https://download.csdn.net/download/u011018979/14026773 CSDN文章:https://kunnan.blog.csdn.net/article/details/103902362 获取资源下载链接

1.1 采用富文本属性进行内容设置

attributedText

包括下划线NSUnderlineStyleSingle、 超链接NSLinkAttributeName 、 颜色NSForegroundColorAttributeName 等信息

  • 新增超链接属性
代码语言:javascript复制
        


        [attrStr addAttribute:NSLinkAttributeName value:k_serviceAgreement_URL range:str4Range];
        
        [attrStr addAttribute:NSLinkAttributeName value:k_serviceAgreement_URL range:str2Range];

        tmp.editable = NO;
        tmp.attributedText = attrStr;//text
            tmp.selectedRange = NSMakeRange(attrStr.length, 0);

        
        tmp.delegate = self;
        tmp.userInteractionEnabled = YES;
        

  • 设置下划线和颜色
代码语言:javascript复制
    [attrStr setAttributes:@{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)}
                     range:str4Range];
    [attrStr addAttribute:NSFontAttributeName value:kPingFangFont(13) range:str4Range];
    
    
    [attrStr addAttribute:NSForegroundColorAttributeName value:HWColor(6, 53, 253) range:str4Range];

1.2 实现代理方法

  • 处理点击超链接的回调(打开对应URL Webview)
代码语言:javascript复制
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {

代码语言:javascript复制

/**
 代理方法
 */
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    
    __weak __typeof__(self) weakSelf = self;

    if([URL.absoluteString isEqualToString:k_serviceAgreement_URL]){
        
        [self.viewModel.hiddenQCTserviceAgreementViewSubject sendNext:nil];

        void (^showQCTserviceAgreementViewBlock)(id sender) = ^void(id sender) {
            NSLog(@"nil");
            // 展示
            [weakSelf.viewModel.showQCTserviceAgreementViewSubject sendNext:nil];
            
            
        };
                                        [self.viewModel.User_Agreement_and_Privacy_PolicySubject sendNext:showQCTserviceAgreementViewBlock];

    }
    
//
    
    
    return NO;
}

II、封装《用户协议及隐私政策》视图

  • 获取带有富文本字符串的TextView视图
代码语言:javascript复制
//
//  QCTTextViewHyperLink.m
//  retail
//
//  Created by mac on 2020/1/9.
//  Copyright © 2020 QCT. All rights reserved.
//

#import "QCTTextViewHyperLink.h"

@implementation QCTTextViewHyperLink

/**
 获取 《用户协议及隐私政策》的数据
 @return《用户协议及隐私政策》的数据
 */
  (instancetype)getserviceAgreemenTextView{
    
    QCTTextViewHyperLink * tmp  = [QCTTextViewHyperLink new];
    NSString *str1 = QCTLocal(@"Wemaycollect1");
    NSString *str2 = [NSString stringWithFormat:@"《%@》",QCTLocal(@"Service_Agreement4User")];
    
    NSString *str3 = QCTLocal(@"Explain3");
    NSString *str4 = [NSString stringWithFormat:@"《%@》",QCTLocal(@"Service_Agreement4User")];
    
    NSString *str5 = QCTLocal(@"then_click_Agree");
    NSString *str = [NSString stringWithFormat:@"%@%@%@%@%@",str1,str2,str3,str4,str5];
    
    //1、 设置富文本属性
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
    
    [attrStr addAttribute:NSFontAttributeName value:kPingFangFont(13) range:NSMakeRange(0 ,attrStr.length)];
    
    [attrStr addAttribute:NSForegroundColorAttributeName value:HWColor(51, 51, 51) range:NSMakeRange(0 ,attrStr.length)];
    
    
// 2、设置行lineSpacing
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.lineSpacing = 5;
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    [attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
    [attrStr addAttributes:attributes range:NSMakeRange(0, attrStr.length)];
    
    //        NSParagraphStyle defaultParagraphStyle
//3、设置下划线和颜色
    NSRange str2Range = NSMakeRange(str1.length, str2.length );
    
    NSRange str4Range = NSMakeRange(str1.length str2.length str3.length, str4.length );
    

    [attrStr setAttributes:@{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)}
                     range:str2Range];
    [attrStr addAttribute:NSFontAttributeName value:kPingFangFont(13) range:str2Range];
    
    [attrStr addAttribute:NSForegroundColorAttributeName value:HWColor(6, 53, 253) range:str2Range];
    
    
    [attrStr setAttributes:@{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)}
                     range:str4Range];
    [attrStr addAttribute:NSFontAttributeName value:kPingFangFont(13) range:str4Range];
    
    [attrStr addAttribute:NSForegroundColorAttributeName value:HWColor(6, 53, 253) range:str4Range];
    
    
    //4、新增超链接属性
    [attrStr addAttribute:NSLinkAttributeName value:k_serviceAgreement_URL range:str4Range];
    
    [attrStr addAttribute:NSLinkAttributeName value:k_serviceAgreement_URL range:str2Range];
    
    tmp.editable = NO;
    tmp.attributedText = attrStr;//text
    tmp.selectedRange = NSMakeRange(attrStr.length, 0);
    //        tmp.userInteractionEnabled = YES;

    
    return tmp;
}

// 继承UITextView重写这个方法
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    // 返回NO为禁用,YES为开启
    // 粘贴
    if (action == @selector(paste:)) return NO;
    // 剪切
    if (action == @selector(cut:)) return NO;
    // 复制
    if (action == @selector(copy:)) return NO;
    // 选择
    if (action == @selector(select:)) return NO;
    // 选中全部
    if (action == @selector(selectAll:)) return NO;
    // 删除
    if (action == @selector(delete:)) return NO;
    // 分享
    if (action == @selector(share)) return NO;
    return [super canPerformAction:action withSender:sender];
    
}


@end

III、Q&A

完整内容请点击这里

0 人点赞