問題:有時候,我們在做App項目時,不需要做注冊登錄頁面??,但是,這個App必須要有隱私政策和用戶協(xié)議。所以,在第一次打開App時,就需要先進(jìn)入到隱私政策頁面。
1、首先,自定義一個UIViewController,可以在這個UIViewController上自定義一些按鈕或其他內(nèi)容,點擊按鈕可以直接到加載HTML格式的隱私政策WKWebView頁面:
GW_PrivacyPolicy_VC.h:
@interface GW_PrivacyPolicy_VC : GWBaseViewController
/**
* 同意隱私政策之后,繼續(xù),拒絕,提示:APP不可用
*/
@property (nonatomic, copy) void (^agreePrivacyPolicy_Block) (BOOL is_Agree);
@end
GW_PrivacyPolicy_VC.m:
#import "GW_PrivacyPolicy_VC.h"
#import "GW_PrivacyPolicy_PopView.h" // 隱私政策 彈窗
@interface GW_PrivacyPolicy_VC ()
@end
@implementation GW_PrivacyPolicy_VC
- (void)viewDidLoad {
[super viewDidLoad];
self.statusBg.hidden = YES;
self.leftButton.hidden = YES;
self.rightButton.hidden = YES;
self.navigationBar.hidden = YES;
__weak typeof(self)weakSelf = self;
GW_PrivacyPolicy_PopView *popView = [[GW_PrivacyPolicy_PopView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
[self.view addSubview:popView];
popView.agreePrivacyPolicy_ContinueBlock = ^(BOOL is_Agree) { // 同意,繼續(xù)代碼
if (weakSelf.agreePrivacyPolicy_Block) {
weakSelf.agreePrivacyPolicy_Block(is_Agree);
}
};
return;
}
@end
2、使用:將這個自定義的UIViewController在AppDelegate.m中跳轉(zhuǎn),
- 原理:在打開App時,先判斷其是否已經(jīng)同意“隱私政策”,如果同意就跳到首頁,否則,就是沒同意,跳到“隱私政策”頁面,不同意,無法使用改App。
AppDelegate.h:
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m:
#import "AppDelegate.h"
#import "KMainTabBarController.h"
#import "GW_PrivacyPolicy_VC.h"
@interface AppDelegate ()
@property (nonatomic, strong) KMainTabBarController *mainTabBarController;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
self.window.backgroundColor = [UIColor whiteColor];
self.mainTabBarController = [[KMainTabBarController alloc] init];
self.window.rootViewController = self.mainTabBarController;
[self.window makeKeyAndVisible];
// 判斷是否有保存到本地的標(biāo)志:NSUserDefaults(同意“隱私政策” 保存1,否則就是nil或0)
NSString *is_AgreeStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"is_Agree_App_PrivacyPolicy"];
if (![is_AgreeStr isEqualToString:@"1"]) {
GW_PrivacyPolicy_VC *privacyPolicyVC = [[GW_PrivacyPolicy_VC alloc] init];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:privacyPolicyVC];
privacyPolicyVC.agreePrivacyPolicy_Block = ^(BOOL is_Agree) {
// 同意隱私政策之后,再跳轉(zhuǎn)首頁
self.mainTabBarController.selectedIndex = 0;
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:self.mainTabBarController];
self.mainTabBarController.navigationController.navigationBarHidden = YES; // (如果首頁不帶 系統(tǒng)導(dǎo)航欄 的話)不添加這一行的話,第一次運行會有導(dǎo)航欄,是其上面那一行造成的
// 定位權(quán)限(獲取定位)(這種操作,在進(jìn)入頁面之后在判斷)
//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
};
} else {
//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
}
return YES;
}
@end