Skip to content

Commit

Permalink
-Camera Permission Impl
Browse files Browse the repository at this point in the history
  • Loading branch information
KhubaibKhan4 committed Jan 1, 2025
1 parent 0adf53e commit b4f3ebc
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 12 deletions.
6 changes: 6 additions & 0 deletions Notes.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Notes/Info.plist;
INFOPLIST_KEY_NSCameraUsageDescription = "Provide us the Camera Permission to Take Images and Record Videos to Store in Notes App.";
INFOPLIST_KEY_NSLocationAlwaysAndWhenInUseUsageDescription = "Provide us the Location Permission to Provide the realtime location inside the app.";
INFOPLIST_KEY_NSPhotoLibraryUsageDescription = "Provide us the Photo Library Access to get Image or Video.";
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
Expand Down Expand Up @@ -305,6 +308,9 @@
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Notes/Info.plist;
INFOPLIST_KEY_NSCameraUsageDescription = "Provide us the Camera Permission to Take Images and Record Videos to Store in Notes App.";
INFOPLIST_KEY_NSLocationAlwaysAndWhenInUseUsageDescription = "Provide us the Location Permission to Provide the realtime location inside the app.";
INFOPLIST_KEY_NSPhotoLibraryUsageDescription = "Provide us the Photo Library Access to get Image or Video.";
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
Expand Down
Binary file not shown.
29 changes: 17 additions & 12 deletions Notes/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@ import SwiftData

struct ContentView: View {
@State private var isSelected : Int = 0
@AppStorage("isPermissionGranted") var isPermissionGranted = false
var body: some View {
TabView(selection:$isSelected) {
Tab("Home", systemImage: "house",value: 0) {
HomeScreen()
}

Tab("Todo", systemImage: "checklist",value: 1) {
TodoScreen()

}
if !isPermissionGranted {
PermissionScreen()
}else {
TabView(selection:$isSelected) {
Tab("Home", systemImage: "house",value: 0) {
HomeScreen()
}

Tab("Todo", systemImage: "checklist",value: 1) {
TodoScreen()

}

Tab("Setting", systemImage: "gear",value:2) {
SettingScreen()
}
Tab("Setting", systemImage: "gear",value:2) {
SettingScreen()
}

}
}
}
}
Expand Down
68 changes: 68 additions & 0 deletions Notes/permissions_handler/PermissionScreen.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// PermissionScreen.swift
// Notes
//
// Created by Muhammad Khubaib Imtiaz on 01/01/2025.
//

import SwiftUI
import AVFoundation

struct PermissionScreen: View {

@State var cameraPermission: AVAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: .video)
@State var cameraPermissionText: String = "Allow Camera"
@State var photosPermissionText: String = "Allow Photos"
@State var locationPermissionText: String = "Allow Location"

@AppStorage("isPermissionGranted") private var isPermissionGranted = false

var body: some View {
VStack(alignment: .center, spacing: CGFloat(6)) {
Image("notes")
.resizable()
.frame(width: 200, height: 200)
.clipShape(RoundedRectangle(cornerRadius: 10))

Text("Please allow Notes to access your Camera, Photos, and Location.")
.padding(.all)
.frame(alignment: .center)



Button {
requestCameraPermission()
if cameraPermission == .authorized {
//isPermissionGranted = true
cameraPermissionText = "Camera Permission Granted"
} else {
cameraPermissionText = "Allow Camera"
}
} label: {
Label("\(cameraPermissionText)", systemImage: "camera")
}

Button {

} label: {
Label("Allow Photos", systemImage: "photo")
}

Button {

} label: {
Label("Allow Location", systemImage: "location")
}
}.onAppear {
cameraPermission = AVCaptureDevice.authorizationStatus(for: .video)
}
}

private func requestCameraPermission() {
AVCaptureDevice.requestAccess(for: .video) { isGranted in
DispatchQueue.main.async {
cameraPermission = AVCaptureDevice.authorizationStatus(for: .video)
}
}
}
}
41 changes: 41 additions & 0 deletions Notes/permissions_handler/cameraPermissions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// cameraPermissions.swift
// Notes
//
// Created by Muhammad Khubaib Imtiaz on 01/01/2025.
//

import SwiftUI
import AVFoundation


struct CameraPermissions: View {

@State private var cameraPermissionStatus: AVAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: .video)



var body: some View {
VStack {
if cameraPermissionStatus == .authorized {
Text("Camera Permission Granted")
}else{
Text("Camera Permissions Required")
Button("Provide Camera Permissions", systemImage: "camera") {
requestPermissions()
}
}

}.onAppear {
cameraPermissionStatus = AVCaptureDevice.authorizationStatus(for: .video)
}
}

private func requestPermissions() {
AVCaptureDevice.requestAccess(for: .video) { granted in
DispatchQueue.main.async {
cameraPermissionStatus = AVCaptureDevice.authorizationStatus(for: .video)
}
}
}
}

0 comments on commit b4f3ebc

Please sign in to comment.