Skip to content

Instantly share code, notes, and snippets.

View steipete's full-sized avatar

Peter Steinberger steipete

View GitHub Profile
@steipete
steipete / PSPDFCatalystPatches.m
Last active October 19, 2019 12:00
Fixes Mac Catalyst: random crash inside [_UIBlurEffectCoreUIImpl _needsUpdateForTransitionFromEnvironment:toEnvironment:usage:] ()
Use this code: (code incomplete, but you get the idea what needs to be done)
// TODO: Stop calling this once 10.15.1 is out!
#if TARGET_OS_UIKITFORMAC
static void PSPDFFixVisualEffectsEnvironment(void) {
static const char *PSPDFVisualEffectsWindowKey;
static const char *PSPDFVisualEffectsSuperviewKey;
let klass = NSClassFromString([NSString stringWithFormat:@"_%@sualEffe%@ironment", @"UIVi", @"ctEnv"]);
@steipete
steipete / HowToUseURLByResolvingBookmarkDataOnMacCatalyst.md
Last active October 3, 2022 16:32
Using URLByResolvingBookmarkData on Mac Catalyst: Access sandboxed URLs after an app restart.

Here's what needs to be done in order to use security scoped bookmarks on Mac Catalyst:

  1. You need an entitlement: "com.apple.security.files.bookmarks.app-scope" needs to be se to 1.

  2. Pass both NSURLBookmarkCreationWithSecurityScope and NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess when creating the bookmark.

Note: The headers mark these API as unavailable for iOS, and this indeed does only work on Mac and not iOS. However, Mac Catalyst really is a Mac app, so using these values is fine. In order to allow compilation, use following:

@steipete
steipete / UIView+AnimationContext.swift
Created August 10, 2019 13:36 — forked from CodingMeSwiftly/UIView+AnimationContext.swift
An extension on UIView to retrieve information about the animation context the view is currently in.
import UIKit
extension UIView {
struct AnimationContext {
public let duration: TimeInterval
public let timingParameters: UITimingCurveProvider?
}
}
extension UIView {
@steipete
steipete / UIView+AnimationContext.swift
Created August 10, 2019 13:36 — forked from CodingMeSwiftly/UIView+AnimationContext.swift
An extension on UIView to retrieve information about the animation context the view is currently in.
import UIKit
extension UIView {
struct AnimationContext {
public let duration: TimeInterval
public let timingParameters: UITimingCurveProvider?
}
}
extension UIView {
@steipete
steipete / Runtime.m
Created August 7, 2019 08:59
pspdf_swizzleSelectorWithBlock, pspdf_swizzleSelector. Please use your own prefix when you copy these.
// http://defagos.github.io/yet_another_article_about_method_swizzling/ (Thank you!!)
// Returns the original implementation
static _Nullable IMP pspdf_swizzleSelector(Class clazz, SEL selector, IMP newImplementation) {
NSCParameterAssert(clazz);
NSCParameterAssert(selector);
NSCParameterAssert(newImplementation);
// If the method does not exist for this class, do nothing.
const Method method = class_getInstanceMethod(clazz, selector);
if (!method) {
@steipete
steipete / SomeObjCFile.m
Created August 7, 2019 08:57
HACK around FB6940492: Mac Catalyst: Crash when presenting view controllers. Call early.
/**
HACK around FB6940492: Mac Catalyst: Crash when presenting view controllers
'NSRangeException', reason: 'Cannot remove an observer <UINSSceneViewController 0x600003558790> for the key path "view.window.screen.contentLayoutRect" from <UINSSceneViewController 0x600003558790> because it is not registered as an observer.'
0 CoreFoundation 0x00007fff3b20f183 __exceptionPreprocess + 250
1 libobjc.A.dylib 0x00007fff71719b64 objc_exception_throw + 48
2 Foundation 0x00007fff3d8ca2aa -[NSObject(NSKeyValueObserverRegistration) _removeObserver:forProperty:] + 578
3 Foundation 0x00007fff3d8ca01b -[NSObject(NSKeyValueObserverRegistration) removeObserver:forKeyPath:] + 74
4 Foundation 0x00007fff3d8e2898 -[NSObject(NSKeyValueObserverRegistration) removeObserver:forKeyPath:context:] + 190
@steipete
steipete / UIWindow+PSPDFAdditions.h
Last active June 5, 2024 20:09
Mac Catalyst: Get the NSWindow from a UIWindow (Updated for macOS 11 Big Sur, also works with Catalina)
// Don't forget to prefix your category!
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIWindow (PSPDFAdditions)
#if TARGET_OS_UIKITFORMAC
@steipete
steipete / AppDelegate.swift
Last active September 2, 2022 15:09
Create menu in mac Catalyst via UIMenuBuilder (beta 4) - (void)buildMenuWithBuilder:(id<UIMenuBuilder>)builder;
// AppDelegate.swift:
// macCatalyst: Create menu
override func buildMenu(with builder: UIMenuBuilder) {
guard builder.system == .main else { return }
// The format menu doesn't make sense
builder.remove(menu: .format)
// Add Open command
@steipete
steipete / PSPDFEnvironment.m
Last active March 26, 2019 09:39
PSPDFApplicationIsTerminating - detect application termination on iOS and macOS. License: MIT. Taken out of the commercial PSPDFKit PDF SDK. http://pspdfkit.com
static _Atomic(BOOL) _applicationWillTerminate = NO;
__attribute__((constructor)) static void PSPDFInstallAppWillTerminateHandler(void) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[NSNotificationCenter.defaultCenter addObserverForName:UIApplicationWillTerminateNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
_applicationWillTerminate = YES;
PSPDFLogWarning(@"Application shutdown event detected.");
}];
});
}
extension UndoManager {
@objc func workaround_pspdf_endUndoGrouping() {
guard self.groupingLevel > 0 else { return }
self.workaround_pspdf_endUndoGrouping()
}
// Ensure this is only called once!
fileprivate static func InstallWorkaroundForRdar46395110() {
let originalSelector = #selector(UndoManager.endUndoGrouping)
let swizzledSelector = #selector(UndoManager.workaround_pspdf_endUndoGrouping)