問題:有時候,我們在做App項目時,不需要做注冊登錄頁面
??,但是,這個App必須要有隱私政策和用戶協議
。所以,在第一次打開App時,就需要先進入到隱私政策
頁面。
1、首先,自定義一個UIViewController
,可以在這個UIViewController
上自定義一些按鈕或其他內容,點擊按鈕可以直接到加載HTML
格式的隱私政策WKWebView
頁面:
GW_PrivacyPolicy_VC.h:
@interface GW_PrivacyPolicy_VC : GWBaseViewController
/**
* 同意隱私政策之后,繼續,拒絕,提示: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) { // 同意,繼續代碼
if (weakSelf.agreePrivacyPolicy_Block) {
weakSelf.agreePrivacyPolicy_Block(is_Agree);
}
};
return;
}
@end
2、使用:將這個自定義的UIViewController
在AppDelegate.m
中跳轉,
- 原理:在打開App時,先判斷其是否已經同意“隱私政策”,如果同意就跳到首頁,否則,就是沒同意,跳到“隱私政策”頁面,不同意,無法使用改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];
// 判斷是否有保存到本地的標志: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) {
// 同意隱私政策之后,再跳轉首頁
self.mainTabBarController.selectedIndex = 0;
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:self.mainTabBarController];
self.mainTabBarController.navigationController.navigationBarHidden = YES; // (如果首頁不帶 系統導航欄 的話)不添加這一行的話,第一次運行會有導航欄,是其上面那一行造成的
// 定位權限(獲取定位)(這種操作,在進入頁面之后在判斷)
//[[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