Swift में प्राधिकरण और प्रमाणीकरण: iOS/macOS के लिए पूर्ण गाइड

शुरुआती लोगों के लिए ऑनलाइन Python प्रशिक्षक

Python आसानी से सीखें बिना सिद्धांत के बोझ के। स्वचालित जांच के साथ व्यावहारिक कार्य हल करें और ब्राउज़र में सीधे कोड लिखें।

पाठ्यक्रम शुरू करें

Swift में प्राधिकरण और प्रमाणीकरण: उपकरणों और लाइब्रेरीज़ का अवलोकन



iOS और macOS डेवलपमेंट में, उपयोगकर्ता डेटा की सुरक्षा सर्वोच्च प्राथमिकता है। प्रमाणीकरण (पहचान की पुष्टि) और प्राधिकरण (पहुँच अधिकार प्रदान करना) किसी भी आधुनिक एप्लिकेशन के मूलभूत घटक हैं। इस लेख में, हम Apple ID, तृतीय-पक्ष सेवाओं (Google, Facebook) के माध्यम से लॉगिन लागू करने के साथ-साथ टोकन के स्थानीय भंडारण के लिए प्रमुख Swift उपकरणों, लाइब्रेरीज़ और फ्रेमवर्क का विश्लेषण करेंगे।



यह लेख Swift और Xcode की मूल बातों से परिचित डेवलपर्स के लिए है। सभी कोड उदाहरण Swift 5.9+ पर परीक्षण किए गए हैं और iOS 17+/macOS 14+ के साथ संगत हैं।



1. AuthenticationServices: Apple Sign In



Apple ID के माध्यम से मूल प्राधिकरण उन ऐप्स के लिए App Store की एक अनिवार्य आवश्यकता है जो तृतीय-पक्ष प्रदाताओं (Google, Facebook) का उपयोग करते हैं। AuthenticationServices फ्रेमवर्क Sign in with Apple को लागू करने के लिए एक सरल API प्रदान करता है।



import AuthenticationServicesimport SwiftUI

struct SignInWithAppleView: View { var body: some View { SignInWithAppleButton( onRequest: { request in request.requestedScopes = [.fullName, .email] }, onCompletion: { result in switch result { case .success(let authResults): // उपयोगकर्ता टोकन प्राप्त करें guard let credential = authResults.credential as? ASAuthorizationAppleIDCredential, let tokenData = credential.identityToken, let token = String(data: tokenData, encoding: .utf8) else { return } print("Apple token: \\(token)") case .failure(let error): print("Error: \\(error.localizedDescription)") } } ) .signInWithAppleButtonStyle(.black) .frame(width: 280, height: 44) }}


महत्वपूर्ण: Apple केवल पहले लॉगिन पर fullName और email लौटाता है। बाद के प्राधिकरणों के लिए credential.user से userID का उपयोग करें।



2. Firebase Authentication: मल्टी-प्रदाता प्रमाणीकरण



Firebase Auth ईमेल, Google, Facebook, GitHub और अन्य प्रदाताओं के माध्यम से प्राधिकरण के लिए एक क्रॉस-प्लेटफ़ॉर्म समाधान है। इसमें तैयार UI घटक और Firestore के साथ एकीकरण शामिल है।



Swift Package Manager के माध्यम से स्थापना:

// Package.swift या Xcode के माध्यम से: File > Add Packages// URL: https://github.com/firebase/firebase-ios-sdk.git// FirebaseAuth, FirebaseFirestore चुनें


ईमेल/पासवर्ड के माध्यम से लॉगिन का उदाहरण:

import FirebaseAuth

func signInWithEmail(email: String, password: String) { Auth.auth().signIn(withEmail: email, password: password) { authResult, error in if let error = error { print("Auth error: \\(error.localizedDescription)") return } guard let user = authResult?.user else { return } print("User signed in: \\(user.uid)") }}

func createUser(email: String, password: String) { Auth.auth().createUser(withEmail: email, password: password) { authResult, error in // पंजीकरण प्रसंस्करण }}


Google Sign-In के लिए अतिरिक्त रूप से GoogleSignIn SDK का उपयोग किया जाता है। Firebase स्वचालित रूप से सत्रों और टोकन नवीनीकरण का प्रबंधन करता है।



3. Keychain Services: टोकन का सुरक्षित भंडारण



UserDefaults में JWT टोकन या रिफ्रेश टोकन संग्रहीत करना एक गंभीर सुरक्षा त्रुटि है। Keychain (iOS/macOS का एन्क्रिप्टेड स्टोरेज) का उपयोग करें।



Keychain के लिए रैपर:

import Security

class KeychainManager { static func save(key: String, data: Data) { let query: [String: Any] = [ kSecClass as String: kSecClassGener

अनुशंसाएँ