阅读 635

iOS小知识:用户协议及隐私政策弹框(含超链接属性)【demo支持中英文切换】

前言

熟悉监管要求,掌握合规操作流程,避免App被下架。

您需要确保App有《隐私政策》,并且在用户首次启动App时就弹出《隐私政策》取得用户同意。

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

1、自定义TextView,采用富文本属性进行内容设置attributedText(包括下划线NSUnderlineStyleSingle、超链接NSLinkAttributeName 、颜色NSForegroundColorAttributeName 等信息)

2、实现代理方法textView:shouldInteractWithURL:inRange,处理点击超链接的回调(打开对应URL Webview)

  • 效果图(点击demo的右上架文字进行中英文切换

公众号:iOS技能 公众号:iOS技能

  • 文本框信息对应的中英文key,用于本地化

 "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."; 复制代码

I、 自定义TextView: QCTTextViewHyperLink

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

demo源码:https://download.csdn.net/download/u011018979/14026773

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

attributedText

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

  • 新增超链接属性

                 [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;          复制代码

  • 设置下划线和颜色

    [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)

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange { 复制代码

 /**  代理方法  */ - (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视图

// //  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

3.1 为什么我下载demo后,在xcode12中允许报错:library not found for -lAXIndicatorView;请问怎么解决?

在这里插入图片描述

原因:这是找不到 CocoaPods库 AXIndicatorView。是 AXWebViewController库依赖于它

在这里插入图片描述 在这里插入图片描述

  • 解决方法:  你更新 pod 'AXWebViewController' 即可。

1、只更新指定的库,其它库忽略:pod update AXWebViewController --verbose  --repo-update  

2、pod update会更新所有的类库,获取最新版本的类库

cocoapods 用法文章:https://blog.csdn.net/z929118967/article/details/103830017

exit 0%                                                                                                                           ➜  retail git:(develop) ✗ cat ~/bin/knpod #!/bin/sh #该命令只安装新添加的库,已更新的库忽略 pod install --verbose --no-repo-update #该命令只更新指定的库,其它库忽略 #pod update 库名 --verbose --no-repo-update exit 0%                                                                                                                           ➜  retail git:(develop) ✗  复制代码

3.2 在hbuilder打包好ipa包了,需要隐私弹窗的解决方案

方案一:写webview形式的弹框/nvue

在这里插入图片描述

方案二:利用js 和OC的交互,在需要弹窗的地方,调用已经封装好的OC方法即可

具体请看这篇文章的第三部分 kunnan.blog.csdn.net/article/det…在这里插入图片描述

  • 基于HBuilder iOS离线打包widget方式 js 调oc 的参考代码

js调用原生oc方法

//LoginViewController是oc类 txt是要传的参数   plus.ios.importClass("LoginViewController").login("txt“); 复制代码

js调用oc对象方法

//objectToJsonImage是oc对象方法  massageArray返回值   var Massage = plus.ios.importClass("LoginViewController");   var massage = new Massage();   var massageArray = massage.objectToJsonImage(); 复制代码

IV、see also

更多内容请关注#小程序:iOS逆向,只为你呈现有价值的信息,专注于移动端技术研究领域。

更多资讯和服务请关注#公众号:iOS技能

4.1 创建带有图片的富文本

iOS图文混排之【 NSAttachmentAttributeName 创建带有图片的富文本】(案例:展示信用卡标签)

kunnan.blog.csdn.net/article/det…

4.2 富文本属性

属性用途类型
NSFontAttributeName字号UIFont 默认12
NSParagraphStyleAttributeName段落样式NSParagraphStyle
NSForegroundColorAttributeName前景色UIColor
NSBackgroundColorAttributeName背景色UIColor
NSObliquenessAttributeName字体倾斜NSNumber
NSExpansionAttributeName字体加粗NSNumber 比例 0就是不变 1增加一倍
NSKernAttributeName字间距CGFloat
NSUnderlineStyleAttributeName下划线1或0
NSUnderlineColorAttributeName下划线颜色UIColor
NSStrikethroughStyleAttributeName删除线1或0
NSStrikethroughColorAttributeName删除线颜色UIColor
NSStrokeColorAttributeNamesame as ForegroundColorUIColor
NSStrokeWidthAttributeName字体描边CGFloat
NSLigatureAttributeName连笔字1或0
NSShadowAttributeName阴影NSShawdow
NSTextEffectAttributeName设置文本特殊效果,目前只有图版印刷效果可用NSString
NSAttachmentAttributeName设置文本附件,常用插入图片NSTextAttachment
NSLinkAttributeName链接NSURL (preferred) or NSString
NSBaselineOffsetAttributeName基准线偏移NSNumber,可用于布局
NSWritingDirectionAttributeName文字方向分别代表不同的文字出现方向@[@(1),@(2)]
NSVerticalGlyphFormAttributeName水平或者竖直文本1竖直 0水平


作者:公众号iOS逆向
链接:https://juejin.cn/post/7023270005550612516

文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐