Thuta Learning
ရှာဖွေရန်
IntermediateMobile Developmentintermediate

Runtime Permissions Basics

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Runtime Permissions Basics ကို ကြောက်စရာမလိုအောင် နားလည်မယ်
  • ကိုယ်တိုင် Xcode/SwiftUI code ကို run ကြည့်တတ်မယ်
  • Real project ထဲမှာ ဒီ concept ကို ချက်ချင်း အသုံးချတတ်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

Basic chapter မှာ `Info.plist` ထဲ usage description string ထည့်ရမယ်ဆိုတာ လေ့လာခဲ့ပြီးသားပါ — iOS API (Camera, Location) ကို ခေါ်တာနဲ့ system dialog ကို automatic ပြပေးပါတယ် (Android လိုမျိုး `rememberLauncherForActivityResult` manual trigger စရာ မလိုပါ) — user 'Allow' ဒါမှမဟုတ် 'Don't Allow' ရွေးနိုင်ပါတယ်။ Location permission ကို 'While Using App' / 'Always' ဆိုပြီး granularity ၂ မျိုးထိ ခွဲထားတတ်ပါတယ် — 'Always' (background location) ကို App Store review က အထူး strict စစ်ဆေးပါတယ်.

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

Location feature ရှိတဲ့ app မှာ `CLLocationManager().requestWhenInUseAuthorization()` ကို ခေါ်ရင် — `Info.plist` ထဲက `NSLocationWhenInUseUsageDescription` string ပါတဲ့ system dialog ချက်ချင်း ပေါ်လာပါတယ်, user 'Allow' နှိပ်မှသာ location data ကို ရယူနိုင်ပါတယ်, 'Don't Allow' ရွေးရင် graceful ဖြစ်ဖြစ် handle လုပ်ပေးရပါတယ်.

အတူတူ ကြည့်မယ်

swift
import CoreLocation

class LocationManager: NSObject, ObservableObject {
    private let manager = CLLocationManager()

    override init() {
        super.init()
        manager.delegate = self
    }

    func requestPermission() {
        manager.requestWhenInUseAuthorization()
    }
}

extension LocationManager: CLLocationManagerDelegate {
    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        print("Authorization status: \(manager.authorizationStatus)")
    }
}
You should see
`requestPermission()` ခေါ်ရင် system location permission dialog ပေါ်လာပြီး, 'Allow' ရွေးရင် authorizationStatus ပြောင်းသွားတာ Console ထဲ တွေ့ရမည်။

၅ မိနစ် စမ်းကြည့်

`LocationManager` ကို ကိုယ်တိုင်ရေးပြီး `Info.plist` ထဲ `NSLocationWhenInUseUsageDescription` ထည့်ကာ Simulator ပေါ်မှာ permission dialog ကို trigger ကြည့်ပါ — 'Allow' နဲ့ 'Don't Allow' ၂ မျိုးလုံး ရွေးကြည့်ပါ။

သတိလေးတစ်ချက်

User 'Don't Allow' ကို ရွေးလိုက်ရင် app ကနေ dialog ကို ထပ်ခါထပ်ခါ ပြခွင့် မရှိတော့ပါ — Settings ကို manual သွားရမှာမို့, app ထဲမှာ 'Settings ဖွင့်ပါ' ဆိုတဲ့ guidance ကို ပြသင့်ပါတယ်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • `Info.plist` ထဲ usage description string မထည့်ဘဲ location/camera API ကို ခေါ်ခြင်း — app ချက်ချင်း crash ဖြစ်ပါလိမ့်မယ် (Android ထက် ပိုတင်းကျပ်တဲ့ failure mode)
  • Background location ('Always') ကို core feature အတွက် တကယ်မလိုအပ်ဘဲ တောင်းဆိုခြင်း — App Store review ကနေ reject ခံရနိုင်ပါတယ်

အခု ကိုယ်တိုင် စမ်းကြည့်

`LocationManager` ကို ကိုယ်တိုင်ရေးပြီး `Info.plist` ထဲ `NSLocationWhenInUseUsageDescription` ထည့်ကာ Simulator ပေါ်မှာ permission dialog ကို trigger ကြည့်ပါ — 'Allow' နဲ့ 'Don't Allow' ၂ မျိုးလုံး ရွေးကြည့်ပါ။

You'll know it worked when: `requestPermission()` ခေါ်ရင် system location permission dialog ပေါ်လာပြီး, 'Allow' ရွေးရင် authorizationStatus ပြောင်းသွားတာ Console ထဲ တွေ့ရမည်။

Runtime Permissions Basics | Thuta Learning