Skip to content

VelociaTech/IOSWebViewAppExample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

iOS WebView Camera / Photo Access Sample (Minimum requirements for Velocia WebApp)

This is a sample (and basic) iOS application demonstrating how to properly implement camera access for web applications running inside a WKWebView.

Features

  • ✅ 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

Requirements

  • 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)

  1. Clone this repository

    git clone [repository-url]
    cd temporary
  2. Open in Xcode

    open temporary.xcodeproj
  3. Update the WebView URL

    • Open temporary/ContentView.swift
    • Replace "https://YOUR-WEBAPP-URL-HERE.com" with your actual web application URL
  4. 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."
  5. Build and Run

    • Connect your iOS device
    • Select it as the run destination
    • Press Cmd+R or click the Run button

Project Structure

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

Key Implementation Details

ContentView.swift

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

Important Code Sections

// 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)
}

Testing Camera and Photo Library Access

Camera Stream (getUserMedia API)

navigator.mediaDevices.getUserMedia({
    video: true,
    audio: false
})
.then(stream => {
    // Use the stream
    videoElement.srcObject = stream;
})
.catch(error => {
    console.error('Camera access error:', error);
});

File Input for Camera/Photo Library

<!-- Camera only -->
<input type="file" accept="image/*" capture="camera">

<!-- Camera or Photo Library (user choice) -->
<input type="file" accept="image/*">

Security Considerations

For production use, consider:

  1. Validate origins in the permission handler
  2. Use specific domain checking instead of granting all permissions
  3. Handle errors gracefully with user-friendly messages
  4. Ensure HTTPS for your web application

Troubleshooting

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

License

This sample code is provided as-is for demonstration purposes.

Support

For issues or questions about this implementation, please refer to the included documentation files or Apple's WKWebView documentation.

About

Example IOS application to demonstrate requirement of webview configuration

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages