Construct and manage graphical, event-driven user interfaces for iOS or tvOS apps using UIKit.

UIKit Documentation

Post

Replies

Boosts

Views

Activity

A glitch with retained view controllers on Catalyst
Hello! I discovered a bug on Catalyst about a three years ago but it still seems to be not fixed. My bug report number is FB9705748. The Internet is silent on this so I'm even not sure, perhaps it's only me. So to the problem. When you display UICollectionViewController or UIViewController that contains UICollectionView, interact with the collection view then dismiss the view controller, the displayed view controller isn't released if dismissal is done through navigation bar item. The problem occurs only when the run target is My Mac (Mac Catalyst). Everything is fine when you run on iOS or via My Mac (Designed for iPad). The sample project is uploaded to GitHub. It has a video that shows this strange behavior, see the log for 'deinit' messages. I did have some workaround to fix this but it stops to work, presumable on the new macOS. Also, chances are that it's not only UICollectionView which initiates the glitch, it's just that I only encounter it with collection views.
1
0
192
Sep ’24
[UITextView becomeFirstResponder] crash with [__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]
I am trying to set my UITextView as the first responder with [self.textView becomeFirstResponder] when my view controller called viewDidAppear. But sometimes it will cause crash with the error: [__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0] all I did is just: - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self.textView becomeFirstResponder]; } So if anyone can tell me what happened and how to do? when I call the [self.textView becomeFirstResponder], what will be insert into the responders list? self.textView itself? Thanks very much!
0
0
203
Sep ’24
Drag&Drop broken in iOS18.0?
Hi, I've encountered the following phenomenon when comparing drag&drop in iOS 18.0 with iOS 17.5: Let's say you have a small child view sitting on top of a larger parent view. In iOS 18.0, when hovering over the child view with a drag object and leaving the child view, the child view will propagate a "dropInteraction:sessionDidExit:" call to the UIDropInteractionDelegate of its parent view. The parent view now thinks, that the drag object is moved away from it (which is not the case!) and in turn its delegate doesn't receive any "dropInteraction:performDrop:" calls anymore. In iOS 17.5 this case is handled correctly (without "dropInteraction:sessionDidExit:" being propagated wrongly by child views to their parent views). Is this change in behavior intended or is this a bug?
1
0
203
Sep ’24
iOS App is crashing
My iOS app crashes, when kept idle in the background for sometime. Below is the detailed crash report. Incident Identifier: 90DF0404-7A1D-4AF3-8892-19AB744DF0FD Distributor ID: com.apple.AppStore Hardware Model: iPhone13,3 Process: Runner [35073] Path: /private/var/containers/Bundle/Application/894B2440-D20C-4C01-B278-A0DC4B199530/Runner.app/Runner Identifier: com.era.tk Version: 5.0.2 (5) AppStoreTools: 15F31e AppVariant: 1:iPhone13,3:15 Code Type: ARM-64 (Native) Role: Foreground Parent Process: launchd [1] Coalition: com.era.tk [793] Date/Time: 2024-09-25 13:17:31.3101 +0100 Launch Time: 2024-09-25 12:29:23.1866 +0100 OS Version: iPhone OS 17.6.1 (21G93) Release Type: User Baseband Version: 4.70.01 Report Version: 104 Exception Type: EXC_BREAKPOINT (SIGTRAP) Exception Codes: 0x0000000000000001, 0x0000000100c75e48 Termination Reason: SIGNAL 5 Trace/BPT trap: 5 Terminating Process: exc handler [35073] Triggered by Thread: 0 Thread 0 Crashed: 0 Runner 0x0000000100c75e48 0x100c2c000 + 302664 1 Runner 0x0000000100c75e80 0x100c2c000 + 302720 2 UIKitCore 0x00000001a7e937e8 -[UIViewController _setViewAppearState:isAnimating:] + 1224 (UIViewController.m:6441) 3 UIKitCore 0x00000001a80520b0 -[UIViewController __viewWillDisappear:] + 108 (UIViewController.m:6705) 4 UIKitCore 0x00000001a8602cec -[UINavigationController _startCustomTransition:] + 1476 (UINavigationController.m:2260) 5 UIKitCore 0x00000001a7e97400 -[UINavigationController _startDeferredTransitionIfNeeded:] + 496 (UINavigationController.m:8062) 6 UIKitCore 0x00000001a7efc248 -[UINavigationController __viewWillLayoutSubviews] + 96 (UINavigationController.m:8369) 7 UIKitCore 0x00000001a81be688 -[UILayoutContainerView layoutSubviews] + 172 (UILayoutContainerView.m:89) 8 UIKitCore 0x00000001a7da7918 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1528 (UIView.m:20054) 9 QuartzCore 0x00000001a720626c CA::Layer::layout_if_needed(CA::Transaction*) + 504 (CALayer.mm:10816) 10 QuartzCore 0x00000001a7205df0 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 148 (CALayer.mm:2598) 11 QuartzCore 0x00000001a7260fd8 CA::Context::commit_transaction(CA::Transaction*, double, double*) + 464 (CAContextInternal.mm:2760) 12 QuartzCore 0x00000001a71d5ee0 CA::Transaction::commit() + 648 (CATransactionInternal.mm:432) 13 QuartzCore 0x00000001a721fc34 CA::Transaction::flush_as_runloop_observer(bool) + 88 (CATransactionInternal.mm:942) 14 UIKitCore 0x00000001a7e50ee8 _UIApplicationFlushCATransaction + 52 (UIApplication.m:3181) 15 UIKitCore 0x00000001a7e4e660 _UIUpdateSequenceRun + 84 (_UIUpdateSequence.mm:119) 16 UIKitCore 0x00000001a7e4e2a4 schedulerStepScheduledMainSection + 172 (_UIUpdateScheduler.m:1058) 17 UIKitCore 0x00000001a7e4f148 runloopSourceCallback + 92 (_UIUpdateScheduler.m:1221) 18 CoreFoundation 0x00000001a5b6b834 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 28 (CFRunLoop.c:1957) 19 CoreFoundation 0x00000001a5b6b7c8 __CFRunLoopDoSource0 + 176 (CFRunLoop.c:2001) 20 CoreFoundation 0x00000001a5b69298 __CFRunLoopDoSources0 + 244 (CFRunLoop.c:2038) 21 CoreFoundation 0x00000001a5b68484 __CFRunLoopRun + 828 (CFRunLoop.c:2955) 22 CoreFoundation 0x00000001a5b67cd8 CFRunLoopRunSpecific + 608 (CFRunLoop.c:3420) 23 GraphicsServices 0x00000001ea5b51a8 GSEventRunModal + 164 (GSEvent.c:2196) 24 UIKitCore 0x00000001a81a1ae8 -[UIApplication _run] + 888 (UIApplication.m:3713) 25 UIKitCore 0x00000001a8255d98 UIApplicationMain + 340 (UIApplication.m:5303) 26 Runner 0x0000000100c4e660 0x100c2c000 + 140896 27 dyld 0x00000001c933f154 start + 2356 (dyldMain.cpp:1298)
1
0
174
Sep ’24
iOS 18 ShareExtension openURL:
When I write code in shareExtension like below: dispatch_async(dispatch_get_global_queue(0, 0), ^{ UIResponder *responder = [self nextResponder]; while ((responder = [responder nextResponder]) != nil) { if ([responder respondsToSelector:@selector(openURL:)] == YES) { [responder performSelector:@selector(openURL:) withObject:[NSURL URLWithString:APP_SCHEME]]; break; } } }); Will output in the console: BUG IN CLIENT OF UIKIT: The caller of UIApplication.openURL(:) needs to migrate to the non-deprecated UIApplication.open(:options:completionHandler:). Force returning false (NO).
4
1
1.1k
Sep ’24
UISwitch initialization failure in SPM test for visionOS 2 & Xcode 16
In SPM package test running on visionOS 2, when creating a UISwitch we get a crash / error: Nil UISwitch visual element provided by <UISwitch: 0x102b15590; frame = (0 0; 0 0); layer = <CALayer: 0x6000004c1780>> (NSInternalInconsistencyException) This issue only occurs on visionOS 2 in Xcode 16 with Swift Package Manager. It works fine when running on visionOS 1.2 in Xcode 16 with SPM, as well as on iOS 18 in Xcode 16 with SPM. Additionally, running the test on visionOS inside of a non-SPM package (Project test) is also fine. What results you expected - The test should pass for UISwitch initialization on SPM for visionOS 2 on Xcode 16. What results you actually saw - Test failure with error - Nil UISwitch visual element provided by <UISwitch: 0x102b15590; frame = (0 0; 0 0); layer = <CALayer: 0x6000004c1780>> (NSInternalInconsistencyException) The version of Xcode you are using - Xcode 16 Feedback id - FB15254532
0
0
199
Sep ’24
UITabBarController didSelect item not working on iOS 18
I have been using the following function in my UITabBarController for several years, which runs some code when the user clicks a tab. I have now built the app using Xcode 16 and when I run it on my iPad with iPadOS 18, the function is not called. When I run the same app on iPadOS 16, it works just fine like it always has. Is this a bug, or is there something new that I need to do for iOS 18 support? override func tabBar(_tabBar: UITabBar, didSelect item: UITabBarItem) { // Some code } NOTE: My UITabBarController is presented from my root view controller, which is a UINavigationController.
2
1
389
Sep ’24
How to transition & resize a view from one view controller to another using UIKit & Snapshots?
I am trying to seamlessly transition a view from one view controller and have it "jump" into a sheet. Apple does this in their widget picker for example when you add one of the top widgets. Based on all the documentation I've read, the most common approach to this is to snapshot the area/view that you are trying to seamlessly transition in the presented view controller (the place where the item is coming from. And then have that snapshot translate across into where it should be in the layout of the presenting view controller. func makeAnimatorIfNeeded( using transitionContext: UIViewControllerContextTransitioning ) -> UIViewPropertyAnimator { if let animator = animator { return animator } // Presentation animation let animator = UIViewPropertyAnimator( duration: 0.5, timingParameters: UISpringTimingParameters(dampingRatio: 1.0) ) guard let fromVC = transitionContext.viewController(forKey: .from), let toVC = transitionContext.viewController(forKey: .to) else { transitionContext.completeTransition(false) return animator } /// !IMPORTANT: For some weird reason, accessing the views of the view controller /// directly, like `fromVC.view` or `toVC.view` will break the sheet transition and gestures. /// Instead, use the `view(forKey:)` method of the `transitionContext` to get the views. let fromView = transitionContext.view(forKey: .from) let toView = transitionContext.view(forKey: .to) let containerView = transitionContext.containerView let fromFrame = transitionContext.viewController(forKey: .from).map { transitionContext.finalFrame(for: $0) } ?? containerView.bounds let toFrame = transitionContext.viewController(forKey: .to).map { transitionContext.finalFrame(for: $0) } ?? containerView.bounds // Calculate the frame of sourceView relative to fromVC.view to capture the correct snapshot area. let snapshotFrameInFromVC = sourceView?.convert(sourceView?.frame ?? .zero, to: fromVC.view) ?? containerView.frame // Calculate the frame of sourceView relative to containerView to position the snapshot correctly. let snapshotFrameInContainer = sourceView?.convert(sourceView?.frame ?? .zero, to: containerView) ?? containerView.frame let snapshot: UIView? if isPresenting { // Create a snapshot of fromVC.view from the defined area (snapshotFrameInFromVC). let originalColor = fromVC.view.backgroundColor fromVC.view.backgroundColor = .clear snapshot = fromVC.view.resizableSnapshotView(from: snapshotFrameInFromVC, afterScreenUpdates: true, withCapInsets: .zero) fromVC.view.backgroundColor = originalColor // Position the snapshot correctly within the snapshot container snapshot?.frame = snapshotFrameInContainer toView?.frame = toFrame toView?.transform = CGAffineTransform(translationX: 0, y: containerView.frame.size.height) toView?.layoutIfNeeded() if let fromView { containerView.addSubview(fromView) } if let toView { containerView.addSubview(toView) containerView.addSubview(snapshot ?? UIView()) } let toViewCenter = CGPoint( x: toVC.view.bounds.midX, y: toVC.view.bounds.midY + 55 ) let gestureVelocity = CGPoint(x: 0, y: -5000) animator.addAnimations { Wave.animate( withSpring: self.animatedSpring, mode: .animated, gestureVelocity: gestureVelocity ) { snapshot?.animator.frame.size = CGSize(width: 204, height: 204) // Important to animate first snapshot?.animator.center = toViewCenter } completion: { finished, retargeted in print("finished: \(finished), retargeted: \(retargeted)") } toView?.transform = CGAffineTransform.identity } animator.addCompletion { animatingPosition in switch animatingPosition { case .end: snapshot?.removeFromSuperview() transitionContext.completeTransition(true) default: transitionContext.completeTransition(false) } } } else { // Transitioning view is fromView if let toView { containerView.addSubview(toView) } if let fromView { containerView.addSubview(fromView) } animator.addAnimations { fromView?.transform = CGAffineTransform(translationX: 0, y: containerView.frame.size.height) } animator.addCompletion { animatingPosition in switch animatingPosition { case .end: transitionContext.completeTransition(true) default: transitionContext.completeTransition(false) } } } self.animator = animator return animator } I can pull this off seamlessly if the animation involves a simple movement from A to B. But if I try to resize the snapshot as well, the snapshot becomes pixelated and low quality (I assume this is because the snapshot is literally a small screenshot and I'm stretching it from 56x56 to 204x204). Is there another way that I'm overlooking to pull this off without a snapshot? Or is there a way I can resize the snapshot without losing quality? Here is the animator code, it works great without the sizing so this should help future people looking to replicate the same effect anyways.
0
0
163
Sep ’24
Tapping a TextKit 2 backed UITextView moves the caret to a random location
With the upcoming launch of Apple Intelligence and Writing Tools, we've been forced to migrate our text editing app to TextKit 2. As soon as we released the update, we immediately got complaints about incorrect selection behaviour, where the user would tap a word to begin editing, but the caret would be shown in an undefined location, often dozens of paragraphs below the selected content. To reproduce: Create a UITextView backed by a standard TextKit 2 stack and a large amount of text (50,000+ words) - see sample project below Scroll quickly through the text view (at least 20% of the way down) Tap once to select a position in the document. Expected: The caret appears at the location the user tapped, and UITextView.selectedRange is the range of the text at the location of the tap. This is the behaviour of TextKit 1 based UITextViews. Actual: The caret is positioned at an undefined location (often completely off screen), and the selectedRange is different to the range at the location of the tap, often by several thousand. There is no pattern to the magnitude of the discrepancy. This incorrect behaviour occurs consistently in the sample project on the simulator, but you may need to hide the keyboard by pulling down, then repeat steps 2-3 a few times. This happens on iPhone and iPad, and on iOS 17, 18, and 18.1. Do you have any insight into why this might be happening or how to work around this issue? Sample code is here: https://github.com/nathantesler/textkit2-issue/tree/master
2
0
312
Sep ’24
UITextView changed sequence of views in iOS 18
UITextView changed the sequence of views and on the top UIView. As a result in the custom gesture class in "touchesBegan" method, this code brings me "let touchedView = touches.first?.view" type of view UIView but it must be my own custom class type. How can I get a tapped type of view? Help please override func touchesBegan(_ touches: Set, with event: UIEvent) { super.touchesBegan(touches, with: event) let touchedView = touches.first?.view }
1
0
170
Sep ’24
iOS 18.0 (22A3354) -[UIGestureRecognizer _delayTouchesForEvent:inPhase:] + 292 Crash
Exception Type: EXC_CRASH (SIGABRT) Exception Codes: 0x0000000000000000, 0x0000000000000000 Exception Reason: *** -[__NSArrayM insertObject:atIndex:]: object cannot be nil Termination Reason: SIGNAL 6 Abort trap: 6 Terminating Process:XXXX [XXXX] Triggered by Thread: 0 Application Specific Information: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil UserInfo:(null)' Last Exception Backtrace: 0 CoreFoundation 0x1930d108c __exceptionPreprocess + 164 (NSException.m:249) 1 libobjc.A.dylib 0x1903d32e4 objc_exception_throw + 88 (objc-exception.mm:356) 2 CoreFoundation 0x193070424 -[__NSArrayM insertObject:atIndex:] + 1276 (NSArrayM.m:164) 3 UIKitCore 0x195b6249c 0x195818000 + 3450012 4 UIKitCore 0x1958c9070 0x195818000 + 725104 5 UIKitCore 0x195899c3c _UIGestureEnvironmentUpdate + 2488 (UIGestureEnvironment.m:192) 6 UIKitCore 0x19598f914 -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 336 (UIGestureEnvironment.m:843) 7 UIKitCore 0x195b2ff18 -[UIGestureEnvironment _updateForEvent:window:] + 188 (UIGestureEnvironment.m:810) 8 UIKitCore 0x195b2f2fc -[UIWindow sendEvent:] + 2948 (UIWindow.m:3627) 11 UIKitCore 0x1959c39b4 -[UIApplication sendEvent:] + 376 (UIApplication.m:12948) 13 UIKitCore 0x1959c3ee0 __dispatchPreprocessedEventFromEventQueue + 1048 (UIEventDispatcher.m:2641) 14 UIKitCore 0x1959cdcc4 __processEventQueue + 5696 (UIEventDispatcher.m:2999) 15 UIKitCore 0x1958c6270 updateCycleEntry + 160 (UIEventDispatcher.m:133) 16 UIKitCore 0x1958c3fe8 _UIUpdateSequenceRun + 84 (_UIUpdateSequence.mm:136) 17 UIKitCore 0x1958c3c38 schedulerStepScheduledMainSection + 172 (_UIUpdateScheduler.m:1171) 18 UIKitCore 0x1958c4bac runloopSourceCallback + 92 (_UIUpdateScheduler.m:1334) 19 CoreFoundation 0x1930a4088 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 28 (CFRunLoop.c:1950) 20 CoreFoundation 0x1930a401c __CFRunLoopDoSource0 + 176 (CFRunLoop.c:1994) 21 CoreFoundation 0x1930a1b08 __CFRunLoopDoSources0 + 244 (CFRunLoop.c:2031) 22 CoreFoundation 0x1930a0d04 __CFRunLoopRun + 840 (CFRunLoop.c:2949) 23 CoreFoundation 0x1930a05b8 CFRunLoopRunSpecific + 572 (CFRunLoop.c:3414) 24 GraphicsServices 0x1deb361c4 GSEventRunModal + 164 (GSEvent.c:2196) 25 UIKitCore 0x195bf65f0 -[UIApplication _run] + 816 (UIApplication.m:3820) 26 UIKitCore 0x195ca510c UIApplicationMain + 340 (UIApplication.m:5472) 27 XXXX 0x104744880 main + 200 (main.m:41) 28 dyld 0x1b8873d34 start + 2724 (dyldMain.cpp:1334)
1
0
249
Sep ’24
UIKit in SwiftUI memory leak while displaying images
I'm using UIKit to display a long list of large images inside a SwiftUI ScrollView and LazyHStack using UIViewControllerRepresentable. When an image is loaded, I'm using SDWebImage to load the image from the disk. As the user navigates through the list and continues to load more images, more memory is used and is never cleared, even as the images are unloaded by the LazyHStack. Eventually, the app reaches the memory limit and crashes. This issue persists if I load the image with UIImage(contentsOfFile: ...) instead of SDWebImage. How can I free the memory used by UIImage when the view is removed? ScrollView(.horizontal, showsIndicators: false) { LazyHStack(spacing: 16) { ForEach(allItems) { item in TestImageDisplayRepresentable(item: item) .frame(width: geometry.size.width, height: geometry.size.height) .id(item.id) } } .scrollTargetLayout() } import UIKit import SwiftUI import SDWebImage class TestImageDisplay: UIViewController { var item: TestItem init(item: TestItem) { self.item = item super.init(nibName: nil, bundle: nil) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 200, height: 200)) imageView.center = view.center view.addSubview(imageView) imageView.sd_setImage(with: item.imageURL, placeholder: nil) } } struct TestImageDisplayRepresentable: UIViewControllerRepresentable { var item: TestItem func makeUIViewController(context: Context) -> TestImageDisplay { return TestImageDisplay(item: item) } func updateUIViewController(_ uiViewController: TestImageDisplay, context: Context) { uiViewController.item = item } }
0
0
290
Sep ’24
UIDocumentBrowserViewController create new document used to work, but...
is now broken. (but definitely worked when I originally wrote my Document-based app) It's been a few years. DocumentBrowserViewController's delegate implements the following func. func documentBrowser(_ controller: UIDocumentBrowserViewController, didRequestDocumentCreationWithHandler importHandler: @escaping (URL?, UIDocumentBrowserViewController.ImportMode) -> Void) { let newDocumentURL: URL? = Bundle.main.url(forResource: "blankFile", withExtension: "trtl2") // Make sure the importHandler is always called, even if the user cancels the creation request. if newDocumentURL != nil { importHandler(newDocumentURL, .copy) } else { importHandler(nil, .none) } } When I tap the + in the DocumentBrowserView, the above delegate func is called (my breakpoint gets hit and I can step through the code) newDocumentURL is getting defined successfully and importHandler(newDocumentURL, .copy) gets called, but returns the following error: Optional(Error Domain=com.apple.DocumentManager Code=2 "No location available to save “blankFile.trtl2”." UserInfo={NSLocalizedDescription=No location available to save “blankFile.trtl2”., NSLocalizedRecoverySuggestion=Enable at least one location to be able to save documents.}) This feels like something new I need to set up in the plist, but so far haven't been able to discover what it is. perhaps I need to update something in info.plist? perhaps one of: CFBundleDocumentTypes UTExportedTypeDeclarations Any guidance appreciated. thanks :-)
1
0
221
Sep ’24
iOS 18 rotate animation not working by layer for custom icon
I have created a custom logo in the SFSymbols 6.1 app and exported it as SVG. clock.arrow.trianglehead.clockwise.rotate.90.svg The icon consists of two layers and I want them to animate by layer. This works for the wiggle effect (the two layers are moving independently), but not for the rotate effect (the logo moves as a whole). Why? This is my code: let imageView = UIImageView() imageView.preferredSymbolConfiguration = UIImage.SymbolConfiguration(scale: .large) imageView.image = UIImage(named: "clock.arrow.trianglehead.clockwise.rotate.90", in: nil, with: nil) imageView.translatesAutoresizingMaskIntoConstraints = false let imageView2 = UIImageView() imageView2.preferredSymbolConfiguration = UIImage.SymbolConfiguration(scale: .large) imageView2.image = UIImage(named: "clock.arrow.trianglehead.clockwise.rotate.90", in: nil, with: nil) imageView2.translatesAutoresizingMaskIntoConstraints = false imageView.addSymbolEffect(.rotate.byLayer, options: .repeat(.continuous)); imageView2.addSymbolEffect(.wiggle.byLayer, options: .repeat(.continuous)); view.addSubview(imageView) view.addSubview(imageView2) NSLayoutConstraint.activate([ imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor), imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor), imageView2.centerXAnchor.constraint(equalTo: view.centerXAnchor), imageView2.topAnchor.constraint(equalTo: imageView.bottomAnchor, constant: 40), ])
2
0
341
Sep ’24
iOS 18 navigationbar UI bug
In iOS 18, when navigating from Page A (with a red navigation bar background color) to Page B, and Page B hides the navigation bar, upon returning to Page A, the background color of the navigation bar appears white (seemingly transparent). The navigation bar background for the share button should be red.
0
1
308
Sep ’24
I have a question regarding UI hang issues when using CompositionalLayout. As the number of cells displayed on the screen increases, the UI becomes less responsive. Could you provide advice or solutions for optimizing performance in such cases?
We are currently combining AutoLayout and StackView to structure our cells, and we’re using UICollectionViewCompositionalLayout to set up sections. Additionally, we’re utilizing UICollectionViewDiffableDataSource to manage the data source. On the home screen, we display cells in a multi-section layout. In the main screen, we support infinite scrolling and use a paging method that fetches 10 items at a time from the API. As we fetch and render more data, the number of displayed cells increases. However, we’ve noticed that as the number of displayed cells grows, the UI freezes during the process of fetching and rendering more data. We suspect that the issue lies in the configuration of UICollectionViewCompositionalLayout. When data is fetched through paging, the data is applied to the CollectionViewDiffableDataSource, and during the process of displaying it on the screen, the method that configures the UICollectionViewCompositionalLayout layout is called. The problem here is that when 10 cells are displayed on the screen and we fetch more data through paging and add 10 more cells, the layout calculation is redone for all 20 cells. In other words, the UICollectionViewCompositionalLayout layout configuration occurs a total of 20 times. Because of this, it seems that the UI freezing issue occurs as the number of cells increases. What steps can we take to resolve this problem? We have attached the relevant code for your reference. private lazy var collectionView = UICollectionView( frame: .zero, collectionViewLayout: self.createCollectionViewLayout() ) private func createCollectionViewLayout() -> UICollectionViewLayout { let layout = UICollectionViewCompositionalLayout { [weak self] sectionIndex, _ in guard let self, self.dataSource.snapshot().sectionIdentifiers.isEmpty == false else { return LayoutProvider.emptySection() } private func applyRefreshSnapshot(with sections: [PlateViewSection]) { var snapshot = self.dataSource.snapshot() snapshot.deleteAllItems() snapshot.appendSections(sections) sections.forEach { section in snapshot.appendItems(section.items, toSection: section) } self.dataSource.apply(snapshot, animatingDifferences: false) } let sectionIdentifier = self.dataSource.snapshot().sectionIdentifiers[safe: sectionIndex] let analyticsScreen = self.payload.analyticsScreen switch sectionIdentifier?.module { case let .tabOutlined(_, reactor): if self.tabOutlinedView == nil { let tabOutlinedView = self.dependency.tabOutlinedViewFactory.create( payload: .init( reactor: reactor, collectionView: self.collectionView, analyticsScreen: analyticsScreen, currentDisplayDepthObserver: .init { event in guard let depth = event.element else { return } self.currentLayoutHeaderTabDepth = depth }, selectedTabItemObserver: .init { [weak self] event in guard let (tab, isPrimaryTabClick) = event.element else { return } var queryParams: [String: String]? if isPrimaryTabClick == false { guard let currentState = self?.reactor?.currentState else { return } let currentSelectedQueryParams = self?.dependency.userDefaults .dictionary(forKey: Keys.PlateParamsKeys.brandQueryParams) as? [String: String] queryParams = currentSelectedQueryParams ?? currentState.defaultQueryParams } self?.scrollCollectionViewToTop() self?.collectionView.viewWithTag( QueryToggleModuleCell.Metric.optionsMenuViewTag )?.removeFromSuperview() self?.reactor?.action.onNext(.refreshWithParams( tabParams: tab?.params, queryParams: queryParams )) } ) ) self.view.addSubview(tabOutlinedView) tabOutlinedView.snp.makeConstraints { make in make.top.leading.trailing.equalToSuperview() } self.view.layoutIfNeeded() self.tabOutlinedView = tabOutlinedView self.tabOutlinedView?.emitInitializeAction() } return LayoutProvider.emptySection() case let .carouselOneRowBrand(module, _): let section = self.dependency.layoutProvider.createLayoutSection(module: module) section.visibleItemsInvalidationHandler = { [weak self] _, _, _ in guard let self else { return } self.emitFetchLikedAction(module: module, sectionIndex: sectionIndex) } return section case let .queryToggle(module): return self.dependency.layoutProvider.createLayoutSection(module: module) case .loadingIndicator: return LayoutProvider.loadingIndicatorSection() case let .space(module): return self.dependency.layoutProvider.createLayoutSection(module: module) case let .noResult(module): return self.dependency.layoutProvider.createLayoutSection(module: module) case let .buttonViewAll(module): return self.dependency.layoutProvider.createLayoutSection(module: module) case .footer: return LayoutProvider.footerSection() default: return LayoutProvider.emptySection() } } return layout } private func applyAppendSnapshot(with sections: [PlateViewSection]) { var snapshot = self.dataSource.snapshot() // loadingIndicator section delete let sectionsToDelete = snapshot.sectionIdentifiers.filter { section in section.module == .loadingIndicator } snapshot.deleteSections(sectionsToDelete) snapshot.appendSections(sections) sections.forEach { section in snapshot.appendItems(section.items, toSection: section) } self.dataSource.apply(snapshot, animatingDifferences: false) }
3
0
189
Sep ’24
Issues with Handling URL Schemes and App State Transitions in iOS App
I'm working on an iOS app that integrates with Spotify for authentication. I’m facing a problem where the app doesn’t handle URL schemes and app state transitions properly. Specifically, when Spotify redirects back to my app, it either doesn’t handle the URL correctly or doesn’t transition between states as expected. The authorization for spotify is successful, then my app reopens and nothing happens after that. Expected Behavior: When Spotify redirects to my app, it should correctly handle the URL and process authentication parameters. Actual Behavior: Even with URL handling code commented out, the app reopens from Spotify, which suggests a possible issue with how URL schemes or app state transitions are managed. The app’s state transitions don’t seem to be handled as expected. Troubleshooting Steps Taken: Verified URL schemes and redirect URIs. Implemented application(_:open:options:) for URL handling. Tested various app states (foreground, background, suspended). //This code should open my app using the URL from spotify but this code never triggers and yet my app opens anyways// func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool { print("Received URL: (url.absoluteString)") handleSpotifyURL(url) return true } private func handleSpotifyURL(_ url: URL) { // URL handling logic } func applicationDidBecomeActive(_ application: UIApplication) { // Logic for when the app becomes active } //Using latest software for xcode, testing on iphone 14 with up to date software and up to date spotify As mentioned - have ensure that my info plist is configured correctly, that spotify authorizes correctly, that the redirect URI is correct in my code and on spotify for developers. From the testing I've done I imagine there is something wrong with how my app gets opened vs how it should get opened with the callback URL triggering func application(_ app: UIApplication, open url: URL, options:.... What I am expecting is that when spotify triggers the callback url - my app reopens with the above function and from there can retrieve the access token etc.
0
0
280
Sep ’24