Create an SFSafariViewController safely — don’t crash on an unhandled Objective-C exception.

This commit is contained in:
Brent Simmons committed 2025-12-04 09:32:46 -08:00
1 parent a63609b7a0
commit 599636b310
5 files changed
+54 -3

No files matched your search

+3 -2
View File
@@ -810,8 +810,9 @@ private extension WebViewController {
}
func openURLInSafariViewController(_ url: URL) {
let viewController = SFSafariViewController(url: url)
guard let viewController = SFSafariViewController.safeSafariViewController(url) else {
return
}
present(viewController, animated: true)
}
}
@@ -1,3 +1,5 @@
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "SFSafariViewController+Extras.h"
@@ -0,0 +1,19 @@
//
// SFSafariViewControllerExtras.h
// NetNewsWire-iOS
//
// Created by Brent Simmons on 11/29/25.
// Copyright © 2025 Ranchero Software. All rights reserved.
//
@import SafariServices;
NS_ASSUME_NONNULL_BEGIN
@interface SFSafariViewController (Extras)
+ (nullable SFSafariViewController *)safeSafariViewController:(NSURL *)url;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,29 @@
//
// SFSafariViewController+Extras.m
// NetNewsWire-iOS
//
// Created by Brent Simmons on 11/29/25.
// Copyright © 2025 Ranchero Software. All rights reserved.
//
#import "SFSafariViewController+Extras.h"
@implementation SFSafariViewController (Extras)
/// Safely create an SFSafariViewController.
///
/// Returns nil if it can’t be created due to an exception.
///
/// Reason: <https://github.com/Ranchero-Software/NetNewsWire/issues/4857>
+ (nullable SFSafariViewController *)safeSafariViewController:(NSURL *)url {
@try {
return [[SFSafariViewController alloc] initWithURL:url];
}
@catch (NSException *exception) {
NSLog(@"Failed to create SFSafariViewController: %@", exception);
return nil;
}
}
@end
+1 -1
View File
@@ -11,7 +11,7 @@ CODE_SIGN_ENTITLEMENTS = iOS/Resources/NetNewsWire$(DEVELOPER_ENTITLEMENTS).enti
PRODUCT_BUNDLE_IDENTIFIER = $(ORGANIZATION_IDENTIFIER).NetNewsWire.iOS
PRODUCT_NAME = NetNewsWire
CLANG_ENABLE_MODULES = YES
SWIFT_OBJC_BRIDGING_HEADER = iOS/Resources/NetNewsWire-iOS-Bridging-Header.h
SWIFT_OBJC_BRIDGING_HEADER = iOS/NetNewsWire-iOS-Bridging-Header.h
SUPPORTED_PLATFORMS = iphoneos iphonesimulator
SUPPORTS_MACCATALYST = NO
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO