This is a sample (and basic) iOS application demonstrating how to properly implement camera access for web applications running inside a WKWebView.
- ✅ WKWebView configuration for camera access
- ✅ Proper permission handling for getUserMedia API
- ✅ Full-screen WebView implementation
- ✅ Camera permission request handling
- ✅ Support for both front and back cameras
- ✅ Photo library access for image uploads
- Xcode 14.0+
- iOS 14.0+
- Physical iOS device (camera doesn't work in simulator)
- Web application served over HTTPS
Setup Instructions (If you already have a WebView setup in your native application, please reference the basic configuration below and add any missing parts if necessary)
-
Clone this repository
git clone [repository-url] cd temporary
-
Open in Xcode
open temporary.xcodeproj
-
Update the WebView URL
- Open
temporary/ContentView.swift
- Replace
"https://YOUR-WEBAPP-URL-HERE.com"
with your actual web application URL
- Open
-
Add Required Permissions (if not already present)
- Select your project in Xcode
- Go to the Info tab
- Add these keys:
NSCameraUsageDescription
: "This app needs access to your camera to enable camera functionality in the web application."NSPhotoLibraryUsageDescription
: "This app needs access to your photo library to upload images."
-
Build and Run
- Connect your iOS device
- Select it as the run destination
- Press Cmd+R or click the Run button
temporary/
├── temporary.xcodeproj # Xcode project file
├── temporary/
│ ├── temporaryApp.swift # App entry point
│ ├── ContentView.swift # Main WebView implementation
│ └── Assets.xcassets/ # App assets
├── temporaryTests/ # Unit tests
└── temporaryUITests/ # UI tests
The main implementation includes:
- WKWebView Configuration: Allows inline media playback and removes user interaction requirements
- Delegates: WKNavigationDelegate and WKUIDelegate for handling navigation and permission requests
- Permission Handler: Automatically grants camera permissions to the web content
- AVFoundation Integration: Requests camera permission at the native level
// WebView configuration for media
configuration.allowsInlineMediaPlayback = true
configuration.mediaTypesRequiringUserActionForPlayback = []
// Permission handling
func webView(_ webView: WKWebView,
requestMediaCapturePermissionFor origin: WKSecurityOrigin,
initiatedByFrame frame: WKFrameInfo,
type: WKMediaCaptureType,
decisionHandler: @escaping (WKPermissionDecision) -> Void) {
decisionHandler(.grant)
}
navigator.mediaDevices.getUserMedia({
video: true,
audio: false
})
.then(stream => {
// Use the stream
videoElement.srcObject = stream;
})
.catch(error => {
console.error('Camera access error:', error);
});
<!-- Camera only -->
<input type="file" accept="image/*" capture="camera">
<!-- Camera or Photo Library (user choice) -->
<input type="file" accept="image/*">
For production use, consider:
- Validate origins in the permission handler
- Use specific domain checking instead of granting all permissions
- Handle errors gracefully with user-friendly messages
- Ensure HTTPS for your web application
Issue | Solution |
---|---|
Camera permission dialog doesn't appear | Check Info.plist for NSCameraUsageDescription |
Photo library permission dialog doesn't appear | Check Info.plist for NSPhotoLibraryUsageDescription |
getUserMedia fails | Ensure web app uses HTTPS |
App crashes on launch | Verify the URL is valid and starts with https:// |
Camera doesn't work | Test on real device, not simulator |
This sample code is provided as-is for demonstration purposes.
For issues or questions about this implementation, please refer to the included documentation files or Apple's WKWebView documentation.