LockedCameraCapture - Lock Screen Camera Capture UI Question

After the session video, "Build a great Lock Screen camera capture experience", was unclear about the UI.

So do developers need to provide a whole new UI in the extension? The main UI cannot be repurposed?

Answered by Frameworks Engineer in 790448022

Hello!

You're welcome to use your existing UI in a Capture Extension. Make any source files you want to use available to both your app target and your capture extension target. Then you can embed the UI you want to use into your LockedCameraCaptureUIScene.

Just keep in mind that your existing UI may need modification in some important ways, such as transitioning the user to the app when they attempt to do something the extension cannot not support.

Hi codeblocknl,

Yup, that's expected. When the control is pressed, iOS will decide whether to open the app - while running the "perform" block of the CameraCaptureIntent - or the capture extension.

This can vary based on where the button was pressed, and whether the device was locked or unlocked.

For juliomartinezb:

We took a look at your provided test code and confirmed it works when "Privacy - Camera Usage Description" and a valid string value for it is added to the Capture Extension.

BOTH the app and the capture extension need this!

Once you do this, and make sure that you have camera permission in your app, you should see the capture extension launch. 🚀 😊

Currently I still got an issue that when you press Use Photo button after taking the image, the Capture Extension crashes or freezes

Hey codeblocknl:

UIImagePickerController is only an example for Capture Extensions as a starting point. The example code provided doesn't really go anywhere, so to speak, since it's designed to be a modal popup for a larger app. So any behavior that would dismiss the view, like selecting "Use Photo," will cause some wacky behavior. That's expected.

If you want to go deeper into Capture Extensions, the next step would be to start working with AVFoundation's camera API's yourself.

Hello,

I experienced several of the issues described here but the explanations from the Frameworks Engineer were very helpful to debugging, thank you (I think it's retrospectively obvious, but not obvious in the moment, that the app and extension both need the privacy camera usage strings and that the main app has to first receive camera permissions from the user before the extension can inherit them, so as an enhancement I would suggest adding it to the documentation or emphasizing it more if it is in there, along with a request to add a full working example of the implementation of the control widget, and I also stumbled on adding the Widget kind to the WidgetKit env vars in its scheme, although the Xcode error messages were helpful).

My extension and control widget are now working, but I filed feedback FB13958518 about the pin unlock numpad popping up over the extension viewfinder when it works; maybe you would be able to check it out.

Thanks again!

Hi Halle,

Thank you for filling a feedback! The issue you mention is a known issue, listed on the Xcode 16 Release Notes:

https://developer.apple.com/documentation/xcode-release-notes/xcode-16-release-notes

It's under investigation, stay tuned 😀

Your documentation feedback is also appreciated. I'll pass it on.

Whoops, sorry for filing a known issue! Thanks for checking it out, regardless.

I've provided camera usage descriptions for both my app and extension, but the control only opens the app.

@Sridharan

Keep in mind that the control may open the app or the capture extension depending on the context. For example: if your extension doesn't have permission to access the camera, the app will always be launched.

You'll get the capture extension if the device is locked and camera permission is provided. 😀

Hello, I am using a custom UI. It can be opened normally, but the extension will be automatically closed after a while. How to open the main app with custom buttons?

struct CaptureEx: LockedCameraCaptureExtension { var body: some LockedCameraCaptureExtensionScene { LockedCameraCaptureUIScene { session in // CaptureExViewFinder(session: session) MyUIKitView(session: session) } } }

struct MyUIKitView: UIViewControllerRepresentable { let session: LockedCameraCaptureSession func makeUIViewController(context: Context) -> MyUiViewController { let viewController = MyUiViewController() return viewController }

func updateUIViewController(_ uiViewController: MyUiViewController, context: Context) { }

}

class MyUiViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.red
    
    // 创建按钮
           let button = UIButton(type: .system)
           
           // 设置按钮的标题
           button.setTitle("点击我", for: .normal)
           
           // 设置按钮的大小和位置
           button.frame = CGRect(x: 100, y: 100, width: 200, height: 50)
           
           // 添加点击事件处理程序
           button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
           
           // 将按钮添加到视图中
           self.view.addSubview(button)
}

@objc func buttonTapped() {
    // 按钮点击事件的处理
    self.openHost(with: URL("myapp://"))
}

}

@yangshaohua

If your "MyUiViewController" does not implement a camera view from AVFoundation, the extension will be closed by the system in 10 seconds. Make sure to review all of the requirements of capture extensions here:

https://developer.apple.com/documentation/lockedcameracapture/creating-a-camera-experience-for-the-lock-screen

If you want to create a button that opens the app, you can use:

session.openApplication(userActivity: NSUserActivity)

Where "session" is LockedCameraCaptureSession.

I use AVCapture Session and it still closes after 10 seconds. Use UIImagePickerController to avoid closing

LockedCameraCapture - Lock Screen Camera Capture UI Question
 
 
Q