Hello!
I have a destination navigation which is TabVIew where each tab item is ScrollView. And when scrolling content of any of tab items is underneath navigation bar its background is always hidden. But at the same time tab bar background is toggled depending on scrolling content position.
I expected it would work with TabView the same as with any other view.
Is it supposed to work like that?
Swift
RSS for tagSwift is a powerful and intuitive programming language for Apple platforms and beyond.
Posts under Swift tag
200 Posts
Sort by:
Post
Replies
Boosts
Views
Activity
Hello!!in the terms of swift challenge say that is required to be enroledl to the apple developer program paid or free version (to join to developer programs you need to be under 18 years old).How over 18 years old can join to swift challenge if he can't make apple developer account.
Fatal Error in Swift Playground
Description
I'm experiencing a catastrophic error when importing Package Dependency in any Swift Playgrounds that has icon or name that caused the whole Playground won't work anymore with error messages below.
I'm current running macOS Sequoia 15.3 (24D60) and Swift Playgrounds 4.6.1. They're all up-to-date.
Reproduction
Open Swift Playgrounds and and create a new project.
Import a package dependency
https://github.com/simibac/ConfettiSwiftUI.git
Rename the project and add an icon
Then you should able the reproduce the problem. I strongly believed that this is a serious bug.
You'll find that Assets in the left column are disappeared and appeared Assets.xcassets, you're unable to reveal the Dependency in the column like the reference picture above. The whole Playground is destroyed now and unable to work anymore.
Topic:
Developer Tools & Services
SubTopic:
Swift Playground
Tags:
Swift Packages
Swift
Swift Playground
Hello,
I am developing an app for the Swift Student challenge; however, I keep encountering an error when using ClassifyImageRequest from the Vision framework in Xcode:
VTEST: error: perform(_:): inside 'for await result in resultStream' error: internalError("Error Domain=NSOSStatusErrorDomain Code=-1 \"Failed to create espresso context.\" UserInfo={NSLocalizedDescription=Failed to create espresso context.}")
It works perfectly when testing it on a physical device, and I saw on another thread that ClassifyImageRequest doesn't work on simulators. Will this cause problems with my submission to the challenge?
Thanks
Topic:
Machine Learning & AI
SubTopic:
General
Tags:
Swift Student Challenge
Swift
Swift Playground
Vision
I'm unable to rename my Swift Playground or add/change the app icon when I imported a package dependency after upgraded my Swift Playgrounds App to the newest version. Every time I tried to do that, the whole project will be destroyed and showed error message below.
I wonder if anyone have same experience with me or someone can tell me the solution, it terrifies me and I'm worried that I'll be unable to submit my project on time because of that.
I have an alertController that is presented as popover on iPad
let alertController = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: alertStyle)
if let ppc = alertController.popoverPresentationController {
// …
}
Is it possible to change the message font color (which is really very light on iPad) ?
It is OK on iPhone with the same alert (not popover): text is much more readable:
In mainland China, CallKit is not available. Recently, I discovered LiveCommunicationKit.
Can it replace CallKit?
There is no relevant introduction in the documentation. Can someone help me understand how to use it?
https://developer.apple.com/documentation/livecommunicationkit
I am developing an iOS application that supports screen mirroring to Google TV (or Chromecast with Google TV). My goal is to mirror the iPhone/iPad screen in real time to a Google TV device.
What I Have Tried So Far
I have explored multiple approaches but haven't found a direct way to achieve low-latency screen mirroring. Here are some of my findings:
Google Cast SDK:
Google Cast SDK is primarily designed for casting media (videos, images, audio) rather than real-time mirroring. It supports custom receiver applications, but there are no direct APIs for full screen mirroring. Casting a recorded video is possible, but it introduces latency and is not real-time.
ReplayKit for Screen Capture:
RPScreenRecorder.shared().startCapture(handler: ...) allows capturing the iPhone screen as a video stream. However, sending this stream to Google TV in real time is a challenge. I could potentially encode the video as HLS and stream it, but the delay is significant.
RTSP/UDP Streaming:
Some third-party libraries support RTSP/UDP streaming for real-time screen sharing. Google TV does not natively support RTSP, making this approach difficult.
My Questions:
Is it possible to achieve real-time screen mirroring on Google TV using Google Cast SDK? Does Google TV support WebRTC or any low-latency streaming protocol that can be used from iOS? Are there any alternative approaches to mirror an iOS screen to Google TV with minimal latency? I would appreciate any guidance, code examples, or references to relevant documentation.
I am currently developing a custom-protocol VPN application for iOS using PacketTunnelProvider. I have also integrated an HTTP proxy service, which is launched via a dylib.
The overall flow is as follows:
App -> VPN TUN -> Local HTTP Proxy -> External Network
I have a question:
I am capturing all traffic, and normally, requests sent out by the HTTP proxy are also captured again by the VPN. However, when I send requests using createUdpSession in my code, they are not being captured by the virtual interface (TUN).
What could be the reason for this?
override func startTunnel(options: [String : NSObject]?, completionHandler: @escaping (Error?) -> Void) {
let tunnelNetworkSettings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: "192.168.18.0")
tunnelNetworkSettings.mtu=1400
let ipv4Settings = NEIPv4Settings(addresses: ["192.169.10.10"], subnetMasks: ["255.255.255.0"])
ipv4Settings.includedRoutes=[NEIPv4Route.default()]
ipv4Settings.excludedRoutes = [NEIPv4Route(destinationAddress: "10.0.0.0", subnetMask: "255.0.0.0"),
NEIPv4Route(destinationAddress: "172.16.0.0", subnetMask: "255.240.0.0"),
NEIPv4Route(destinationAddress: "192.168.0.0", subnetMask: "255.255.0.0"),
NEIPv4Route(destinationAddress:"127.0.0.0", subnetMask: "255.0.0.0"),
]
tunnelNetworkSettings.ipv4Settings = ipv4Settings
// Configure proxy settings
let proxySettings = NEProxySettings()
proxySettings.httpEnabled = true
proxySettings.httpServer = NEProxyServer(address: "127.0.0.1", port: 7890)
proxySettings.httpsEnabled = true
proxySettings.httpsServer = NEProxyServer(address: "127.0.0.1", port: 7890)
proxySettings.excludeSimpleHostnames = true
proxySettings.exceptionList=["localhost","127.0.0.1"]
tunnelNetworkSettings.proxySettings = proxySettings
setTunnelNetworkSettings(tunnelNetworkSettings) { [weak self] error in
if error != nil {
completionHandler(error)
return
}
completionHandler(nil)
let stack = TUNInterface(packetFlow: self!.packetFlow)
RawScoketFactory.TunnelProvider=self
stack.register(stack: UDPDirectStack())
stack.register(stack: TCPDirectStack())
stack.start()
}
}
NWUdpSession.swift
//
// NWUDPSocket.swift
// supervpn
//
// Created by TobbyQuinn on 2025/2/3.
//
import Foundation
import NetworkExtension
import CocoaLumberjack
public protocol NWUDPSocketDelegate: AnyObject{
func didReceive(data:Data,from:NWUDPSocket)
func didCancel(socket:NWUDPSocket)
}
public class NWUDPSocket:NSObject{
private let session:NWUDPSession
private let timeout:Int
private var pendingWriteData: [Data] = []
private var writing = false
private let queue:DispatchQueue=QueueFactory.getQueue()
public weak var delegate:NWUDPSocketDelegate?
public init?(host:String,port:UInt16,timeout:Int=Opt.UDPSocketActiveTimeout){
guard let udpSession = RawScoketFactory.TunnelProvider?.createUDPSession(to: NWHostEndpoint(hostname: host, port: "\(port)"), from: nil) else{
return nil
}
session = udpSession
self.timeout=timeout
super.init()
session.addObserver(self, forKeyPath: #keyPath(NWUDPSession.state),options: [.new], context: nil)
session.setReadHandler({ dataArray, error in
self.queueCall{
guard error == nil, let dataArray = dataArray else {
print("Error when reading from remote server or connection reset")
return
}
for data in dataArray{
self.delegate?.didReceive(data: data, from: self)
}
}
}, maxDatagrams: 32)
}
/**
Send data to remote.
- parameter data: The data to send.
*/
public func write(data: Data) {
pendingWriteData.append(data)
checkWrite()
}
public func disconnect() {
session.cancel()
}
public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard keyPath == "state" else {
return
}
switch session.state {
case .cancelled:
queueCall {
self.delegate?.didCancel(socket: self)
}
case .ready:
checkWrite()
default:
break
}
}
private func checkWrite() {
guard session.state == .ready else {
return
}
guard !writing else {
return
}
guard pendingWriteData.count > 0 else {
return
}
writing = true
session.writeMultipleDatagrams(self.pendingWriteData) {_ in
self.queueCall {
self.writing = false
self.checkWrite()
}
}
self.pendingWriteData.removeAll(keepingCapacity: true)
}
private func queueCall(block:@escaping ()->Void){
queue.async {
block()
}
}
deinit{
session.removeObserver(self, forKeyPath: #keyPath(NWUDPSession.state))
}
}
I am interested in participating in the Swift Student Challenge. My application contains a significant amount of augmented reality (AR) content, necessitating access to the camera. It is evident that if the reviewer utilizes a simulator or operates on a Mac, they will not be able to experience the AR function. Therefore, the AR function in the camera experience application must be utilized to access a real iPad.
However, it is mentioned in https://developer.apple.com/forums/thread/773530 that the plan is to evaluate Xcode app playgrounds within the simulator. Additionally, I observed the statement “Note: Xcode app playgrounds are executed in Simulator” on the submission page. Consequently, it is clear that the reviewers are limited to using a simulator or running my application on a Mac.
In light of this, I am seeking guidance on how to enable the reviewer to utilize a real iPad to access the AR function in the camera experience application. Alternatively, I may need to reconsider my strategy and discontinue utilizing AR.
Topic:
Community
SubTopic:
Swift Student Challenge
Tags:
Swift Student Challenge
Swift
Swift Playground
Swans Quest
The App has the ability to use WebKit and display web pages and the ability to add phone numbers to CallDirectory at specific timing.
In this App, when the App is launched or when the Add Contact button on the web page is pressed,
CallDirectoryExtention is reloaded from the host app (WebKit-viewController), the phone number is retrieved from the server, and the entry is updated.
I would like to add a process to remove the already added phone number entry when a specific value is retrieved from the application server.
The specific process we wish to implement is as follows.
Step 1: Use URLsession to retrieve values from the application server. (ViewController)
Step 2: If the value is a specific value, call a Function that deletes the CallDirectoryExtention entry. (ViewController)
Step 3: Delete all entries for the registered phone numbers.
However, I am aware that I have to use reloadExtension() to call the CallDirectoryExtention process from ViewController on Step2.
if I do so, CallDirectoryHandler.beginRequest() will be processed, and I am wondering if it is not possible to execute only the Function that deletes the entry.
Is there a way to run only the Function that deletes the CallDirectoryExtention entry from the host app(viewController)?
In this setup, label do not show properly because of the textColor.
Labels are defined in IB, in the following hierarchy:
ViewController
View
Label 1
scrollView
View
Button
Label 2
Buttons show properly, but labels, even though defined with default label color appear as if their alpha was 0.2. It is even worse in dark mode:
I have checked the settings for the label and did not find anything anormal:
I have tried to change label color to system.gray 2, to no avail. If I change to red, does not show in red in IB.
Problem appears for both Label 1 (at the top level in the view) and Label 2
It was mentioned in the Swift Student Challenge that outstanding winners will have the opportunity to visit Apple Park in the United States. However, as a challenger from China who is not currently in the U.S., this means that if I receive the outstanding award, I will need to apply for a visa to travel to Apple Park. Since I am under 18, my guardian would also need to apply for a visa. Therefore, I would like to know if Apple provides visa assistance for outstanding winners and their guardians from China, or if we are responsible for applying for the visas on our own.
Topic:
Community
SubTopic:
Swift Student Challenge
Tags:
Swift Student Challenge
Swift
Swift Playground
DocC
When integrating SwiftData for an already existing app that uses CoreData as data management, I encounter errors.
When building the ModelContainer for the first time, the following error appears:
Error: Persistent History (184) has to be truncated due to the following entities being removed (all Entities except for the 2 where I defined a SwiftData Model)
class SwiftDataManager: ObservableObject {
static let shared = SwiftDataManager()
private let persistenceManager = PersistenceManager.shared
private init(){}
lazy var modelContainer: ModelContainer = {
do {
let storeUrl = persistenceManager.storeURL()
let schema = Schema([
HistoryIncident.self,
HistoryEvent.self
])
let modelConfig = ModelConfiguration(url: storeUrl)
return try ModelContainer(for: schema, configurations: [modelConfig])
} catch {
fatalError("Could not create ModelContainer: \(error)")
}
}()
}
@Model public class HistoryIncident {
var missionNr: String?
@Relationship(deleteRule: .cascade) var events: [HistoryEvent]?
public init(){}
}
@Model class HistoryEvent {
var decs: String?
var timestamp: Date?
init(){}
}
As soon as I call the following function.
func addMockEventsToCurrentHistorie() {
var descriptor = FetchDescriptor<HistoryIncident>()
let key = self.hKey ?? ""
descriptor.predicate = #Predicate { mE in
key == mE.key
}
let historyIncident = try? SwiftDataManager.shared.modelContext.fetch(descriptor).first
guard var events = historyIncident?.events else {return}
events.append(contentsOf: createEvents())
}
I get the error:
CoreData: error: (1) I/O error for database at /var/mobile/Containers/Data/Application/55E9D59D-48C4-4D86-8D9F-8F9CA019042D/Library/ Private Documents/appDatabase.sqlite. SQLite error code:1, 'no such column: t0.Z1EVENTS'
/var/mobile/Containers/Data/Application/55E9D59D-48C4-4D86-8D9F-8F9CA019042D/Library/ Private Documents/appDatabase.sqlite. SQLite error code:1, 'no such column: t0.Z1EVENTS' with userInfo of { NSFilePath = "/var/mobile/Containers/Data/Application/55E9D59D-48C4-4D86-8D9F-8F9CA019042D/Library/ Private Documents/appDatabase.sqlite"; NSSQLiteErrorDomain = 1; }
Hello everyone. I'm building a simple Form in a Multiplatform App with SwiftUI. Originally I had something like this.
import SwiftUI
struct OnboardingForm: View {
@State var firstName: String = ""
@State var lastName: String = ""
@State var email: String = ""
@State var job: String = ""
@State var role: String = ""
var body: some View {
Form {
TextField("First Name", text: $firstName, prompt: Text("Required"))
TextField("Last Name", text: $lastName, prompt: Text("Required"))
TextField("Email", text: $email, prompt: Text("Required"))
TextField("Job", text: $job, prompt: Text("Required"))
TextField("Role", text: $role, prompt: Text("Required"))
}
}
}
#Preview {
OnboardingForm()
}
In macOS it looks ok but then in iOS it looks like this:
and it's impossible to know what each field is for if all the prompts are the same. I tried adding LabeledContent around each text field and that solves it for iOS but then on macOS it looks like this:
The labels are shown twice and the columns are out of alignment. I think I could get around it by doing something like this:
#if os(iOS)
LabeledContent {
TextField("First Name", text: $firstName, prompt: Text("Required"))
} label: {
Text("First Name")
}
#else
TextField("First Name", text: $firstName, prompt: Text("Required"))
#endif
but it seems to me like reinventing the wheel. Is there a "correct" way to declare TextFields with labels that works for both iOS and macOS?
Summary
I have a SwiftUI Chart that worked correctly in iOS 17, allowing both horizontal scrolling and tap gesture selection. However, in iOS 18, the same exact chart will not allow for both tap gestures and scrolling to work -- it's either we allow scrolling or we allow tap gestures but not both. We have tried everything to try to circumvent this issue but have had to resort to old methods of creating the chart. This is an issue that has negatively impacted our customers as well.
Again, the charts were working fine on iOS 17, but on iOS 18 the chart scroll + tap gesture capability is not working.
Expected Behavior (iOS 17)
Users can scroll horizontally through the chart.
Users can tap on data points to highlight them.
The selected data point updates when tapped.
Observed Behavior (iOS 18)
The chart no longer scrolls when chartOverlay with the Tap Gesture is applied.
Tap selection still works as expected.
Code Snippet
Below is the working implementation from iOS 17:
private var iOS17ChartView: some View {
Chart {
RectangleMark(
yStart: .value(String(firstLevelAlertBand), firstLevelAlertBand),
yEnd: .value("100", 100)
)
.foregroundStyle(Theme.Colors.green.opacity(0.15))
RectangleMark(
yStart: .value(String(secondLevelAlertBand), secondLevelAlertBand),
yEnd: .value(String(firstLevelAlertBand), firstLevelAlertBand)
)
.foregroundStyle(Theme.Colors.orange.opacity(0.15))
RectangleMark(
yStart: .value("0", 0),
yEnd: .value(String(secondLevelAlertBand), secondLevelAlertBand)
)
.foregroundStyle(Theme.Colors.red.opacity(0.15))
ForEach(telemetryData, id: \.timestamp) { entry in
if let utcDate = dateFormatter.date(from: entry.timestamp) {
let localDate = convertToUserTimeZone(date: utcDate)
let tankLevel = entry.tankLevel ?? 0
LineMark(
x: .value("Date", localDate),
y: .value("Tank Level", tankLevel)
)
.foregroundStyle(statusColor)
AreaMark(
x: .value("Date", localDate),
y: .value("Tank Level", tankLevel)
)
.foregroundStyle(statusColor.opacity(0.50))
PointMark(
x: .value("Date", localDate),
y: .value("Tank Level", tankLevel)
)
.foregroundStyle(selectedDataPoint?.date == localDate ? Theme.Colors.primaryColor : statusColor)
.symbolSize(selectedDataPoint?.date == localDate ? 120 : 80)
PointMark(
x: .value("Date", localDate),
y: .value("Tank Level", tankLevel)
)
//.foregroundStyle(.white).symbolSize(10)
.foregroundStyle(Theme.Colors.white(colorScheme: colorScheme))
.symbolSize(12)
}
}
}
.chartXScale(domain: (firstTimestamp ?? Date())...(latestTimestamp ?? Date()))
.chartXVisibleDomain(length: visibleDomainSize)
.chartScrollableAxes(.horizontal)
.chartScrollPosition(x: $chartScrollPositionX)
.chartXAxis {
AxisMarks(values: .stride(by: xAxisStrideUnit, count: xAxisCount())) { value in
if let utcDate = value.as(Date.self) {
let localDate = convertToUserTimeZone(date: utcDate)
let formatStyle = self.getFormatStyle(for: interval)
AxisValueLabel {
Text(localDate, format: formatStyle)
.font(Theme.Fonts.poppinsRegularExtraSmall)
.foregroundStyle(Theme.Colors.black(colorScheme: colorScheme))
}
AxisTick()
.foregroundStyle(Theme.Colors.black(colorScheme: colorScheme).opacity(1))
}
}
}
.chartOverlay { proxy in
GeometryReader { geometry in
Rectangle().fill(Color.clear).contentShape(Rectangle())
.onTapGesture { location in
let xPosition = location.x - geometry[proxy.plotAreaFrame].origin.x
// Use proxy to get the x-axis value at the tapped position
if let selectedDate: Date = proxy.value(atX: xPosition) {
if let closestEntry = telemetryData.min(by: { abs(dateFormatter.date(from: $0.timestamp)!.timeIntervalSince1970 - selectedDate.timeIntervalSince1970) < abs(dateFormatter.date(from: $1.timestamp)!.timeIntervalSince1970 - selectedDate.timeIntervalSince1970) }) {
selectedDataPoint = (convertToUserTimeZone(date: dateFormatter.date(from: closestEntry.timestamp)!), closestEntry.tankLevel ?? 0)
if let dateXPos = proxy.position(forX: convertToUserTimeZone(date: dateFormatter.date(from: closestEntry.timestamp)!)),
let tankLevelYPos = proxy.position(forY: closestEntry.tankLevel ?? 0) {
// Offset the x-position based on the scroll position
let adjustedXPos = dateXPos - proxy.position(forX: chartScrollPositionX)!
withAnimation(.spring()) {
selectedPointLocation = CGPoint(x: adjustedXPos, y: tankLevelYPos - 60) // Offset popup above the point
showPopup = true
}
}
}
}
}
}
.onChange(of: chartScrollPositionX) { newValue in
// Dynamically update the popup position when scroll changes
if let selectedDataPoint = selectedDataPoint {
if let dateXPos = proxy.position(forX: selectedDataPoint.date) {
let adjustedXPos = dateXPos - proxy.position(forX: chartScrollPositionX)!
selectedPointLocation.x = adjustedXPos
}
}
}
}
}
Please help!
Nick
In an AppKit document-based project created by Xcode, setting canConcurrentlyReadDocuments to true allows new documents to open normally in Swift 5, but switching to Swift 6 causes an error.
Judging from the error message, it seems to be a threading issue, but I’m not sure how to adjust the code to support the Swift 6 environment.
The project is the most basic code from an Xcode-created document-based project without any modifications, except for changing the Swift version to 6 and setting canConcurrentlyReadDocuments to true.
Source code: https://drive.google.com/file/d/1ryb2TaU6IX884q0h5joJqqZwSX95Q335/view?usp=sharing
AppDelegate.swift
import Cocoa
@main
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification)
{
// Insert code here to initialize your application
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
return true
}
}
Document.swift
import Cocoa
class Document: NSDocument {
override init() {
super.init()
// Add your subclass-specific initialization here.
}
override class var autosavesInPlace: Bool {
return true
}
override class func canConcurrentlyReadDocuments(ofType typeName: String) -> Bool {
true
}
override func canAsynchronouslyWrite(to url: URL, ofType typeName: String, for saveOperation: NSDocument.SaveOperationType) -> Bool {
true
}
override func makeWindowControllers() {
// Returns the Storyboard that contains your Document window.
let storyboard = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: nil)
let windowController = storyboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("Document Window Controller")) as! NSWindowController
self.addWindowController(windowController)
}
override func data(ofType typeName: String) throws -> Data {
// Insert code here to write your document to data of the specified type, throwing an error in case of failure.
// Alternatively, you could remove this method and override fileWrapper(ofType:), write(to:ofType:), or write(to:ofType:for:originalContentsURL:) instead.
// throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
return Data()
}
override func read(from data: Data, ofType typeName: String) throws {
// Insert code here to read your document from the given data of the specified type, throwing an error in case of failure.
// Alternatively, you could remove this method and override read(from:ofType:) instead.
// If you do, you should also override isEntireFileLoaded to return false if the contents are lazily loaded.
// throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
}
}
ViewController.swift
import Cocoa
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}
By setting the PKCanvasView background color to blue, I can tell that the PKCanvasView for each PDFPage is created normally, but it does not respond to touch. Specifically, whether it is finger or applepencil, all the responses of the page occur from PDFView(such as zoom and scroll), and PKCanvasView can not draw, please how to solve?
class PDFAnnotatableViewController: UIViewController, PDFViewDelegate {
private let pdfView = PDFView()
private var pdfDocument: PDFDocument?
let file: FileItem
private var userSettings: UserSettings
@Binding var selectedPage: Int
@Binding var currentMode: Mode
@Binding var latestPdfChatResponse: LatestPDFChatResponse
@State private var pdfPageCoordinator = PDFPageCoordinator()
@ObservedObject var userMessage: ChatMessage
init(file: FileItem,
userSettings: UserSettings,
drawDataList: Binding<[DrawDataItem]>,
selectedPage: Binding<Int>,
currentMode: Binding<Mode>,
latestPdfChatResponse: Binding<LatestPDFChatResponse>,
userMessage: ChatMessage) {
self.file = file
self.userSettings = userSettings
self._selectedPage = selectedPage
self._currentMode = currentMode
self._latestPdfChatResponse = latestPdfChatResponse
self.userMessage = userMessage
super.init(nibName: nil, bundle: nil)
DispatchQueue.global(qos: .userInitiated).async {
if let document = PDFDocument(url: file.pdfLocalUrl) {
DispatchQueue.main.async {
self.pdfDocument = document
self.pdfView.document = document
self.goToPage(selectedPage: selectedPage.wrappedValue - 1)
}
}
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
setupPDFView()
}
private func setupPDFView() {
pdfView.delegate = self
pdfView.autoScales = true
pdfView.displayMode = .singlePage
pdfView.displayDirection = .vertical
pdfView.backgroundColor = .white
pdfView.usePageViewController(true)
pdfView.displaysPageBreaks = false
pdfView.displaysAsBook = false
pdfView.minScaleFactor = 0.8
pdfView.maxScaleFactor = 3.5
pdfView.pageOverlayViewProvider = pdfPageCoordinator
if let document = pdfDocument {
pdfView.document = document
goToPage(selectedPage: selectedPage)
}
pdfView.frame = view.bounds
pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(pdfView)
NotificationCenter.default.addObserver(
self,
selector: #selector(handlePageChange),
name: .PDFViewPageChanged,
object: pdfView
)
}
// Dealing with page turning
@objc private func handlePageChange(notification: Notification) {
guard let currentPage = pdfView.currentPage, let document = pdfView.document else { return }
let currentPageIndex = document.index(for: currentPage)
if currentPageIndex != selectedPage - 1 {
DispatchQueue.main.async {
self.selectedPage = currentPageIndex + 1
}
}
}
func goToPage(selectedPage: Int) {
guard let document = pdfView.document else { return }
if let page = document.page(at: selectedPage) {
pdfView.go(to: page)
}
}
// Switch function
func togglecurrentMode(currentMode: Mode){
DispatchQueue.main.async {
if self.currentMode == .none{
self.pdfView.usePageViewController(true)
self.pdfView.isUserInteractionEnabled = true
} else if self.currentMode == .annotation {
if let page = self.pdfView.currentPage {
if let canvasView = self.pdfPageCoordinator.getCanvasView(forPage: page) {
canvasView.isUserInteractionEnabled = true
canvasView.tool = PKInkingTool(.pen, color: .red, width: 20)
canvasView.drawingPolicy = .anyInput
canvasView.setNeedsDisplay()
}
}
}
}
}
}
class MyPDFPage: PDFPage {
var drawing: PKDrawing?
func setDrawing(_ drawing: PKDrawing) {
self.drawing = drawing
}
func getDrawing() -> PKDrawing? {
return self.drawing
}
}
class PDFPageCoordinator: NSObject, PDFPageOverlayViewProvider {
var pageToViewMapping = [PDFPage: PKCanvasView]()
func pdfView(_ view: PDFView, overlayViewFor page: PDFPage) -> UIView? {
var resultView: PKCanvasView? = nil
if let overlayView = pageToViewMapping[page] {
resultView = overlayView
} else {
let canvasView = PKCanvasView(frame: view.bounds)
canvasView.drawingPolicy = .anyInput
canvasView.tool = PKInkingTool(.pen, color: .systemYellow, width: 20)
canvasView.backgroundColor = .blue
pageToViewMapping[page] = canvasView
resultView = canvasView
}
if let page = page as? MyPDFPage, let drawing = page.drawing {
resultView?.drawing = drawing
}
return resultView
}
func pdfView(_ pdfView: PDFView, willEndDisplayingOverlayView overlayView: UIView, for page: PDFPage) {
guard let overlayView = overlayView as? PKCanvasView, let page = page as? MyPDFPage else { return }
page.drawing = overlayView.drawing
pageToViewMapping.removeValue(forKey: page)
}
func savePDFDocument(_ pdfDocument: PDFDocument) -> Data {
for i in 0..<pdfDocument.pageCount {
if let page = pdfDocument.page(at: i) as? MyPDFPage, let drawing = page.drawing {
let newAnnotation = PDFAnnotation(bounds: drawing.bounds, forType: .stamp, withProperties: nil)
let codedData = try! NSKeyedArchiver.archivedData(withRootObject: drawing, requiringSecureCoding: true)
newAnnotation.setValue(codedData, forAnnotationKey: PDFAnnotationKey(rawValue: "drawingData"))
page.addAnnotation(newAnnotation)
}
}
let options = [PDFDocumentWriteOption.burnInAnnotationsOption: true]
if let resultData = pdfDocument.dataRepresentation(options: options) {
return resultData
}
return Data()
}
func getCanvasView(forPage page: PDFPage) -> PKCanvasView? {
return pageToViewMapping[page]
}
}
Is there an error in my code? Please tell me how to make PKCanvasView painting normally?
Hello!
I have a simple app that opens a sheet and when you press a button on the sheet it will open a quick look preview of a picture. That works great but when I exit the quick look preview it will close the sheet too. This seems like unexpected behavior because it doesn't happen on iOS.
Any help is appreciated, thank you.
Here is some simple repo:
import QuickLook
import SwiftUI
struct ContentView: View {
@State private var pictureURL: URL?
@State private var openSheet = false
var body: some View {
Button("Open Sheet") {
openSheet = true
}
.sheet(isPresented: $openSheet) {
Button("Open Picture") {
pictureURL = URL(fileURLWithPath: "someImagePath")
}
// When quick look closes it will close the sheet too.
.quickLookPreview($pictureURL)
}
}
}
And here is a quick video:
I get a crash in Apple Watch simulator (Series 9 45mm 18.0) as soon as the app launch if I type anything on external keyboard (just hitting command key for instance to capture a screenshot). Same crash on series 7 (45mm, OS 18.1)
But app works normally when I use mouse to interact with the app on simulator.
App does not crash on real device (Watch 4 OS 10.4.1).
Nor does it crash on Series 6 simulator (44 mm OS 17.4).
Here are the log I could collect (apparently, they contain sensitive language !!! so I attach as a file.:
Attached logs