'Unable to open url to the Mac App Store in an iOS app launched on Mac with Apple Silicon

I have this code to open a link to the App Store for the user to write a review:

guard var urlComponents = URLComponents(string: "https://apps.apple.com/app/anysub/id1615546730") else { return }
urlComponents.queryItems = [URLQueryItem(name: "action", value: "write-review")]

if let writeReviewUrl = urlComponents.url {
    if UIApplication.shared.canOpenURL(writeReviewUrl) {
        UIApplication.shared.open(writeReviewUrl)
    }
}

For some reason my app crashes when I open this link. It opens in Safari, but the app crashes. The most interesting thing is that if I replace the url with any other non App Store link, everything works fine.

Logs:

2022-05-06 20:06:55.830811+0300 App[8586:108045] *** Assertion failure in -[UINSUIKitBackgroundingController _updateAppWideBackgroundStateSlammingNotifyingDelegate:], UINSUIKitBackgroundingController.m:382
2022-05-06 20:06:55.849301+0300 App[8586:108045] [General] An uncaught exception was raised
2022-05-06 20:06:55.850003+0300 App[8586:108045] [General] -[UINSUIKitBackgroundingController _updateAppWideBackgroundStateSlammingNotifyingDelegate:]: Called off the main thread.
2022-05-06 20:06:55.850091+0300 App[8586:108045] [General] (
    0   CoreFoundation                      0x000000019c972b08 __exceptionPreprocess + 240
    1   libobjc.A.dylib                     0x000000019c6bde14 objc_exception_throw + 60
    2   Foundation                          0x000000019d8a1e18 -[NSCalendarDate initWithCoder:] + 0
    3   UIKitMacHelper                      0x00000001b21da2c0 -[UINSUIKitBackgroundingController _updateAppWideBackgroundStateSlammingNotifyingDelegate:] + 504
    4   UIKitMacHelper                      0x00000001b21d8690 -[UINSUIKitBackgroundingController transitionUIAppThroughBackgroundStateIfNecessary] + 144
    5   UIKitMacHelper                      0x00000001b21abfc8 -[UINSApplicationLifecycleController transitionAppThroughBackgroundState] + 308
    6   UIKitMacHelper                      0x00000001b2191d60 -[UINSApplicationDelegate _toggleAppForegroundStateIfAppStoreURL:] + 276
    7   UIKitMacHelper                      0x00000001b2192198 __54-[UINSApplicationDelegate openURL:options:completion:]_block_invoke_2 + 196
    8   AppKit                              0x000000019fd12ffc _NSWorkspaceHandleLSOpenResult + 164
    9   LaunchServices                      0x000000019cefe094 _Z24_LSFireCompletionHandlerU13block_pointerFvPK7__LSASNhP9__CFErrorEP5NSURLS7_P12NSDictionaryIP8NSStringP11objc_objectESC_bP7NSError + 200
    10  LaunchServices                      0x000000019cf00d04 _ZL28_LSOpenAsyncCommonDispatchedP11objc_objectP7NSArrayIP5NSURLES3_S0_P12NSDictionaryIP8NSStringS0_EPU32objcproto21OS_dispatch_semaphore8NSObjectU13block_pointerFvPK7__LSASNhP9__CFErrorE + 5732
    11  libdispatch.dylib                   0x0000000109e447f0 _dispatch_call_block_and_release + 32
    12  libdispatch.dylib                   0x0000000109e46394 _dispatch_client_callout + 20
    13  libdispatch.dylib                   0x0000000109e4f778 _dispatch_lane_serial_drain + 980
    14  libdispatch.dylib                   0x0000000109e50814 _dispatch_lane_invoke + 492
    15  libdispatch.dylib                   0x0000000109e600c8 _dispatch_workloop_worker_thread + 1232
    16  libsystem_pthread.dylib             0x0000000109a47f84 _pthread_wqthread + 288
    17  libsystem_pthread.dylib             0x0000000109a4fa9c start_wqthread + 8
)
(lldb) 

I also tried to change the App Store link to macappstore://itunes.apple.com/app/id1615546730 and it doesn't help.

P.S. It's calling from the main thread.



Solution 1:[1]

To enable a user to initiate a review as a result of an action in the UI use a deep link to the App Store page for your app with the query parameter action=write-review appended to the URL.

https://developer.apple.com/documentation/storekit/requesting_app_store_reviews

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abdul Momen