Skip to content

Instantly share code, notes, and snippets.

@JohnKim
Created July 12, 2024 14:54
Show Gist options
  • Save JohnKim/75fe3718bcfb2ba1f1e2805d472bf13b to your computer and use it in GitHub Desktop.
Save JohnKim/75fe3718bcfb2ba1f1e2805d472bf13b to your computer and use it in GitHub Desktop.
Local Notification 과 privacy screen 을 추가한 코드 for IOS
import UIKit
import Flutter
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
private var flutterViewController: FlutterViewController!
private var securityChannel: FlutterMethodChannel!
private var blurEffectView: UIVisualEffectView?
private var isInBackground: Bool = false // Track whether app is in background
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
private func setupFlutterCommunication() {
flutterViewController = window?.rootViewController as? FlutterViewController
securityChannel = FlutterMethodChannel(
name: "security",
binaryMessenger: flutterViewController.binaryMessenger
)
securityChannel.setMethodCallHandler(handle)
}
override func applicationDidEnterBackground(_ application: UIApplication) {
isInBackground = true // App entered background
enableAppSecurity()
}
override func applicationDidBecomeActive(_ application: UIApplication) {
// Check if the app was in background before becoming active
if isInBackground {
disableAppSecurity()
isInBackground = false
}
}
private func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "enableAppSecurity":
result(nil)
case "disableAppSecurity":
result(nil)
default:
result(FlutterMethodNotImplemented)
}
}
private func enableAppSecurity() {
let blurEffect = UIBlurEffect(style: .light)
blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView?.frame = window!.frame
window?.addSubview(blurEffectView!)
}
private func disableAppSecurity() {
UIView.animate(withDuration: 0.5, animations: {
self.blurEffectView?.alpha = 0.0
}) { _ in
self.blurEffectView?.removeFromSuperview()
}
}
}
import 'dart:developer';
import 'package:flutter/services.dart';
abstract class IAppScreenPrivacy {
Future<void> enableScreenPrivacy();
Future<void> disableScreenPrivacy();
}
class AppScreenPrivacyService extends IAppScreenPrivacy {
static const platform = MethodChannel('security');
@override
Future<void> disableScreenPrivacy() async {
try {
await platform.invokeMethod('disableAppSecurity');
} on PlatformException catch (e) {
log('Failed to disable app security: "${e.message}"');
}
}
@override
Future<void> enableScreenPrivacy() async {
try {
await platform.invokeMethod('enableAppSecurity');
} on PlatformException catch (e) {
log('Failed to enable app security: "${e.message}"');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment