Skip to content

aeonfun/soul.md

Embody this digital identity. Read SOUL.md first, then STYLE.md, then examples/. Become the person—opinions, voice, worldview.

FileKindSize
SKILL.mdskill md4.5 KB
examples/_GUIDE.mdmarkdown1.5 KB
examples/ansem/MEMORY.mdmarkdown645 B
examples/ansem/README.mdmarkdown4.2 KB
examples/ansem/SOUL.mdmarkdown12.5 KB
examples/ansem/STYLE.mdmarkdown10.7 KB
examples/ansem/data/blknoiz06_all.jsonlother38.3 MB
examples/ansem/examples/bad-outputs.mdmarkdown6.1 KB
examples/ansem/examples/good-outputs.mdmarkdown8.7 KB
examples/bad-outputs.template.mdmarkdown2.2 KB
examples/elon-musk/MEMORY.mdmarkdown153 B
examples/elon-musk/QUICKSTART.mdmarkdown4.0 KB
examples/elon-musk/README.mdmarkdown3.5 KB
examples/elon-musk/SOUL.mdmarkdown10.5 KB
examples/elon-musk/STYLE.mdmarkdown5.8 KB
examples/elon-musk/data/sources.mdmarkdown1.2 KB
examples/elon-musk/examples/bad-outputs.mdmarkdown2.6 KB
examples/elon-musk/examples/good-outputs.mdmarkdown3.2 KB
examples/elon-musk/tests/prediction-test.mdmarkdown5.5 KB
examples/garry-tan/MEMORY.mdmarkdown428 B
examples/garry-tan/README.mdmarkdown2.1 KB
examples/garry-tan/SOUL.mdmarkdown13.3 KB
examples/garry-tan/STYLE.mdmarkdown9.2 KB
examples/garry-tan/data/influences.mdmarkdown2.6 KB
examples/garry-tan/examples/bad-outputs.mdmarkdown4.8 KB
examples/garry-tan/examples/good-outputs.mdmarkdown4.5 KB
examples/garry-tan/scripts/fetch_writing.pyscript4.0 KB
examples/garry-tan/scripts/fetch_x.pyscript3.7 KB
examples/garry-tan/scripts/fetch_yt.pyscript2.7 KB
examples/garry-tan/tests/platform_tests.mdmarkdown1006 B
examples/garry-tan/tests/prediction_test.mdmarkdown4.7 KB
examples/garry-tan/tests/results_gpt-4o-mini.mdmarkdown5.9 KB
examples/garry-tan/tests/run_weak_model.pyscript5.5 KB
examples/garry-tan/tests/scores_gpt-4o-mini.mdmarkdown3.6 KB
examples/garry-tan/tests/weak_model_test.mdmarkdown5.3 KB
examples/good-outputs.template.mdmarkdown2.4 KB
examples/hunter-biden/MEMORY.mdmarkdown1.0 KB
examples/hunter-biden/QUICKSTART.mdmarkdown3.3 KB
examples/hunter-biden/README.mdmarkdown6.1 KB
examples/hunter-biden/SOUL.mdmarkdown12.7 KB
examples/hunter-biden/STYLE.mdmarkdown9.1 KB
examples/hunter-biden/data/influences.mdmarkdown4.1 KB
examples/hunter-biden/data/x/tweets.jsonconfig96.0 KB
examples/hunter-biden/data/x/tweets.mdmarkdown65.7 KB
examples/hunter-biden/examples/bad-outputs.mdmarkdown7.4 KB
examples/hunter-biden/examples/good-outputs.mdmarkdown20.7 KB
examples/hunter-biden/tests/prediction-test.mdmarkdown4.2 KB
examples/karpathy/MEMORY.mdmarkdown515 B
examples/karpathy/QUICKSTART.mdmarkdown2.2 KB
examples/karpathy/README.mdmarkdown7.4 KB
examples/karpathy/SOUL.mdmarkdown15.3 KB
examples/karpathy/STYLE.mdmarkdown11.6 KB
examples/karpathy/data/github/karpathy_LLM101n_README.mdmarkdown2.3 KB
examples/karpathy/data/github/karpathy_arxiv-sanity-preserver_README.mdmarkdown6.8 KB
examples/karpathy/data/github/karpathy_char-rnn_README.mdmarkdown12.5 KB
examples/karpathy/data/github/karpathy_llm.c_README.mdmarkdown16.1 KB
examples/karpathy/data/github/karpathy_micrograd_README.mdmarkdown2.4 KB
examples/karpathy/data/github/karpathy_minbpe_README.mdmarkdown9.1 KB
examples/karpathy/data/github/karpathy_nanoGPT_README.mdmarkdown13.5 KB
examples/karpathy/data/github/karpathy_nn-zero-to-hero_README.mdmarkdown6.7 KB
examples/karpathy/data/influences.mdmarkdown4.9 KB
examples/karpathy/data/transcripts/7xTGNNLPyMI.en.txtmarkdown208.7 KB
examples/karpathy/data/transcripts/P6sfmUTpUmc.en.txtmarkdown108.9 KB
examples/karpathy/data/transcripts/PaCmpygFfXo.en.txtmarkdown96.3 KB
examples/karpathy/data/transcripts/SOURCES.mdmarkdown2.7 KB
examples/karpathy/data/transcripts/TCH_1BHY58I.en.txtmarkdown63.3 KB
examples/karpathy/data/transcripts/VMj-3S1tku0.en.txtmarkdown118.3 KB
examples/karpathy/data/transcripts/c3b-JASoPi0.en.txtmarkdown39.5 KB
examples/karpathy/data/transcripts/hM_h0UA7upI.en.txtmarkdown55.4 KB
examples/karpathy/data/transcripts/kCc8FmEb1nY.en.txtmarkdown105.3 KB
examples/karpathy/data/transcripts/l8pRSuU81PU.en.txtmarkdown213.9 KB
examples/karpathy/data/transcripts/q8SA3rM6ckI.en.txtmarkdown102.5 KB
examples/karpathy/data/transcripts/t3YJ5hKiMQ0.en.txtmarkdown52.9 KB
examples/karpathy/data/transcripts/zduSFxRajkE.en.txtmarkdown122.6 KB
examples/karpathy/data/writing/ai-short-peek.htmlother55.7 KB
examples/karpathy/data/writing/biohacking-lite.htmlother41.2 KB
examples/karpathy/data/writing/blockchain.htmlother297.3 KB
examples/karpathy/data/writing/forward-pass.htmlother14.6 KB
examples/karpathy/data/writing/lecun1989.htmlother25.0 KB
examples/karpathy/data/writing/medium.htmlother6.6 KB
examples/karpathy/data/writing/microgpt.htmlother93.8 KB
examples/karpathy/data/writing/phd.htmlother56.7 KB
examples/karpathy/data/writing/recipe.htmlother32.9 KB
examples/karpathy/data/writing/rl.htmlother54.4 KB
examples/karpathy/data/writing/rnn-effectiveness.htmlother79.9 KB
examples/karpathy/data/writing/software-2.0.htmlother235.3 KB
examples/karpathy/data/writing/state-of-cv.htmlother13.3 KB
examples/karpathy/data/x/README.mdmarkdown564 B
examples/karpathy/data/x/curated-tweets.mdmarkdown35.7 KB
examples/karpathy/data/x/posts-original.jsonconfig171.1 KB
examples/karpathy/data/x/profile.jsonconfig470 B
examples/karpathy/data/x/replies.jsonconfig105.5 KB
examples/karpathy/examples/bad-outputs.mdmarkdown6.2 KB
examples/karpathy/examples/good-outputs.mdmarkdown14.8 KB
examples/karpathy/scripts/fetch-data.shscript6.6 KB
examples/karpathy/scripts/weak-model-test.mjsscript15.2 KB
examples/karpathy/tests/prediction-test.mdmarkdown6.1 KB
examples/karpathy/tests/weak-model-results.mdmarkdown12.6 KB
examples/michael-saylor/MEMORY.mdmarkdown351 B
examples/michael-saylor/QUICKSTART.mdmarkdown3.5 KB
examples/michael-saylor/README.mdmarkdown6.5 KB
examples/michael-saylor/SOUL.mdmarkdown13.4 KB
examples/michael-saylor/STYLE.mdmarkdown7.0 KB
examples/michael-saylor/data/influences.mdmarkdown2.5 KB
examples/michael-saylor/examples/bad-outputs.mdmarkdown3.7 KB
examples/michael-saylor/examples/good-outputs.mdmarkdown7.0 KB
examples/michael-saylor/tests/prediction-test.mdmarkdown4.3 KB
examples/steipete/QUICKSTART.mdmarkdown5.1 KB
examples/steipete/README.mdmarkdown6.9 KB
examples/steipete/SOUL.mdmarkdown16.8 KB
examples/steipete/STYLE.mdmarkdown12.1 KB
examples/steipete/data/github/openclaw--openclaw.README.mdmarkdown84.0 KB
examples/steipete/data/github/openclaw--openclaw.VISION.mdmarkdown5.3 KB
examples/steipete/data/github/steipete--Aspects.README.mdmarkdown10.8 KB
examples/steipete/data/github/steipete--CodexBar.README.mdmarkdown8.5 KB
examples/steipete/data/github/steipete--InterposeKit.README.mdmarkdown11.0 KB
examples/steipete/data/github/steipete--Peekaboo.README.mdmarkdown7.4 KB
examples/steipete/data/github/steipete--Tachikoma.README.mdmarkdown3.9 KB
examples/steipete/data/github/steipete--VibeMeter.README.mdmarkdown13.3 KB
examples/steipete/data/github/steipete--agent-rules.README.mdmarkdown146 B
examples/steipete/data/github/steipete--agent-scripts.README.mdmarkdown5.8 KB
examples/steipete/data/github/steipete--birdclaw.README.mdmarkdown15.8 KB
examples/steipete/data/github/steipete--claude-code-mcp.README.mdmarkdown12.7 KB
examples/steipete/data/github/steipete--mcporter.README.mdmarkdown28.4 KB
examples/steipete/data/github/steipete--oracle.README.mdmarkdown28.3 KB
examples/steipete/data/github/steipete--poltergeist.AGENTS.mdmarkdown9.4 KB
examples/steipete/data/github/steipete--poltergeist.README.mdmarkdown13.5 KB
examples/steipete/data/influences.mdmarkdown10.7 KB
examples/steipete/data/transcripts/3-lessons-you-can-use-today.txtmarkdown10.9 KB
examples/steipete/data/transcripts/agentic-workflow-behind-openclaw.txtmarkdown12.6 KB
examples/steipete/data/transcripts/builders-unscripted-ep1.txtmarkdown31.4 KB
examples/steipete/data/transcripts/first-public-appearance-since-launch.txtmarkdown34.4 KB
examples/steipete/data/transcripts/i-ship-code-i-dont-read.txtmarkdown114.7 KB
examples/steipete/data/transcripts/lex-fridman-491.txtmarkdown158.5 KB
examples/steipete/data/transcripts/lex-fridman-clip-ai-agents.txtmarkdown25.9 KB
examples/steipete/data/transcripts/maximize-claude-code-subscription.txtmarkdown25.7 KB
examples/steipete/data/writing/2012-dont-call-willchangevalueforkey.htmlother59.8 KB
examples/steipete/data/writing/2012-hacking-block-support-into-uimenuitem.htmlother183.8 KB
examples/steipete/data/writing/2012-moving-on.htmlother62.2 KB
examples/steipete/data/writing/2012-nsurlcache-uses-a-disk-cache-as-of-ios5.htmlother54.5 KB
examples/steipete/data/writing/2012-pimping-recursivedescription.htmlother55.6 KB
examples/steipete/data/writing/2012-reboot.htmlother50.0 KB
examples/steipete/data/writing/2012-using-subscripting-with-xcode-4_4-and-ios-4_3.htmlother59.5 KB
examples/steipete/data/writing/2014-a-story-about-swizzling-the-right-way-and-touch-forwarding.htmlother112.5 KB
examples/steipete/data/writing/2025-applescript-cli-macos-complete-guide.htmlother110.7 KB
examples/steipete/data/writing/2025-automatic-observation-tracking-uikit-appkit.htmlother104.4 KB
examples/steipete/data/writing/2025-claude-code-anonymous.htmlother54.8 KB
examples/steipete/data/writing/2025-claude-code-is-my-computer.htmlother72.0 KB
examples/steipete/data/writing/2025-code-signing-and-notarization-sparkle-and-tears.htmlother101.6 KB
examples/steipete/data/writing/2025-commanding-your-claude-code-army.htmlother75.2 KB
examples/steipete/data/writing/2025-essential-reading-august-2025.htmlother78.7 KB
examples/steipete/data/writing/2025-essential-reading-july-2025.htmlother73.2 KB
examples/steipete/data/writing/2025-essential-reading.htmlother79.4 KB
examples/steipete/data/writing/2025-finding-my-spark-again.htmlother55.8 KB
examples/steipete/data/writing/2025-introducing-demark-html-to-markdown-in-swift.htmlother63.9 KB
examples/steipete/data/writing/2025-live-coding-session-building-arena.htmlother59.8 KB
examples/steipete/data/writing/2025-llm-codes-transform-developer-docs.htmlother59.9 KB
examples/steipete/data/writing/2025-logging-privacy-shenanigans.htmlother108.4 KB
examples/steipete/data/writing/2025-mcp-best-practices.htmlother69.0 KB
examples/steipete/data/writing/2025-migrating-700-tests-to-swift-testing.htmlother178.7 KB
examples/steipete/data/writing/2025-optimal-ai-development-workflow.htmlother67.8 KB
examples/steipete/data/writing/2025-peekaboo-2-freeing-the-cli-from-its-mcp-shackles.htmlother76.2 KB
examples/steipete/data/writing/2025-peekaboo-mcp-lightning-fast-macos-screenshots-for-ai-agents.htmlother67.1 KB
examples/steipete/data/writing/2025-poltergeist-ghost-keeps-builds-fresh.htmlother81.8 KB
examples/steipete/data/writing/2025-self-hosting-ai-models.htmlother114.9 KB
examples/steipete/data/writing/2025-shipping-at-inference-speed.htmlother70.1 KB
examples/steipete/data/writing/2025-showing-settings-from-macos-menu-bar-items.htmlother110.3 KB
examples/steipete/data/writing/2025-signature-flicker.htmlother69.5 KB
examples/steipete/data/writing/2025-startup-slop.htmlother69.7 KB
examples/steipete/data/writing/2025-stats-store-privacy-first-sparkle-analytics.htmlother58.6 KB
examples/steipete/data/writing/2025-stop-overthinking-ai-subscriptions.htmlother67.4 KB
examples/steipete/data/writing/2025-the-future-of-vibe-coding.htmlother88.9 KB
examples/steipete/data/writing/2025-understanding-codebases-with-ai-gemini-workflow.htmlother62.5 KB
examples/steipete/data/writing/2025-vibe-meter-2-claude-code-usage-calculation.htmlother71.3 KB
examples/steipete/data/writing/2025-vibe-meter-monitor-your-ai-costs.htmlother69.9 KB
examples/steipete/data/writing/2025-vibetunnel-first-anniversary.htmlother59.0 KB
examples/steipete/data/writing/2025-vibetunnel-turn-any-browser-into-your-mac-terminal.htmlother77.7 KB
examples/steipete/data/writing/2025-when-ai-meets-madness-peters-16-hour-days.htmlother70.4 KB
examples/steipete/data/writing/2026-openclaw.htmlother52.1 KB
examples/steipete/data/writing/command-your-claude-code-army-reloaded.htmlother59.4 KB
examples/steipete/data/writing/index.htmlother34.0 KB
examples/steipete/data/writing/just-one-more-prompt.htmlother59.7 KB
examples/steipete/data/writing/just-talk-to-it.htmlother80.8 KB
examples/steipete/data/x/curated-tweets.mdmarkdown42.1 KB
examples/steipete/data/x/openclaw.posts-original.jsonconfig68.4 KB
examples/steipete/data/x/openclaw.profile.jsonconfig478 B
examples/steipete/data/x/openclaw.replies.jsonconfig74.6 KB
examples/steipete/data/x/steipete.posts-original.jsonconfig95.5 KB
examples/steipete/data/x/steipete.profile.jsonconfig485 B
examples/steipete/data/x/steipete.replies.jsonconfig2 B
examples/steipete/examples/bad-outputs.mdmarkdown10.2 KB
examples/steipete/examples/good-outputs.mdmarkdown15.9 KB
examples/steipete/scripts/fetch-data.shexecutablescript11.4 KB
examples/steipete/scripts/pull-x.mjsscript3.9 KB
examples/steipete/scripts/weak-model-test.mjsscript17.7 KB
examples/steipete/tests/prediction-test.mdmarkdown13.2 KB
examples/steipete/tests/weak-model-results.mdmarkdown11.6 KB
examples/vitalik-buterin/MEMORY.mdmarkdown972 B
examples/vitalik-buterin/QUICKSTART.mdmarkdown3.3 KB
examples/vitalik-buterin/README.mdmarkdown6.9 KB

examples/steipete/data/writing/2012-hacking-block-support-into-uimenuitem.html

View on GitHub
<!DOCTYPE html><html lang="en" class="scroll-smooth" data-astro-cid-sckkx6r4> <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width"><meta name="view-transition" content="same-origin"><link rel="icon" type="image/x-icon" href="/favicon.ico"><link rel="canonical" href="https://steipete.me/posts/2012/hacking-block-support-into-uimenuitem"><meta name="generator" content="Astro v5.18.1"><!-- General Meta Tags --><title>Hacking Block Support Into UIMenuItem | Peter Steinberger</title><meta name="title" content="Hacking Block Support Into UIMenuItem | Peter Steinberger"><meta name="description" content="Implement block support for UIMenuItem by swizzling the responder chain to enable cleaner API patterns."><meta name="author" content="Peter Steinberger"><link rel="sitemap" href="/sitemap-index.xml"><!-- Open Graph / Facebook --><meta property="og:title" content="Hacking Block Support Into UIMenuItem | Peter Steinberger"><meta property="og:description" content="Implement block support for UIMenuItem by swizzling the responder chain to enable cleaner API patterns."><meta property="og:url" content="https://steipete.me/posts/2012/hacking-block-support-into-uimenuitem"><meta property="og:image" content="https://steipete.me/posts/2012/hacking-block-support-into-uimenuitem/index.png"><!-- Article Published/Modified time --><meta property="article:published_time" content="2012-07-17T16:22:00.000Z"><!-- Twitter --><meta property="twitter:card" content="summary_large_image"><meta property="twitter:url" content="https://steipete.me/posts/2012/hacking-block-support-into-uimenuitem"><meta property="twitter:title" content="Hacking Block Support Into UIMenuItem | Peter Steinberger"><meta property="twitter:description" content="Implement block support for UIMenuItem by swizzling the responder chain to enable cleaner API patterns."><meta property="twitter:image" content="https://steipete.me/posts/2012/hacking-block-support-into-uimenuitem/index.png"><!-- Google JSON-LD Structured data --><script type="application/ld+json">{"@context":"https://schema.org","@type":"BlogPosting","headline":"Hacking Block Support Into UIMenuItem | Peter Steinberger","image":"https://steipete.me/posts/2012/hacking-block-support-into-uimenuitem/index.png","datePublished":"2012-07-17T16:22:00.000Z","author":[{"@type":"Person","name":"Peter Steinberger","url":"https://steipete.me/about"}]}</script><!-- Enable RSS feed auto-discovery  --><!-- https://docs.astro.build/en/recipes/rss/#enabling-rss-feed-auto-discovery --><link rel="alternate" type="application/rss+xml" title="Peter Steinberger" href="https://steipete.me/rss.xml"><meta name="theme-color" content=""><meta name="astro-view-transitions-enabled" content="true"><meta name="astro-view-transitions-fallback" content="animate"><script type="module" src="/_astro/ClientRouter.astro_astro_type_script_index_0_lang.CDGfc0hd.js"></script><script src="/toggle-theme.js"></script><!-- Domain-based markdown redirect --><script>
      // Check if we're on steipete.md domain
      const hostname = window.location.hostname;
      if ((hostname === 'steipete.md' || hostname === 'www.steipete.md') && !window.location.pathname.endsWith('.md')) {
        // Redirect to the .md version of the current page
        const mdPath = window.location.pathname === '/' ? '/index.md' : window.location.pathname + '.md';
        window.location.replace(mdPath + window.location.search + window.location.hash);
      }
    </script><!-- Twitter Widget Script --><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script><style>@media(max-width:640px){.newsletter-box[data-astro-cid-nbvfnxgh]{margin-left:-1rem;margin-right:-1rem;border-radius:0}}
@keyframes astroFadeInOut{0%{opacity:1}to{opacity:0}}@keyframes astroFadeIn{0%{opacity:0;mix-blend-mode:plus-lighter}to{opacity:1;mix-blend-mode:plus-lighter}}@keyframes astroFadeOut{0%{opacity:1;mix-blend-mode:plus-lighter}to{opacity:0;mix-blend-mode:plus-lighter}}@keyframes astroSlideFromRight{0%{transform:translate(100%)}}@keyframes astroSlideFromLeft{0%{transform:translate(-100%)}}@keyframes astroSlideToRight{to{transform:translate(100%)}}@keyframes astroSlideToLeft{to{transform:translate(-100%)}}@media(prefers-reduced-motion){::view-transition-group(*),::view-transition-old(*),::view-transition-new(*){animation:none!important}[data-astro-transition-scope]{animation:none!important}}
</style>
<link rel="stylesheet" href="/_astro/about.jXOsNRnW.css">
<style>.youtube-embed-container[data-astro-cid-vj4tpspi]{margin:2rem 0;position:relative;width:100%;padding-bottom:56.25%;height:0;overflow:hidden}.youtube-embed-container[data-astro-cid-vj4tpspi] iframe[data-astro-cid-vj4tpspi]{position:absolute;top:0;left:0;width:100%;height:100%;border:0}
</style><style>[data-astro-transition-scope="astro-pu2sknsl-1"] { view-transition-name: hacking-block-support-into-ui-menu-item; }@layer astro { ::view-transition-old(hacking-block-support-into-ui-menu-item) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }::view-transition-new(hacking-block-support-into-ui-menu-item) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; }[data-astro-transition=back]::view-transition-old(hacking-block-support-into-ui-menu-item) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }[data-astro-transition=back]::view-transition-new(hacking-block-support-into-ui-menu-item) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; } }[data-astro-transition-fallback="old"] [data-astro-transition-scope="astro-pu2sknsl-1"],
			[data-astro-transition-fallback="old"][data-astro-transition-scope="astro-pu2sknsl-1"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }[data-astro-transition-fallback="new"] [data-astro-transition-scope="astro-pu2sknsl-1"],
			[data-astro-transition-fallback="new"][data-astro-transition-scope="astro-pu2sknsl-1"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; }[data-astro-transition=back][data-astro-transition-fallback="old"] [data-astro-transition-scope="astro-pu2sknsl-1"],
			[data-astro-transition=back][data-astro-transition-fallback="old"][data-astro-transition-scope="astro-pu2sknsl-1"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }[data-astro-transition=back][data-astro-transition-fallback="new"] [data-astro-transition-scope="astro-pu2sknsl-1"],
			[data-astro-transition=back][data-astro-transition-fallback="new"][data-astro-transition-scope="astro-pu2sknsl-1"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; }</style><style>[data-astro-transition-scope="astro-36ssibgs-2"] { view-transition-name: i-os-development; }@layer astro { ::view-transition-old(i-os-development) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }::view-transition-new(i-os-development) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; }[data-astro-transition=back]::view-transition-old(i-os-development) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }[data-astro-transition=back]::view-transition-new(i-os-development) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; } }[data-astro-transition-fallback="old"] [data-astro-transition-scope="astro-36ssibgs-2"],
			[data-astro-transition-fallback="old"][data-astro-transition-scope="astro-36ssibgs-2"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }[data-astro-transition-fallback="new"] [data-astro-transition-scope="astro-36ssibgs-2"],
			[data-astro-transition-fallback="new"][data-astro-transition-scope="astro-36ssibgs-2"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; }[data-astro-transition=back][data-astro-transition-fallback="old"] [data-astro-transition-scope="astro-36ssibgs-2"],
			[data-astro-transition=back][data-astro-transition-fallback="old"][data-astro-transition-scope="astro-36ssibgs-2"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }[data-astro-transition=back][data-astro-transition-fallback="new"] [data-astro-transition-scope="astro-36ssibgs-2"],
			[data-astro-transition=back][data-astro-transition-fallback="new"][data-astro-transition-scope="astro-36ssibgs-2"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; }</style><style>[data-astro-transition-scope="astro-36ssibgs-3"] { view-transition-name: objective-c; }@layer astro { ::view-transition-old(objective-c) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }::view-transition-new(objective-c) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; }[data-astro-transition=back]::view-transition-old(objective-c) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }[data-astro-transition=back]::view-transition-new(objective-c) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; } }[data-astro-transition-fallback="old"] [data-astro-transition-scope="astro-36ssibgs-3"],
			[data-astro-transition-fallback="old"][data-astro-transition-scope="astro-36ssibgs-3"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }[data-astro-transition-fallback="new"] [data-astro-transition-scope="astro-36ssibgs-3"],
			[data-astro-transition-fallback="new"][data-astro-transition-scope="astro-36ssibgs-3"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; }[data-astro-transition=back][data-astro-transition-fallback="old"] [data-astro-transition-scope="astro-36ssibgs-3"],
			[data-astro-transition=back][data-astro-transition-fallback="old"][data-astro-transition-scope="astro-36ssibgs-3"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }[data-astro-transition=back][data-astro-transition-fallback="new"] [data-astro-transition-scope="astro-36ssibgs-3"],
			[data-astro-transition=back][data-astro-transition-fallback="new"][data-astro-transition-scope="astro-36ssibgs-3"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; }</style><style>[data-astro-transition-scope="astro-36ssibgs-4"] { view-transition-name: ui-kit; }@layer astro { ::view-transition-old(ui-kit) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }::view-transition-new(ui-kit) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; }[data-astro-transition=back]::view-transition-old(ui-kit) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }[data-astro-transition=back]::view-transition-new(ui-kit) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; } }[data-astro-transition-fallback="old"] [data-astro-transition-scope="astro-36ssibgs-4"],
			[data-astro-transition-fallback="old"][data-astro-transition-scope="astro-36ssibgs-4"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }[data-astro-transition-fallback="new"] [data-astro-transition-scope="astro-36ssibgs-4"],
			[data-astro-transition-fallback="new"][data-astro-transition-scope="astro-36ssibgs-4"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; }[data-astro-transition=back][data-astro-transition-fallback="old"] [data-astro-transition-scope="astro-36ssibgs-4"],
			[data-astro-transition=back][data-astro-transition-fallback="old"][data-astro-transition-scope="astro-36ssibgs-4"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }[data-astro-transition=back][data-astro-transition-fallback="new"] [data-astro-transition-scope="astro-36ssibgs-4"],
			[data-astro-transition=back][data-astro-transition-fallback="new"][data-astro-transition-scope="astro-36ssibgs-4"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; }</style><style>[data-astro-transition-scope="astro-36ssibgs-5"] { view-transition-name: blocks; }@layer astro { ::view-transition-old(blocks) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }::view-transition-new(blocks) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; }[data-astro-transition=back]::view-transition-old(blocks) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }[data-astro-transition=back]::view-transition-new(blocks) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; } }[data-astro-transition-fallback="old"] [data-astro-transition-scope="astro-36ssibgs-5"],
			[data-astro-transition-fallback="old"][data-astro-transition-scope="astro-36ssibgs-5"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }[data-astro-transition-fallback="new"] [data-astro-transition-scope="astro-36ssibgs-5"],
			[data-astro-transition-fallback="new"][data-astro-transition-scope="astro-36ssibgs-5"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; }[data-astro-transition=back][data-astro-transition-fallback="old"] [data-astro-transition-scope="astro-36ssibgs-5"],
			[data-astro-transition=back][data-astro-transition-fallback="old"][data-astro-transition-scope="astro-36ssibgs-5"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }[data-astro-transition=back][data-astro-transition-fallback="new"] [data-astro-transition-scope="astro-36ssibgs-5"],
			[data-astro-transition=back][data-astro-transition-fallback="new"][data-astro-transition-scope="astro-36ssibgs-5"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; }</style><style>[data-astro-transition-scope="astro-36ssibgs-6"] { view-transition-name: method-swizzling; }@layer astro { ::view-transition-old(method-swizzling) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }::view-transition-new(method-swizzling) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; }[data-astro-transition=back]::view-transition-old(method-swizzling) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }[data-astro-transition=back]::view-transition-new(method-swizzling) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; } }[data-astro-transition-fallback="old"] [data-astro-transition-scope="astro-36ssibgs-6"],
			[data-astro-transition-fallback="old"][data-astro-transition-scope="astro-36ssibgs-6"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }[data-astro-transition-fallback="new"] [data-astro-transition-scope="astro-36ssibgs-6"],
			[data-astro-transition-fallback="new"][data-astro-transition-scope="astro-36ssibgs-6"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; }[data-astro-transition=back][data-astro-transition-fallback="old"] [data-astro-transition-scope="astro-36ssibgs-6"],
			[data-astro-transition=back][data-astro-transition-fallback="old"][data-astro-transition-scope="astro-36ssibgs-6"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }[data-astro-transition=back][data-astro-transition-fallback="new"] [data-astro-transition-scope="astro-36ssibgs-6"],
			[data-astro-transition=back][data-astro-transition-fallback="new"][data-astro-transition-scope="astro-36ssibgs-6"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; }</style><style>[data-astro-transition-scope="astro-36ssibgs-7"] { view-transition-name: responder-chain; }@layer astro { ::view-transition-old(responder-chain) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }::view-transition-new(responder-chain) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; }[data-astro-transition=back]::view-transition-old(responder-chain) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }[data-astro-transition=back]::view-transition-new(responder-chain) { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; } }[data-astro-transition-fallback="old"] [data-astro-transition-scope="astro-36ssibgs-7"],
			[data-astro-transition-fallback="old"][data-astro-transition-scope="astro-36ssibgs-7"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }[data-astro-transition-fallback="new"] [data-astro-transition-scope="astro-36ssibgs-7"],
			[data-astro-transition-fallback="new"][data-astro-transition-scope="astro-36ssibgs-7"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; }[data-astro-transition=back][data-astro-transition-fallback="old"] [data-astro-transition-scope="astro-36ssibgs-7"],
			[data-astro-transition=back][data-astro-transition-fallback="old"][data-astro-transition-scope="astro-36ssibgs-7"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeOut; }[data-astro-transition=back][data-astro-transition-fallback="new"] [data-astro-transition-scope="astro-36ssibgs-7"],
			[data-astro-transition=back][data-astro-transition-fallback="new"][data-astro-transition-scope="astro-36ssibgs-7"] { 
	animation-duration: 180ms;
	animation-timing-function: cubic-bezier(0.76, 0, 0.24, 1);
	animation-fill-mode: both;
	animation-name: astroFadeIn; }</style></head> <body data-astro-cid-sckkx6r4>  <script type="application/ld+json">{"@context":"https://schema.org","@type":"BlogPosting","headline":"Hacking Block Support Into UIMenuItem","description":"Implement block support for UIMenuItem by swizzling the responder chain to enable cleaner API patterns.","author":{"@type":"Person","name":"Peter Steinberger","url":"https://steipete.me/about"},"datePublished":"2012-07-17T16:22:00.000Z","dateModified":"2012-07-17T16:22:00.000Z","publisher":{"@type":"Person","name":"Peter Steinberger","logo":{"@type":"ImageObject","url":"https://steipete.me/peter-avatar.jpg"}},"mainEntityOfPage":{"@type":"WebPage","@id":"https://steipete.me/posts/2012/hacking-block-support-into-uimenuitem"},"image":"https://steipete.me/posts/2012/hacking-block-support-into-uimenuitem/index.png","articleSection":"iOS-Development","keywords":"iOS-Development, Objective-C, UIKit, Blocks, Method-Swizzling, Responder-Chain","wordCount":2341,"timeRequired":"12 min read"}</script> <header> <a id="skip-to-content" href="#main-content" class="absolute -top-full left-16 z-50 bg-background px-3 py-2 text-accent backdrop-blur-lg transition-all focus:top-4">
Skip to content
</a> <div id="nav-container" class="mx-auto flex max-w-3xl flex-col items-center justify-between sm:flex-row"> <div id="top-nav-wrap" class="relative flex w-full items-baseline justify-between bg-background p-4 sm:items-center sm:py-6"> <a href="/" class="absolute py-1 text-2xl leading-7 font-semibold whitespace-nowrap sm:static"> Peter Steinberger </a> <nav id="nav-menu" class="flex w-full flex-col items-center sm:ml-2 sm:flex-row sm:justify-end sm:space-x-4 sm:py-0"> <button id="menu-btn" class="focus-outline self-end p-2 sm:hidden" aria-label="Open Menu" aria-expanded="false" aria-controls="menu-items"> <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="hidden" id="close-icon"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M18 6l-12 12" /><path d="M6 6l12 12" /></svg> <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-tabler icons-tabler-outline icon-tabler-menu-deep" id="menu-icon"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M4 6h16" /><path d="M7 12h13" /><path d="M10 18h10" /></svg> </button> <ul id="menu-items" class="mt-4 grid w-44 grid-cols-2 place-content-center gap-2 [&#38;>li>a]:block [&#38;>li>a]:px-4 [&#38;>li>a]:py-3 [&#38;>li>a]:text-center [&#38;>li>a]:font-medium [&#38;>li>a]:hover:text-accent sm:[&#38;>li>a]:px-2 sm:[&#38;>li>a]:py-1 hidden sm:mt-0 sm:ml-0 sm:flex sm:w-auto sm:gap-x-5 sm:gap-y-0"> <li class="col-span-2"> <a href="/posts" class="active-nav">
Posts
</a> </li> <!-- <li class="col-span-2">
            <a href="/tags" class:list={{ "active-nav": isActive("/tags") }}>
              Tags
            </a>
          </li> --> <li class="col-span-2"> <a href="/about">
About
</a> </li>  <li class="col-span-1 flex items-center justify-center"> <a href="/search" class="group inline-block hover:text-accent focus-outline flex p-3 sm:p-1" aria-label="search" title="Search"> <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-tabler icons-tabler-outline icon-tabler-search"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M10 10m-7 0a7 7 0 1 0 14 0a7 7 0 1 0 -14 0" /><path d="M21 21l-6 -6" /></svg> <span class="sr-only">Search</span> </a> </li> <li class="col-span-1 flex items-center justify-center"> <button id="theme-btn" class="focus-outline relative size-12 p-4 sm:size-8 hover:[&>svg]:stroke-accent" title="Toggles light & dark" aria-label="auto" aria-live="polite"> <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="absolute top-[50%] left-[50%] -translate-[50%] scale-100 rotate-0 transition-all dark:scale-0 dark:-rotate-90"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M12 3c.132 0 .263 0 .393 0a7.5 7.5 0 0 0 7.92 12.446a9 9 0 1 1 -8.313 -12.454z" /></svg> <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="absolute top-[50%] left-[50%] -translate-[50%] scale-0 rotate-90 transition-all dark:scale-100 dark:rotate-0"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M14.828 14.828a4 4 0 1 0 -5.656 -5.656a4 4 0 0 0 5.656 5.656z" /><path d="M6.343 17.657l-1.414 1.414" /><path d="M6.343 6.343l-1.414 -1.414" /><path d="M17.657 6.343l1.414 -1.414" /><path d="M17.657 17.657l1.414 1.414" /><path d="M4 12h-2" /><path d="M12 4v-2" /><path d="M20 12h2" /><path d="M12 20v2" /></svg> </button> </li> </ul> </nav> </div> </div> <div class="max-w-3xl mx-auto px-4"> <hr class="border-border" aria-hidden="true"> </div> </header> <script type="module">function s(){const e=document.querySelector("#menu-btn"),t=document.querySelector("#menu-items"),n=document.querySelector("#menu-icon"),o=document.querySelector("#close-icon");!e||!t||!n||!o||e.addEventListener("click",()=>{const c=e.getAttribute("aria-expanded")==="true";e.setAttribute("aria-expanded",c?"false":"true"),e.setAttribute("aria-label",c?"Open Menu":"Close Menu"),t.classList.toggle("hidden"),n.classList.toggle("hidden"),o.classList.toggle("hidden")})}s();document.addEventListener("astro:after-swap",()=>{s()});</script> <script type="module">function o(){const t=document.querySelector("#back-button"),e=sessionStorage.getItem("backUrl");e&&t&&(t.href=e)}document.addEventListener("astro:page-load",o);o();</script> <main id="main-content" class="mx-auto w-full max-w-3xl px-4 pb-12 mt-8" data-pagefind-body data-astro-cid-vj4tpspi> <h1 class="inline-block text-2xl font-bold text-accent sm:text-3xl" data-astro-cid-vj4tpspi data-astro-transition-scope="astro-pu2sknsl-1"> Hacking Block Support Into UIMenuItem </h1> <div class="flex items-center justify-between gap-4 mt-2 mb-6" data-astro-cid-vj4tpspi> <div class="flex items-center gap-3" data-astro-cid-vj4tpspi> <div class="flex items-center gap-2 opacity-80"> <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block size-5 min-w-[1.25rem]"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M4 7a2 2 0 0 1 2 -2h12a2 2 0 0 1 2 2v12a2 2 0 0 1 -2 2h-12a2 2 0 0 1 -2 -2v-12z" /><path d="M16 3v4" /><path d="M8 3v4" /><path d="M4 11h16" /><path d="M7 14h.013" /><path d="M10.01 14h.005" /><path d="M13.01 14h.005" /><path d="M16.015 14h.005" /><path d="M13.015 17h.005" /><path d="M7.01 17h.005" /><path d="M10.01 17h.005" /></svg> <span class="sr-only">Published:</span> <span class="text-sm italic sm:text-base"> <time datetime="2012-07-17T16:22:00.000Z">17 Jul, 2012</time> </span> </div>  <span class="text-foreground/30" data-astro-cid-vj4tpspi>•</span> <span class="text-sm text-foreground/60" data-astro-cid-vj4tpspi>12 min read</span>  </div> <div class="opacity-80 max-sm:hidden"><span aria-hidden="true" class="max-sm:hidden">
|
</span><a class="space-x-1.5 hover:opacity-75" href="https://github.com/steipete/steipete.me/edit/main/src/content/blog/2012/hacking-block-support-into-uimenuitem.md" rel="noopener noreferrer" target="_blank"><svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block size-6"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M7 7h-1a2 2 0 0 0 -2 2v9a2 2 0 0 0 2 2h9a2 2 0 0 0 2 -2v-1" /><path d="M20.385 6.585a2.1 2.1 0 0 0 -2.97 -2.97l-8.415 8.385v3h3l8.385 -8.415z" /><path d="M16 5l3 3" /></svg><span class="italic max-sm:text-sm sm:inline">Edit on GitHub</span></a></div> </div> <article id="article" class="mx-auto prose mt-6 max-w-3xl" data-astro-cid-vj4tpspi>  <p>tl;dr: UIMenuItem! Blocks! <a href="https://github.com/steipete/PSMenuItem">Get the code on GitHub.</a></p>
<p>While developing a new version of <a href="http://pspdfkit.com">PSPDFKit</a>, I started using UIMenuController more and more. The first thing you’ll notice is that it’s different from your typical target/action pattern, in that the target is missing:</p>
<pre class="astro-code astro-code-themes min-light night-owl" style="background-color:#ffffff;--shiki-dark-bg:#011627;color:#24292eff;--shiki-dark:#d6deeb; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;" tabindex="0" data-language="objective-c"><code><span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    [UIMenuItem </span><span style="color:#6F42C1;--shiki-dark:#C5E478">alloc</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">] initWithTitle:</span><span style="color:#22863A;--shiki-dark:#D9F5DD">@"</span><span style="color:#22863A;--shiki-dark:#ECC48D">Title</span><span style="color:#22863A;--shiki-dark:#D9F5DD">"</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> action:</span><span style="color:#D32F2F;--shiki-dark:#C792EA">@selector</span><span style="color:#24292EFF;--shiki-dark:#C792EA">(</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic">menuItemAction:</span><span style="color:#24292EFF;--shiki-dark:#C792EA">)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span></code></pre>
<p>This is actually pretty genius, in part. iOS checks if the @selector can be invoked calling <code>canPerformAction:withSender:</code>, which is part of UIResponder. If the object returns NO, the system walks up the UIResponder chain until it finds an object that returns YES or <code>nextResponder</code> returns nil.</p>
<p>A typical responder chain looks like this: UIView -> UIViewController -> UINavigationController -> UIApplication -> AppDelegate.</p>
<p>This works very, very well most of the time, but there’s one problem: if you need to have different items with the same title, all you can do is check the UIMenuController.menuItems to get the selected menu item. This is not very elegant. Especially now that we have <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Blocks/Articles/00_Introduction.html">blocks</a> since iOS 4.0, I kept thinking if there’s a better way to do this. I googled around, and most people solve this by subclassing <code>UIMenuItem</code> and adding some category. This works, but not for <code>performAction:withSender:</code> since that’s not implemented on <code>UIMenuItem</code> but somewhere in the UIEvent family, and there’s no trace of what UIMenuItem is currently invoked.</p>
<p>So first, let’s revisit how we usually show a menu:</p>
<ol>
<li>Register to be able to become first responder</li>
</ol>
<pre class="astro-code astro-code-themes min-light night-owl" style="background-color:#ffffff;--shiki-dark-bg:#011627;color:#24292eff;--shiki-dark:#d6deeb; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;" tabindex="0" data-language="objective-c"><code><span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">// in the .h</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">@interface</span><span style="color:#6F42C1;--shiki-dark:#D6DEEB"> MyView</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> : </span><span style="color:#6F42C1;--shiki-dark:#C5E478">UIView</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">@end</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">// in the .m</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#7FDBCA">-</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">BOOL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)canBecomeFirstResponder {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    return</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> YES</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">}</span></span></code></pre>
<ol start="2">
<li>Add the menu items when needed</li>
</ol>
<pre class="astro-code astro-code-themes min-light night-owl" style="background-color:#ffffff;--shiki-dark-bg:#011627;color:#24292eff;--shiki-dark:#d6deeb; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;" tabindex="0" data-language="objective-c"><code><span class="line"><span style="color:#D32F2F;--shiki-dark:#7FDBCA">-</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">void</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)showMenuAction:(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">id</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)sender {</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    UIMenuItem </span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">*</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">testItem </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> [[UIMenuItem </span><span style="color:#6F42C1;--shiki-dark:#C5E478">alloc</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">] </span><span style="color:#6F42C1;--shiki-dark:#C5E478">initWithTitle</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#22863A;--shiki-dark:#D9F5DD">@"</span><span style="color:#22863A;--shiki-dark:#ECC48D">Test</span><span style="color:#22863A;--shiki-dark:#D9F5DD">"</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> action</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#D32F2F;--shiki-dark:#C792EA">@selector</span><span style="color:#6F42C1;--shiki-dark:#C792EA">(</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic">test:</span><span style="color:#6F42C1;--shiki-dark:#C792EA">)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    UIMenuController </span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">*</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">menuController </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> [UIMenuController </span><span style="color:#6F42C1;--shiki-dark:#C5E478">sharedMenuController</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    [menuController </span><span style="color:#6F42C1;--shiki-dark:#C5E478">setMenuItems</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">[</span><span style="color:#1976D2;--shiki-dark:#C5E478">NSArray</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> arrayWithObject</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">testItem]</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    [menuController </span><span style="color:#6F42C1;--shiki-dark:#C5E478">setTargetRect</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#7FDBCA">self</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">.bounds </span><span style="color:#6F42C1;--shiki-dark:#C5E478">inView</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#7FDBCA">self</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    [menuController </span><span style="color:#6F42C1;--shiki-dark:#C5E478">update</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    [menuController </span><span style="color:#6F42C1;--shiki-dark:#C5E478">setMenuVisible</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#1976D2;--shiki-dark:#82AAFF">YES</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> animated</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#1976D2;--shiki-dark:#82AAFF">YES</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">}</span></span></code></pre>
<ol start="3">
<li>Implement <code>canPerformAction:withSender</code> to respond to actions</li>
</ol>
<pre class="astro-code astro-code-themes min-light night-owl" style="background-color:#ffffff;--shiki-dark-bg:#011627;color:#24292eff;--shiki-dark:#d6deeb; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;" tabindex="0" data-language="objective-c"><code><span class="line"><span style="color:#D32F2F;--shiki-dark:#7FDBCA">-</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">BOOL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)canPerformAction:(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">SEL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)action withSender:(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">id</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)sender {</span></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">    // We return YES (or call super) for the actions we actually implement</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (action </span><span style="color:#D32F2F;--shiki-dark:#C792EA">==</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> @selector</span><span style="color:#24292EFF;--shiki-dark:#C792EA">(</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic">test:</span><span style="color:#24292EFF;--shiki-dark:#C792EA">)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">) {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">        return</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> YES</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    }</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    return</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> NO</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">; </span><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">// NO for all actions that we don't implement</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#7FDBCA">-</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">void</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)test:(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">id</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)sender {</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-dark:#C5E478">    NSLog</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(</span><span style="color:#22863A;--shiki-dark:#D9F5DD">@"</span><span style="color:#22863A;--shiki-dark:#ECC48D">test</span><span style="color:#22863A;--shiki-dark:#D9F5DD">"</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">);</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">}</span></span></code></pre>
<p>So we’ve got a working menu system, but what if we’d want another menu item “Test 2” to do something completely different? Or if we’d want to encapsulate menu creation in a separate file? Usually you would either create a category on UIResponder (which works, but it’s a bit ugly to add 10 methods in a category) or use <code>didHideMenuNotification</code> and query what selection was last active.</p>
<p>Some third-party controls solve this problem by subclassing UIMenuItem and adding an “id” property. But I’m quite sure this is against the iOS SDK Agreement, since you’re not supposed to subclass UIKit controls that are not explicitly marked for subclassing. It’s also a chore to manually check if your menu item is the one that is invoked.</p>
<p>Another idea is to use the sender ID to figure out what menu is active. However, the sender passed to <code>canPerformAction:withSender:</code> is sometimes <code>UIMenuController</code>, sometimes <code>UICalloutBarButtonItem</code>; this isn’t reliable.</p>
<p>What we really want is a <code>UIMenuItem</code> category that adds block support. Something like this:</p>
<pre class="astro-code astro-code-themes min-light night-owl" style="background-color:#ffffff;--shiki-dark-bg:#011627;color:#24292eff;--shiki-dark:#d6deeb; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;" tabindex="0" data-language="objective-c"><code><span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">UIMenuItem </span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">*</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">mailItem </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> [[UIMenuItem </span><span style="color:#6F42C1;--shiki-dark:#C5E478">alloc</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">] </span><span style="color:#6F42C1;--shiki-dark:#C5E478">initWithTitle</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">NSLocalizedString</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(</span><span style="color:#22863A;--shiki-dark:#D9F5DD">@"</span><span style="color:#22863A;--shiki-dark:#ECC48D">Mail</span><span style="color:#22863A;--shiki-dark:#D9F5DD">"</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#22863A;--shiki-dark:#D9F5DD"> @""</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">) </span><span style="color:#6F42C1;--shiki-dark:#C5E478">block</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">^</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">{</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-dark:#C5E478">    NSLog</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(</span><span style="color:#22863A;--shiki-dark:#D9F5DD">@"</span><span style="color:#22863A;--shiki-dark:#ECC48D">Clicked on Mail</span><span style="color:#22863A;--shiki-dark:#D9F5DD">"</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">);</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-dark:#82AAFF">}</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span></code></pre>
<p>I really really like blocks for their flexibility and encapsulation, and I saw this as the only solution that’s “right”. But, at first sight this solution isn’t possible. We can’t store the block in the UIMenuItem, and we can’t use a simple NSDictionary for mapping, since there’s simply no trace of the selected menu item when an action is triggered. If UIMenuItem would send the action to a view, we could swizzle <code>performAction:</code>, hook into it, and check what action is triggered, but the code flow is exactly the opposite: the view calls the action. Calling the action will call the target, which is “missing” with <code>UIMenuItem</code>. So the view that receives <code>test:</code> doesn’t really know what <code>UIMenuItem</code> is calling it.</p>
<p>(I <em>briefly</em> considered overriding <code>test:</code> to check what <code>UIMenuItem</code> is visible on each call, but this would be a multi-step approach, and I don’t want to hook into the UIMenu system on a lower level from multiple sides. That feels really hacky, and I’m afraid that it might break more easily with future iOS versions.)</p>
<p>After playing around with a few more solutions (and rejecting them), I looked around in a few popular iOS frameworks to see how they handle this. <a href="https://github.com/mattgemmell/MGTwitterEngine">MGTwitterEngine</a> uses a delegate pattern to map asynchronous operations to completion delegates. <a href="https://github.com/AFNetworking/AFNetworking">AFNetworking</a> uses blocks for completion handlers. Then it hit me: what if the action is just a placeholder, and we can replace it with something else? We could <a href="http://cocoadev.com/wiki/MethodSwizzling">swizzle</a> canPerformAction: and check if we get our placeholder method. If yes, replace it with some other @selector.</p>
<p>But, we face two problems here: first, we need to remember what @selector needs what block. And second - what @selector to use? The solution for the first is the classic Objective-C one: use a static dictionary to remember the mapping from action to block. For the second problem, we need to synthesize a method.</p>
<p>There is a big drawback, however: this method MUST be implemented on the application! So what to pick? Apple has a little selection of <em>private</em> or undocumented methods, and looking at all selectors, I’ve found one that looked both unique and safe: <code>_accessibilitySetFocusToLiveRegion:</code>. This is part of the accessibility framework, but there’s no trace of it in normal AppKit code, and implementing it doesn’t break anything.</p>
<h2 id="implementation">Implementation</h2>
<p>Now that we have the general idea, let’s implement UIMenuItem+PSMenuItem to capture initialization with a block:</p>
<pre class="astro-code astro-code-themes min-light night-owl" style="background-color:#ffffff;--shiki-dark-bg:#011627;color:#24292eff;--shiki-dark:#d6deeb; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;" tabindex="0" data-language="objective-c"><code><span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">typedef</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> void</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">^</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">PSMenuItemBlock)(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">id</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> sender);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">@interface</span><span style="color:#6F42C1;--shiki-dark:#D6DEEB"> UIMenuItem</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (PSMenuItem)</span></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">// Static way, doesn't need custom view</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">- (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">id</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">initWithTitle</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(</span><span style="color:#1976D2;--shiki-dark:#C5E478">NSString</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> *</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)</span><span style="color:#FF9800;--shiki-dark:#7FDBCA">title</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> block</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(PSMenuItemBlock)</span><span style="color:#FF9800;--shiki-dark:#7FDBCA">block</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">@end</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">@implementation</span><span style="color:#6F42C1;--shiki-dark:#D6DEEB"> UIMenuItem</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (PSMenuItem)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">- (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">id</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">initWithTitle</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(</span><span style="color:#1976D2;--shiki-dark:#C5E478">NSString</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> *</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)</span><span style="color:#FF9800;--shiki-dark:#7FDBCA">title</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> block</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(PSMenuItemBlock)</span><span style="color:#FF9800;--shiki-dark:#7FDBCA">block</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> ((</span><span style="color:#24292EFF;--shiki-dark:#7FDBCA">self</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> =</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> [</span><span style="color:#24292EFF;--shiki-dark:#7FDBCA">self</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> initWithTitle</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">title </span><span style="color:#6F42C1;--shiki-dark:#C5E478">action</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#D32F2F;--shiki-dark:#C792EA">@selector</span><span style="color:#6F42C1;--shiki-dark:#C792EA">(</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic">_accessibilitySetFocusToLiveRegion:</span><span style="color:#6F42C1;--shiki-dark:#C792EA">)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">])) {</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">        PSMenuItemWithBlockAction</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(</span><span style="color:#6F42C1;--shiki-dark:#7FDBCA">self</span><span style="color:#212121;--shiki-dark:#82AAFF">.</span><span style="color:#6F42C1;--shiki-dark:#C5E478">action</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> block)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    }</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    return</span><span style="color:#24292EFF;--shiki-dark:#7FDBCA"> self</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">@end</span></span></code></pre>
<p>That was simple. Now we need to implement the swizzling to capture <code>canPerformAction:withSender:</code> and replace it on the fly. The swizzling itself is done via a class that is automatically initialized via <code>dispatch_once</code> and in a “+load” method.</p>
<pre class="astro-code astro-code-themes min-light night-owl" style="background-color:#ffffff;--shiki-dark-bg:#011627;color:#24292eff;--shiki-dark:#d6deeb; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;" tabindex="0" data-language="objective-c"><code><span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">#</span><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">pragma</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic"> clang</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic"> diagnostic</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic"> push</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">#</span><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">pragma</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic"> clang</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic"> diagnostic</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic"> ignored</span><span style="color:#22863A;--shiki-dark:#D9F5DD"> "</span><span style="color:#22863A;--shiki-dark:#ECC48D">-Wundeclared-selector</span><span style="color:#22863A;--shiki-dark:#D9F5DD">"</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">static</span><span style="color:#1976D2;--shiki-dark:#C5E478"> NSString</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> *</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">PSMenuItemEnabledActions </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#22863A;--shiki-dark:#D9F5DD"> @"</span><span style="color:#22863A;--shiki-dark:#ECC48D">PSMenuItemEnabledActions</span><span style="color:#22863A;--shiki-dark:#D9F5DD">"</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">static</span><span style="color:#1976D2;--shiki-dark:#C5E478"> NSMutableDictionary</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> *</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">PSMenuItemActionBlocks </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> nil</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">// action helper. the real magic is done here.</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">NS_INLINE </span><span style="color:#D32F2F;--shiki-dark:#C792EA">void</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> PSMenuItemWithBlockAction</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">SEL</span><span style="color:#24292EFF;--shiki-dark:#D7DBE0"> action</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> PSMenuItemBlock </span><span style="color:#24292EFF;--shiki-dark:#D7DBE0">block</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">) {</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">    NSCParameterAssert</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(action)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">    NSCParameterAssert</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(block)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">    // use one dictionary per action for UX safety.</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    static</span><span style="color:#1976D2;--shiki-dark:#C5E478"> dispatch_once_t</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> predicate;</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">    dispatch_once</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">&#x26;</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">predicate</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> ^</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">{</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-dark:#82AAFF">        PSMenuItemActionBlocks </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> [[</span><span style="color:#1976D2;--shiki-dark:#C5E478">NSMutableDictionary</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> alloc</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">] </span><span style="color:#6F42C1;--shiki-dark:#C5E478">init</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">];</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-dark:#82AAFF">    })</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#C5E478">    PSMenuItemActionBlocks</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">[@(action)] </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> [block </span><span style="color:#6F42C1;--shiki-dark:#C5E478">copy</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">    // register action</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    [PSMenuItem </span><span style="color:#6F42C1;--shiki-dark:#C5E478">associateMenuItemAction</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">action</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">@implementation</span><span style="color:#6F42C1;--shiki-dark:#D6DEEB"> PSMenuItem</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">// will swizzle for great glory.</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">+ (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">void</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">load</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    static</span><span style="color:#1976D2;--shiki-dark:#C5E478"> dispatch_once_t</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> onceToken;</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">    dispatch_once</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">&#x26;</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">onceToken</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> ^</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">{</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-dark:#82AAFF">    })</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">// globally enable a custom SEL to be used as a menu item.</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">+ (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">void</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">associateMenuItemAction</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">SEL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)</span><span style="color:#FF9800;--shiki-dark:#7FDBCA">action</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    static</span><span style="color:#1976D2;--shiki-dark:#C5E478"> dispatch_once_t</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> oncePredicate;</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">    dispatch_once</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">&#x26;</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">oncePredicate</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> ^</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">{</span></span>
<span class="line"><span style="color:#1976D2;--shiki-dark:#C5E478">        NSDictionary</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> *</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">actions </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> [[</span><span style="color:#1976D2;--shiki-dark:#C5E478">NSUserDefaults</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> standardUserDefaults</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">] </span><span style="color:#6F42C1;--shiki-dark:#C5E478">objectForKey</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">PSMenuItemEnabledActions];</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">        if</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> (actions) {</span></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">            // we have a saved dictionary, try to convert to SEL</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">            for</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> (</span><span style="color:#1976D2;--shiki-dark:#C5E478">NSString</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> *</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">actionStr </span><span style="color:#D32F2F;--shiki-dark:#C792EA">in</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> actions) {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">                SEL</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> selector </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> NSSelectorFromString</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(actionStr);</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-dark:#82AAFF">                [</span><span style="color:#6F42C1;--shiki-dark:#7FDBCA">self</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> associateMenuItemAction</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">selector];</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-dark:#82AAFF">            }</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-dark:#82AAFF">        }</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-dark:#82AAFF">    })</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1976D2;--shiki-dark:#C5E478">    NSString</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> *</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">actionString </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> NSStringFromSelector</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(action);</span></span>
<span class="line"><span style="color:#1976D2;--shiki-dark:#C5E478">    NSUserDefaults</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> *</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">defaults </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> [</span><span style="color:#1976D2;--shiki-dark:#C5E478">NSUserDefaults</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> standardUserDefaults</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#1976D2;--shiki-dark:#C5E478">    NSMutableArray</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> *</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">actions </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> [</span><span style="color:#1976D2;--shiki-dark:#C5E478">NSMutableArray</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> arrayWithArray</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">[defaults </span><span style="color:#6F42C1;--shiki-dark:#C5E478">objectForKey</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">PSMenuItemEnabledActions]</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">!</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">[actions </span><span style="color:#6F42C1;--shiki-dark:#C5E478">containsObject</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">actionString</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">]) {</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">        [actions </span><span style="color:#6F42C1;--shiki-dark:#C5E478">addObject</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">actionString</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">        [defaults </span><span style="color:#6F42C1;--shiki-dark:#C5E478">setObject</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">actions </span><span style="color:#6F42C1;--shiki-dark:#C5E478">forKey</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">PSMenuItemEnabledActions</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">        [defaults </span><span style="color:#6F42C1;--shiki-dark:#C5E478">synchronize</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">        // don't swizzle twice</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">        static</span><span style="color:#1976D2;--shiki-dark:#C5E478"> NSMutableSet</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> *</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">swizzledClasses </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> nil</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">        static</span><span style="color:#1976D2;--shiki-dark:#C5E478"> dispatch_once_t</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> onceToken;</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">        dispatch_once</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">&#x26;</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">onceToken</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> ^</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">{</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-dark:#82AAFF">            swizzledClasses </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> [[</span><span style="color:#1976D2;--shiki-dark:#C5E478">NSMutableSet</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> alloc</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">] </span><span style="color:#6F42C1;--shiki-dark:#C5E478">init</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">];</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-dark:#82AAFF">        })</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">        // now swizzle everything UIResponder for great victory</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">        @</span><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">synchronized</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(</span><span style="color:#24292EFF;--shiki-dark:#7FDBCA">self</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">) {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">            const</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> char</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> *</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">className;</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">            Class</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> currentClass</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> superClass </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> [UIResponder </span><span style="color:#6F42C1;--shiki-dark:#C5E478">class</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">            int</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> numClasses</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> newNumClasses;</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">            Class</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> *</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">classes </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> NULL</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> *</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">newClasses;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">            numClasses </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> objc_getClassList</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(</span><span style="color:#1976D2;--shiki-dark:#82AAFF">NULL</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#1976D2;--shiki-dark:#F78C6C"> 0</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">            if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (numClasses </span><span style="color:#D32F2F;--shiki-dark:#C792EA">></span><span style="color:#1976D2;--shiki-dark:#F78C6C"> 0</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">) {</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">                classes </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (__unsafe_unretained </span><span style="color:#D32F2F;--shiki-dark:#C792EA">Class</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> *</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">malloc</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">sizeof</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">Class</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">) </span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">*</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> numClasses)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">                numClasses </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> objc_getClassList</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(classes</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> numClasses)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">                newClasses </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> NULL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">                newNumClasses </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> numClasses;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">                for</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">int</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> classIndex </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#1976D2;--shiki-dark:#F78C6C"> 0</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">; classIndex </span><span style="color:#D32F2F;--shiki-dark:#C792EA">&#x3C;</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> numClasses; </span><span style="color:#D32F2F;--shiki-dark:#C792EA">++</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">classIndex) {</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">                    currentClass </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#24292EFF;--shiki-dark:#C5E478"> classes</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">[classIndex];</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">                    if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">!</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">class_getSuperclass</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(currentClass)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">) {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">                        continue</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">                    }</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">                    if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> ((</span><span style="color:#D32F2F;--shiki-dark:#C792EA">int</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">*</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">id</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> SEL</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> SEL</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> id</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">))</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">class_getMethodImplementation</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(currentClass</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> @selector</span><span style="color:#6F42C1;--shiki-dark:#C792EA">(</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic">canPerformAction:withSender:</span><span style="color:#6F42C1;--shiki-dark:#C792EA">)</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">)</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> ==</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">int</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">*</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">id</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> SEL</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> SEL</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> id</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">))</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">class_getMethodImplementation</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(superClass</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> @selector</span><span style="color:#6F42C1;--shiki-dark:#C792EA">(</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic">canPerformAction:withSender:</span><span style="color:#6F42C1;--shiki-dark:#C792EA">)</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">) {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">                        continue</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">                    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">                    className </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> class_getName</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(currentClass)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">                    if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">strcmp</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(className</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#22863A;--shiki-dark:#D9F5DD"> "</span><span style="color:#22863A;--shiki-dark:#ECC48D">UICalloutBarButton</span><span style="color:#22863A;--shiki-dark:#D9F5DD">"</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">)</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> ==</span><span style="color:#1976D2;--shiki-dark:#F78C6C"> 0</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">) {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">                        continue</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">                    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">                    if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> ([currentClass </span><span style="color:#6F42C1;--shiki-dark:#C5E478">isSubclassOfClass</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">superClass</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">] </span><span style="color:#D32F2F;--shiki-dark:#C792EA">&#x26;&#x26;</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> !</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">[swizzledClasses </span><span style="color:#6F42C1;--shiki-dark:#C5E478">containsObject</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">currentClass</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">]) {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">                        SEL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> canPerformSelector </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> @selector</span><span style="color:#24292EFF;--shiki-dark:#C792EA">(</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic">canPerformAction:withSender:</span><span style="color:#24292EFF;--shiki-dark:#C792EA">)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">                        if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">class_getInstanceMethod</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(currentClass</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> canPerformSelector)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">) {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">                            IMP</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> origIMP </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> class_getMethodImplementation</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(currentClass</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> canPerformSelector)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">                            SEL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> origIMPSEL </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> NSSelectorFromString</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">([</span><span style="color:#22863A;--shiki-dark:#D9F5DD">@"</span><span style="color:#22863A;--shiki-dark:#ECC48D">_orig_canPerformAction_</span><span style="color:#22863A;--shiki-dark:#D9F5DD">"</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> stringByAppendingString</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#C5E478">NSStringFromClass</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(currentClass)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">]);</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">                            class_addMethod</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(currentClass</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> origIMPSEL</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> origIMP</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> method_getTypeEncoding</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">class_getInstanceMethod</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(currentClass</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> canPerformSelector)))</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">                            // Add the new method and swizzle.</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">                            IMP</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> myIMP </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> imp_implementationWithBlock</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(PSCanPerformActionSwizzleBlock)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">                            method_setImplementation</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">class_getInstanceMethod</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(currentClass</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> canPerformSelector)</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> myIMP)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">                            [swizzledClasses </span><span style="color:#6F42C1;--shiki-dark:#C5E478">addObject</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">currentClass</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1976D2;--shiki-dark:#C5E478">                            NSString</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> *</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">className </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> NSStringFromClass</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(currentClass);</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-dark:#C5E478">                            NSLog</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(</span><span style="color:#22863A;--shiki-dark:#D9F5DD">@"</span><span style="color:#22863A;--shiki-dark:#ECC48D">class </span><span style="color:#1976D2;--shiki-dark:#82AAFF">%@</span><span style="color:#22863A;--shiki-dark:#ECC48D"> has canPerformAction swizzled.</span><span style="color:#22863A;--shiki-dark:#D9F5DD">"</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> className);</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">                        }</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">                    }</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">                }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">                free</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(classes)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">                if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (newClasses) </span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">free</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(newClasses)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">            }</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">        }</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    }</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">// imp_implementationWithBlock doesn't work great on __IPHONE_OS_VERSION_MIN_REQUIRED &#x3C; __IPHONE_4_0, where this is commonly defined to be __IPHONE_3_0.</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">#</span><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> defined(__IPHONE_OS_VERSION_MIN_REQUIRED) &#x26;&#x26; __IPHONE_OS_VERSION_MIN_REQUIRED &#x3C; __IPHONE_4_0</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">#</span><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">import</span><span style="color:#22863A;--shiki-dark:#D9F5DD"> &#x3C;</span><span style="color:#22863A;--shiki-dark:#ECC48D">objc/runtime.h</span><span style="color:#22863A;--shiki-dark:#D9F5DD">></span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">#</span><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">import</span><span style="color:#22863A;--shiki-dark:#D9F5DD"> &#x3C;</span><span style="color:#22863A;--shiki-dark:#ECC48D">objc/objc-runtime.h</span><span style="color:#22863A;--shiki-dark:#D9F5DD">></span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">#</span><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">endif</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">// we declare a helper to find out if custom menu actions are available. otherwise just returns YES if we're the accessibilityFocus selector</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">static</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> IMP</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> PSReplaceCanPerformActionWithTarget</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">SEL</span><span style="color:#24292EFF;--shiki-dark:#D7DBE0"> origSEL</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> IMP</span><span style="color:#24292EFF;--shiki-dark:#D7DBE0"> replaceIMP</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> SEL</span><span style="color:#24292EFF;--shiki-dark:#D7DBE0"> replaceSEL</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> Class</span><span style="color:#24292EFF;--shiki-dark:#D7DBE0"> target</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> BOOL</span><span style="color:#24292EFF;--shiki-dark:#D7DBE0"> aggressive</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">) {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">#</span><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">pragma</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic"> clang</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic"> diagnostic</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic"> push</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">#</span><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">pragma</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic"> clang</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic"> diagnostic</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic"> ignored</span><span style="color:#22863A;--shiki-dark:#D9F5DD"> "</span><span style="color:#22863A;--shiki-dark:#ECC48D">-Wundeclared-selector</span><span style="color:#22863A;--shiki-dark:#D9F5DD">"</span></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">    // first check if the class actually has an IMP for the selector (not every UIResponder has)</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">    IMP</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> origIMP </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> class_getMethodImplementation</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(target</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> origSEL)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">!</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">origIMP) </span><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">return</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> NULL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">    // don't swizzle twice</span></span>
<span class="line"><span style="color:#1976D2;--shiki-dark:#C5E478">    NSString</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> *</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">replaceIMPStr </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> NSStringFromSelector</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(replaceSEL);</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> ([replaceIMPStr </span><span style="color:#6F42C1;--shiki-dark:#C5E478">hasPrefix</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#22863A;--shiki-dark:#D9F5DD">@"</span><span style="color:#22863A;--shiki-dark:#ECC48D">_orig_</span><span style="color:#22863A;--shiki-dark:#D9F5DD">"</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">]) {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">        return</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> NULL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">    @</span><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">try</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> {</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">        Method origMethod </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> class_getInstanceMethod</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(target</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> origSEL)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">        Method replaceMethod </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> class_getInstanceMethod</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(target</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> replaceSEL)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">        if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(replaceMethod </span><span style="color:#D32F2F;--shiki-dark:#C792EA">&#x26;&#x26;</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> origMethod) {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">            if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">class_addMethod</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(target</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> origSEL</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> method_getImplementation</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(replaceMethod)</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> method_getTypeEncoding</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(replaceMethod))</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">) {</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">                class_replaceMethod</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(target</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> replaceSEL</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> method_getImplementation</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(origMethod)</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> method_getTypeEncoding</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(origMethod))</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">                return</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> method_getImplementation</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(origMethod)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">            }</span><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">else</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> {</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">                method_exchangeImplementations</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(origMethod</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> replaceMethod)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">                return</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> method_getImplementation</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(replaceMethod)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">            }</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">        }</span><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">else</span><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic"> if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(aggressive) {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">            return</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> method_setImplementation</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(origMethod</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> replaceIMP)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">        }</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    }</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">    @</span><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">catch</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (</span><span style="color:#1976D2;--shiki-dark:#C5E478">NSException</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> *</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">exception) {</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-dark:#C5E478">        NSLog</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(</span><span style="color:#22863A;--shiki-dark:#D9F5DD">@"</span><span style="color:#22863A;--shiki-dark:#ECC48D">Failed to swizzle: </span><span style="color:#1976D2;--shiki-dark:#82AAFF">%@</span><span style="color:#22863A;--shiki-dark:#D9F5DD">"</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> exception);</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    }</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    return</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> NULL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">static</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> BOOL</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> PSCanPerformActionSwizzleBlock</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">id</span><span style="color:#24292EFF;--shiki-dark:#D7DBE0"> self</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> SEL</span><span style="color:#24292EFF;--shiki-dark:#D7DBE0"> _cmd</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> SEL</span><span style="color:#24292EFF;--shiki-dark:#D7DBE0"> action</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> id</span><span style="color:#24292EFF;--shiki-dark:#D7DBE0"> sender</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">) {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    for</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (</span><span style="color:#1976D2;--shiki-dark:#C5E478">NSData</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> *</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">sel </span><span style="color:#D32F2F;--shiki-dark:#C792EA">in</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> PSMenuItemActionBlocks) {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">        SEL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> selector </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> nil</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">        [sel </span><span style="color:#6F42C1;--shiki-dark:#C5E478">getBytes</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">&#x26;</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">selector </span><span style="color:#6F42C1;--shiki-dark:#C5E478">length</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#C5E478">sizeof</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(selector)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">        if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (selector </span><span style="color:#D32F2F;--shiki-dark:#C792EA">==</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> action) {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">            return</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> YES</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">        }</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">    // call original implementation if the selector doesn't match</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">    SEL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> origSel </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> NSSelectorFromString</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">([</span><span style="color:#22863A;--shiki-dark:#D9F5DD">@"</span><span style="color:#22863A;--shiki-dark:#ECC48D">_orig_canPerformAction_</span><span style="color:#22863A;--shiki-dark:#D9F5DD">"</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> stringByAppendingString</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#C5E478">NSStringFromClass</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">([</span><span style="color:#6F42C1;--shiki-dark:#7FDBCA">self</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> class</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">])</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">]);</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">    BOOL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> retVal </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> NO</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">    // don't crash if the original Method doesn't exist anymore. Should never happen</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> ([</span><span style="color:#24292EFF;--shiki-dark:#7FDBCA">self</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> respondsToSelector</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">origSel</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">]) {</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">        retVal </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">BOOL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">objc_msgSend</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(</span><span style="color:#6F42C1;--shiki-dark:#7FDBCA">self</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> origSel</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> action</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> sender)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    return</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> retVal;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">+ (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">BOOL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">validateAction</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">SEL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)</span><span style="color:#FF9800;--shiki-dark:#7FDBCA">selector</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    for</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (</span><span style="color:#1976D2;--shiki-dark:#C5E478">NSData</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> *</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">sel </span><span style="color:#D32F2F;--shiki-dark:#C792EA">in</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> PSMenuItemActionBlocks) {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">        SEL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> action </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> nil</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">        [sel </span><span style="color:#6F42C1;--shiki-dark:#C5E478">getBytes</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">&#x26;</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">action </span><span style="color:#6F42C1;--shiki-dark:#C5E478">length</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#C5E478">sizeof</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(action)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">        if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (action </span><span style="color:#D32F2F;--shiki-dark:#C792EA">==</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> selector) {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">            return</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> YES</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">        }</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    }</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    return</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> NO</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">+ (PSMenuItemBlock)</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">blockForAction</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">SEL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)</span><span style="color:#FF9800;--shiki-dark:#7FDBCA">selector</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    return</span><span style="color:#24292EFF;--shiki-dark:#C5E478"> PSMenuItemActionBlocks</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">[@(selector)];</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">+ (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">void</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">performAction</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">SEL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)</span><span style="color:#FF9800;--shiki-dark:#7FDBCA">selector</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> withSender</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">id</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)</span><span style="color:#FF9800;--shiki-dark:#7FDBCA">sender</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> {</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    PSMenuItemBlock callBlock </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#24292EFF;--shiki-dark:#C5E478"> PSMenuItemActionBlocks</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">[@(selector)];</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (callBlock) {</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">        callBlock</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(sender)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    }</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">+ (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">void</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">performAccessibilityActionWithSender</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">id</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)</span><span style="color:#FF9800;--shiki-dark:#7FDBCA">sender</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> {</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    [</span><span style="color:#24292EFF;--shiki-dark:#7FDBCA">self</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> performAction</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#D32F2F;--shiki-dark:#C792EA">@selector</span><span style="color:#6F42C1;--shiki-dark:#C792EA">(</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic">_accessibilitySetFocusToLiveRegion:</span><span style="color:#6F42C1;--shiki-dark:#C792EA">)</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> withSender</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">sender</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">@end</span></span></code></pre>
<p>Now we hook into <code>UIResponder</code> (I’ve decided against subclassing), and we need to override the action response. This looks funky but actually works:</p>
<pre class="astro-code astro-code-themes min-light night-owl" style="background-color:#ffffff;--shiki-dark-bg:#011627;color:#24292eff;--shiki-dark:#d6deeb; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;" tabindex="0" data-language="objective-c"><code><span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">@implementation</span><span style="color:#6F42C1;--shiki-dark:#D6DEEB"> UIResponder</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (PSMenuItem)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">// Called instead of _accessibilitySetFocusToLiveRegion:sender.</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">- (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">void</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">_accessibilitySetFocusToLiveRegion</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">id</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)</span><span style="color:#FF9800;--shiki-dark:#7FDBCA">sender</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> {</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    [PSMenuItem </span><span style="color:#6F42C1;--shiki-dark:#C5E478">performAccessibilityActionWithSender</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">sender</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">@end</span></span></code></pre>
<h2 id="a-sample">A Sample</h2>
<p>The usage is extremely similar to our initial example:</p>
<pre class="astro-code astro-code-themes min-light night-owl" style="background-color:#ffffff;--shiki-dark-bg:#011627;color:#24292eff;--shiki-dark:#d6deeb; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;" tabindex="0" data-language="objective-c"><code><span class="line"><span style="color:#D32F2F;--shiki-dark:#7FDBCA">-</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">void</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)showMenuAction:(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">id</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)sender {</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    UIMenuItem </span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">*</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">testItem </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> [[UIMenuItem </span><span style="color:#6F42C1;--shiki-dark:#C5E478">alloc</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">] </span><span style="color:#6F42C1;--shiki-dark:#C5E478">initWithTitle</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#22863A;--shiki-dark:#D9F5DD">@"</span><span style="color:#22863A;--shiki-dark:#ECC48D">Test</span><span style="color:#22863A;--shiki-dark:#D9F5DD">"</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> block</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">^</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">id</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> sender) {</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-dark:#C5E478">        NSLog</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(</span><span style="color:#22863A;--shiki-dark:#D9F5DD">@"</span><span style="color:#22863A;--shiki-dark:#ECC48D">test</span><span style="color:#22863A;--shiki-dark:#D9F5DD">"</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">);</span></span>
<span class="line"><span style="color:#6F42C1;--shiki-dark:#82AAFF">    }</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    UIMenuController </span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">*</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">menuController </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> [UIMenuController </span><span style="color:#6F42C1;--shiki-dark:#C5E478">sharedMenuController</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    [menuController </span><span style="color:#6F42C1;--shiki-dark:#C5E478">setMenuItems</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">[</span><span style="color:#1976D2;--shiki-dark:#C5E478">NSArray</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> arrayWithObject</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">testItem]</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    [menuController </span><span style="color:#6F42C1;--shiki-dark:#C5E478">setTargetRect</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#7FDBCA">self</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">.bounds </span><span style="color:#6F42C1;--shiki-dark:#C5E478">inView</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#7FDBCA">self</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    [menuController </span><span style="color:#6F42C1;--shiki-dark:#C5E478">update</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    [menuController </span><span style="color:#6F42C1;--shiki-dark:#C5E478">setMenuVisible</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#1976D2;--shiki-dark:#82AAFF">YES</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> animated</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#1976D2;--shiki-dark:#82AAFF">YES</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#7FDBCA">-</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">BOOL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)canBecomeFirstResponder {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    return</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> YES</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">// selectively decide what menu items to display</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#7FDBCA">-</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">BOOL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)canPerformAction:(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">SEL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)action withSender:(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">id</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)sender {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (action </span><span style="color:#D32F2F;--shiki-dark:#C792EA">==</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> @selector</span><span style="color:#24292EFF;--shiki-dark:#C792EA">(</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#C5E478;--shiki-dark-font-style:italic">_accessibilitySetFocusToLiveRegion:</span><span style="color:#24292EFF;--shiki-dark:#C792EA">)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">) {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">        return</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> YES</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    }</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    return</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> NO</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">}</span></span></code></pre>
<p>That’s it! Using blocks has a few disadvantages, though. If we declare the block in the view scope, we automatically capture all variables on the stack and <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Blocks/Articles/bxVariables.html">retain them</a>. That’s not always a good idea. A good rule of thumb is to capture the variables you need, and use <code>[weakSelf self]</code> if you need to capture self. There’s a <a href="http://blog.parse.com/2012/04/12/building-for-ios-with-parse-blocks-and-categories/">whole blog post</a> on Parse’s blog about using blocks with self. In a nutshell:</p>
<pre class="astro-code astro-code-themes min-light night-owl" style="background-color:#ffffff;--shiki-dark-bg:#011627;color:#24292eff;--shiki-dark:#d6deeb; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;" tabindex="0" data-language="objective-c"><code><span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">__weak </span><span style="color:#D32F2F;--shiki-dark:#C792EA">id</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> weakSelf </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#24292EFF;--shiki-dark:#7FDBCA"> self</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">void</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">^</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">myCommonBlock)(UIMenuItem </span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">*</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> UIView </span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">*</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#1976D2;--shiki-dark:#C5E478"> CGRect</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">) </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> ^</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(UIMenuItem </span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">*</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">item</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> UIView </span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">*</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">view</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#1976D2;--shiki-dark:#C5E478"> CGRect</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> bounds) {</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    __strong </span><span style="color:#D32F2F;--shiki-dark:#C792EA">id</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> strongSelf </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> weakSelf;</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (strongSelf) {</span></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">        // ... do your thing</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    }</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">};</span></span></code></pre>
<h2 id="source">Source</h2>
<p>The code is available on GitHub at <a href="https://github.com/steipete/PSMenuItem">https://github.com/steipete/PSMenuItem</a>.</p>
<h2 id="issues--fixes">Issues &#x26; Fixes</h2>
<p><strong>Update (2012/07/18)</strong>: Apple internally calls <code>canPerformAction:withSender:</code> recursively. While searching for possible candidates for <code>_accessibilitySetFocusToLiveRegion:</code>, I started to look at calling all of the undocumented/private methods to check if any of them would look good. However, all of them are actually called by AppKit code, including the accessibility one. I somehow missed that. When an accessibility client queries a responder for elements, this is ultimately called to check what elements are available. What happens then is interesting - our <code>canPerformAction:withSender:</code> swizzled one is called again, but we don’t call the original if our action is not found. This resulted in a classic infinite recursion and a EXC_BAD_ACCESS crash.</p>
<p>To fix this, we might need to restrict swizzling to specific classes, but that would be a lot more code, and we’d likely miss some. The much simpler solution is to use a flag. Before <code>PSCanPerformActionSwizzleBlock</code> executes, we set a thread-local variable to say that we’re already checking, and in that case we always call the original. This way, all <code>canPerformAction:withSender:</code> calls work, but any recursive call will call the original.</p>
<p>Here’s the fix:</p>
<pre class="astro-code astro-code-themes min-light night-owl" style="background-color:#ffffff;--shiki-dark-bg:#011627;color:#24292eff;--shiki-dark:#d6deeb; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;" tabindex="0" data-language="objective-c"><code><span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">static</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> BOOL</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic"> PSCanPerformActionSwizzleBlock</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">(</span><span style="color:#D32F2F;--shiki-dark:#C792EA">id</span><span style="color:#24292EFF;--shiki-dark:#D7DBE0"> self</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> SEL</span><span style="color:#24292EFF;--shiki-dark:#D7DBE0"> _cmd</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> SEL</span><span style="color:#24292EFF;--shiki-dark:#D7DBE0"> action</span><span style="color:#212121;--shiki-dark:#D6DEEB">,</span><span style="color:#D32F2F;--shiki-dark:#C792EA"> id</span><span style="color:#24292EFF;--shiki-dark:#D7DBE0"> sender</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">) {</span></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">    // check if we're already verifying. (The method is called recursively).</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    static</span><span style="color:#1976D2;--shiki-dark:#C5E478"> NSString</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> *</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">PSCanPerformActionKey </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#22863A;--shiki-dark:#D9F5DD"> @"</span><span style="color:#22863A;--shiki-dark:#ECC48D">PSCanPerformActionKey</span><span style="color:#22863A;--shiki-dark:#D9F5DD">"</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">    if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> ([[</span><span style="color:#1976D2;--shiki-dark:#C5E478">NSThread</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> currentThread</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">].threadDictionary </span><span style="color:#6F42C1;--shiki-dark:#C5E478">objectForKey</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">PSCanPerformActionKey</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">]) {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">        SEL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> origSel </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> NSSelectorFromString</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">([</span><span style="color:#22863A;--shiki-dark:#D9F5DD">@"</span><span style="color:#22863A;--shiki-dark:#ECC48D">_orig_canPerformAction_</span><span style="color:#22863A;--shiki-dark:#D9F5DD">"</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> stringByAppendingString</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#C5E478">NSStringFromClass</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">([</span><span style="color:#6F42C1;--shiki-dark:#7FDBCA">self</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> class</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">])</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">]);</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">        BOOL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> retVal </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> NO</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">        if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> ([</span><span style="color:#24292EFF;--shiki-dark:#7FDBCA">self</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> respondsToSelector</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">origSel</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">]) {</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">            retVal </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">BOOL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">objc_msgSend</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(</span><span style="color:#6F42C1;--shiki-dark:#7FDBCA">self</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> origSel</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> action</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> sender)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">        }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">        return</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> retVal;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    [</span><span style="color:#1976D2;--shiki-dark:#C5E478">NSThread</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> currentThread</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">]</span><span style="color:#212121;--shiki-dark:#D6DEEB">.</span><span style="color:#24292EFF;--shiki-dark:#C5E478">threadDictionary</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">[PSCanPerformActionKey] </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> @</span><span style="color:#1976D2;--shiki-dark:#82AAFF">YES</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">    @</span><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">try</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">        for</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (</span><span style="color:#1976D2;--shiki-dark:#C5E478">NSData</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA"> *</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">sel </span><span style="color:#D32F2F;--shiki-dark:#C792EA">in</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> PSMenuItemActionBlocks) {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">            SEL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> selector </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> nil</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">            [sel </span><span style="color:#6F42C1;--shiki-dark:#C5E478">getBytes</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#D32F2F;--shiki-dark:#7FDBCA">&#x26;</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">selector </span><span style="color:#6F42C1;--shiki-dark:#C5E478">length</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#C5E478">sizeof</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(selector)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">            if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (selector </span><span style="color:#D32F2F;--shiki-dark:#C792EA">==</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> action) {</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">                return</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> YES</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">            }</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">        }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">        // call original implementation if the selector doesn't match</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">        SEL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> origSel </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> NSSelectorFromString</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">([</span><span style="color:#22863A;--shiki-dark:#D9F5DD">@"</span><span style="color:#22863A;--shiki-dark:#ECC48D">_orig_canPerformAction_</span><span style="color:#22863A;--shiki-dark:#D9F5DD">"</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> stringByAppendingString</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#C5E478">NSStringFromClass</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">([</span><span style="color:#6F42C1;--shiki-dark:#7FDBCA">self</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> class</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">])</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">]);</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">        BOOL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> retVal </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#1976D2;--shiki-dark:#82AAFF"> NO</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C2C3C5;--shiki-light-font-style:inherit;--shiki-dark:#637777;--shiki-dark-font-style:italic">        // don't crash if the original Method doesn't exist anymore. Should never happen</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">        if</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> ([</span><span style="color:#24292EFF;--shiki-dark:#7FDBCA">self</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> respondsToSelector</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">origSel</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">]) {</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">            retVal </span><span style="color:#D32F2F;--shiki-dark:#C792EA">=</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> (</span><span style="color:#D32F2F;--shiki-dark:#C792EA">BOOL</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">)</span><span style="color:#6F42C1;--shiki-light-font-style:inherit;--shiki-dark:#82AAFF;--shiki-dark-font-style:italic">objc_msgSend</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">(</span><span style="color:#6F42C1;--shiki-dark:#7FDBCA">self</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> origSel</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> action</span><span style="color:#212121;--shiki-dark:#82AAFF">,</span><span style="color:#6F42C1;--shiki-dark:#82AAFF"> sender)</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">        }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">        return</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> retVal;</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    }</span></span>
<span class="line"><span style="color:#D32F2F;--shiki-dark:#C792EA">    @</span><span style="color:#D32F2F;--shiki-light-font-style:inherit;--shiki-dark:#C792EA;--shiki-dark-font-style:italic">finally</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB"> {</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">        [[</span><span style="color:#1976D2;--shiki-dark:#C5E478">NSThread</span><span style="color:#6F42C1;--shiki-dark:#C5E478"> currentThread</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">].threadDictionary </span><span style="color:#6F42C1;--shiki-dark:#C5E478">removeObjectForKey</span><span style="color:#212121;--shiki-dark:#D9F5DD">:</span><span style="color:#6F42C1;--shiki-dark:#82AAFF">PSCanPerformActionKey</span><span style="color:#24292EFF;--shiki-dark:#D6DEEB">];</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">    }</span></span>
<span class="line"><span style="color:#24292EFF;--shiki-dark:#D6DEEB">}</span></span></code></pre>
<p>Using @try/@finally makes sure that we don’t leak the thread-local variable.</p>
<p><strong>Update</strong>: Just realized that there’s a <code>__bridge_retained</code> leak in the imp_implementationWithBlock call, since we never call imp_removeBlock. This is a one-time leak when the class loads, so this is acceptable, especially since we simply can never know when to remove the IMP.</p>
<p><strong>Update (2012/07/18 v2)</strong>: I uploaded a new version which is much simpler and doesn’t depend on a special selector, but instead simply replaces the selector that is used. This works by intercepting the action in UIMenuItem’s init function. A bit nasty, but it’s much cleaner.</p>
<p><strong>Update (2014/01)</strong>: Completely rewrote the category to just use associated objects and method swizzling. Saved the code on <a href="https://github.com/steipete/PSMenuItem">GitHub</a> and created a pod for it.</p> </article> <div class="newsletter-box bg-gray-50 dark:bg-gray-800/50 border border-gray-200 dark:border-gray-700 rounded-lg p-6 my-8" data-astro-cid-nbvfnxgh> <p class="text-sm text-gray-600 dark:text-gray-400 mb-4" data-astro-cid-nbvfnxgh>
New posts, shipping stories, and nerdy links straight to your inbox.
</p> <form action="https://buttondown.com/api/emails/embed-subscribe/steipete" method="post" target="_blank" class="flex flex-col sm:flex-row gap-3" data-astro-cid-nbvfnxgh> <input type="hidden" name="tag" value="steipete" data-astro-cid-nbvfnxgh> <input type="text" name="name" placeholder="Your Name" class="flex-1 px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100 placeholder-gray-500 dark:placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-accent focus:border-transparent" data-astro-cid-nbvfnxgh> <input type="email" name="email" placeholder="Your Email" required class="flex-1 px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100 placeholder-gray-500 dark:placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-accent focus:border-transparent" data-astro-cid-nbvfnxgh> <button type="submit" class="px-6 py-2 bg-accent rounded-md hover:bg-accent/90 transition-colors font-medium whitespace-nowrap focus:outline-none focus:ring-2 focus:ring-accent focus:ring-offset-2" style="color: white;" data-astro-cid-nbvfnxgh>
Subscribe
</button> </form> <p class="text-xs text-gray-500 dark:text-gray-400 mt-3" data-astro-cid-nbvfnxgh>
2× per month, pure signal, zero fluff.
</p> </div>  <hr class="my-8 border-dashed" data-astro-cid-vj4tpspi> <div class="opacity-80 sm:hidden"><span aria-hidden="true" class="max-sm:hidden">
|
</span><a class="space-x-1.5 hover:opacity-75" href="https://github.com/steipete/steipete.me/edit/main/src/content/blog/2012/hacking-block-support-into-uimenuitem.md" rel="noopener noreferrer" target="_blank"><svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block size-6"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M7 7h-1a2 2 0 0 0 -2 2v9a2 2 0 0 0 2 2h9a2 2 0 0 0 2 -2v-1" /><path d="M20.385 6.585a2.1 2.1 0 0 0 -2.97 -2.97l-8.415 8.385v3h3l8.385 -8.415z" /><path d="M16 5l3 3" /></svg><span class="italic max-sm:text-sm sm:inline">Edit on GitHub</span></a></div> <ul class="mt-4 mb-8 sm:my-8" data-astro-cid-vj4tpspi> <li class="group inline-block group-hover:cursor-pointer my-1 underline-offset-4"> <a href="/tags/i-os-development/" class="relative pr-2 text-lg underline decoration-dashed group-hover:-top-0.5 group-hover:text-accent focus-visible:p-1 text-sm" data-astro-transition-scope="astro-36ssibgs-2"> <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block opacity-80 -mr-3.5 size-4"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M5 9l14 0" /><path d="M5 15l14 0" /><path d="M11 4l-4 16" /><path d="M17 4l-4 16" /></svg>
&nbsp;<span>iOS-Development</span> </a> </li><li class="group inline-block group-hover:cursor-pointer my-1 underline-offset-4"> <a href="/tags/objective-c/" class="relative pr-2 text-lg underline decoration-dashed group-hover:-top-0.5 group-hover:text-accent focus-visible:p-1 text-sm" data-astro-transition-scope="astro-36ssibgs-3"> <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block opacity-80 -mr-3.5 size-4"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M5 9l14 0" /><path d="M5 15l14 0" /><path d="M11 4l-4 16" /><path d="M17 4l-4 16" /></svg>
&nbsp;<span>Objective-C</span> </a> </li><li class="group inline-block group-hover:cursor-pointer my-1 underline-offset-4"> <a href="/tags/ui-kit/" class="relative pr-2 text-lg underline decoration-dashed group-hover:-top-0.5 group-hover:text-accent focus-visible:p-1 text-sm" data-astro-transition-scope="astro-36ssibgs-4"> <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block opacity-80 -mr-3.5 size-4"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M5 9l14 0" /><path d="M5 15l14 0" /><path d="M11 4l-4 16" /><path d="M17 4l-4 16" /></svg>
&nbsp;<span>UIKit</span> </a> </li><li class="group inline-block group-hover:cursor-pointer my-1 underline-offset-4"> <a href="/tags/blocks/" class="relative pr-2 text-lg underline decoration-dashed group-hover:-top-0.5 group-hover:text-accent focus-visible:p-1 text-sm" data-astro-transition-scope="astro-36ssibgs-5"> <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block opacity-80 -mr-3.5 size-4"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M5 9l14 0" /><path d="M5 15l14 0" /><path d="M11 4l-4 16" /><path d="M17 4l-4 16" /></svg>
&nbsp;<span>Blocks</span> </a> </li><li class="group inline-block group-hover:cursor-pointer my-1 underline-offset-4"> <a href="/tags/method-swizzling/" class="relative pr-2 text-lg underline decoration-dashed group-hover:-top-0.5 group-hover:text-accent focus-visible:p-1 text-sm" data-astro-transition-scope="astro-36ssibgs-6"> <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block opacity-80 -mr-3.5 size-4"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M5 9l14 0" /><path d="M5 15l14 0" /><path d="M11 4l-4 16" /><path d="M17 4l-4 16" /></svg>
&nbsp;<span>Method-Swizzling</span> </a> </li><li class="group inline-block group-hover:cursor-pointer my-1 underline-offset-4"> <a href="/tags/responder-chain/" class="relative pr-2 text-lg underline decoration-dashed group-hover:-top-0.5 group-hover:text-accent focus-visible:p-1 text-sm" data-astro-transition-scope="astro-36ssibgs-7"> <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block opacity-80 -mr-3.5 size-4"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M5 9l14 0" /><path d="M5 15l14 0" /><path d="M11 4l-4 16" /><path d="M17 4l-4 16" /></svg>
&nbsp;<span>Responder-Chain</span> </a> </li> </ul> <div class="flex flex-col items-center justify-between gap-6 sm:flex-row sm:items-end sm:gap-4" data-astro-cid-vj4tpspi> <div class="flex flex-col flex-wrap items-center justify-center gap-2 sm:gap-1 sm:items-start"> <span class="italic">Share this post on:</span> <div class="text-center flex flex-wrap gap-1"> <a href="https://x.com/intent/post?url=https://steipete.me/posts/2012/hacking-block-support-into-uimenuitem" class="group inline-block hover:text-accent p-3 sm:p-2 hover:rotate-6" title="Share this post on X"><svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block size-8 sm:size-6 fill-transparent stroke-current stroke-2 opacity-90 group-hover:fill-transparent"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M4 4l11.733 16h4.267l-11.733 -16z" /><path d="M4 20l6.768 -6.768m2.46 -2.46l6.772 -6.772" /></svg><span class="sr-only">Share this post on X</span> </a><a href="https://bsky.app/intent/compose?text=https://steipete.me/posts/2012/hacking-block-support-into-uimenuitem" class="group inline-block hover:text-accent p-3 sm:p-2 hover:rotate-6" title="Share this post on BlueSky"><svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block size-8 sm:size-6 fill-transparent stroke-current stroke-2 opacity-90 group-hover:fill-transparent">
  <path d="M12 11.5c-.87-1.69-3.24-4.84-5.44-6.4-2.1-1.48-2.89-1.24-3.42-1.01-.62.27-.72 1.17-.72 1.72 0 .55.3 4.52.5 5.18.65 2.19 2.97 2.93 5.1 2.69.11-.02.22-.03.33-.04-.11.01-.22.03-.33.04-3.13.47-5.91 1.61-2.26 5.67 4.01 4.15 5.5-.89 6.26-3.45.76 2.56 1.64 7.42 6.19 3.45 3.41-3.45.94-5.2-2.19-5.67-.11-.01-.22-.03-.33-.04.11.01.22.02.33.04 2.14.24 4.45-.5 5.1-2.69.2-.66.5-4.63.5-5.18 0-.55-.11-1.49-.72-1.77-.53-.24-1.33-.5-3.44.99-2.2 1.56-4.57 3.71-5.44 6.4Z" stroke-width="1.815" />
</svg><span class="sr-only">Share this post on BlueSky</span> </a><a href="https://www.linkedin.com/sharing/share-offsite/?url=https://steipete.me/posts/2012/hacking-block-support-into-uimenuitem" class="group inline-block hover:text-accent p-3 sm:p-2 hover:rotate-6" title="Share this post on LinkedIn"><svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block size-8 sm:size-6 fill-transparent stroke-current stroke-2 opacity-90 group-hover:fill-transparent"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M8 11v5" /><path d="M8 8v.01" /><path d="M12 16v-5" /><path d="M16 16v-3a2 2 0 1 0 -4 0" /><path d="M3 7a4 4 0 0 1 4 -4h10a4 4 0 0 1 4 4v10a4 4 0 0 1 -4 4h-10a4 4 0 0 1 -4 -4z" /></svg><span class="sr-only">Share this post on LinkedIn</span> </a><a href="https://wa.me/?text=https://steipete.me/posts/2012/hacking-block-support-into-uimenuitem" class="group inline-block hover:text-accent p-3 sm:p-2 hover:rotate-6" title="Share this post via WhatsApp"><svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block size-8 sm:size-6 fill-transparent stroke-current stroke-2 opacity-90 group-hover:fill-transparent"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M3 21l1.65 -3.8a9 9 0 1 1 3.4 2.9l-5.05 .9" /><path d="M9 10a.5 .5 0 0 0 1 0v-1a.5 .5 0 0 0 -1 0v1a5 5 0 0 0 5 5h1a.5 .5 0 0 0 0 -1h-1a.5 .5 0 0 0 0 1" /></svg><span class="sr-only">Share this post via WhatsApp</span> </a><a href="https://www.facebook.com/sharer.php?u=https://steipete.me/posts/2012/hacking-block-support-into-uimenuitem" class="group inline-block hover:text-accent p-3 sm:p-2 hover:rotate-6" title="Share this post on Facebook"><svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block size-8 sm:size-6 fill-transparent stroke-current stroke-2 opacity-90 group-hover:fill-transparent"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M7 10v4h3v7h4v-7h3l1 -4h-4v-2a1 1 0 0 1 1 -1h3v-4h-3a5 5 0 0 0 -5 5v2h-3" /></svg><span class="sr-only">Share this post on Facebook</span> </a><a href="https://t.me/share/url?url=https://steipete.me/posts/2012/hacking-block-support-into-uimenuitem" class="group inline-block hover:text-accent p-3 sm:p-2 hover:rotate-6" title="Share this post via Telegram"><svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block size-8 sm:size-6 fill-transparent stroke-current stroke-2 opacity-90 group-hover:fill-transparent"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M15 10l-4 4l6 6l4 -16l-18 7l4 2l2 6l3 -4" /></svg><span class="sr-only">Share this post via Telegram</span> </a><a href="https://pinterest.com/pin/create/button/?url=https://steipete.me/posts/2012/hacking-block-support-into-uimenuitem" class="group inline-block hover:text-accent p-3 sm:p-2 hover:rotate-6" title="Share this post on Pinterest"><svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block size-8 sm:size-6 fill-transparent stroke-current stroke-2 opacity-90 group-hover:fill-transparent"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M8 20l4 -9" /><path d="M10.7 14c.437 1.263 1.43 2 2.55 2c2.071 0 3.75 -1.554 3.75 -4a5 5 0 1 0 -9.7 1.7" /><path d="M12 12m-9 0a9 9 0 1 0 18 0a9 9 0 1 0 -18 0" /></svg><span class="sr-only">Share this post on Pinterest</span> </a><a href="mailto:?subject=See%20this%20post&#38;body=https://steipete.me/posts/2012/hacking-block-support-into-uimenuitem" class="group inline-block hover:text-accent p-3 sm:p-2 hover:rotate-6" title="Share this post via email"><svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block size-8 sm:size-6 fill-transparent stroke-current stroke-2 opacity-90 group-hover:fill-transparent"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M3 7a2 2 0 0 1 2 -2h14a2 2 0 0 1 2 2v10a2 2 0 0 1 -2 2h-14a2 2 0 0 1 -2 -2v-10z" /><path d="M3 7l9 6l9 -6" /></svg><span class="sr-only">Share this post via email</span> </a> </div> </div> <button id="back-to-top" class="focus-outline py-1 whitespace-nowrap hover:opacity-75" data-astro-cid-vj4tpspi> <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block rotate-90" data-astro-cid-vj4tpspi="true"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M15 6l-6 6l6 6" /></svg> <span data-astro-cid-vj4tpspi>Back to Top</span> </button> </div> <hr class="my-6 border-dashed" data-astro-cid-vj4tpspi> <!-- Previous/Next Post Buttons --> <div data-pagefind-ignore class="flex flex-col sm:flex-row justify-between gap-6" data-prev-url="/posts/2013/uiappearance-for-custom-views" data-next-url="/posts/2012/using-subscripting-with-xcode-4_4-and-ios-4_3" data-astro-cid-vj4tpspi> <a href="/posts/2013/uiappearance-for-custom-views" class="flex gap-2 hover:opacity-75 items-start" id="prev-post-link" data-astro-cid-vj4tpspi> <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block flex-shrink-0 mt-0.5" data-astro-cid-vj4tpspi="true"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M15 6l-6 6l6 6" /></svg> <div class="min-w-0" data-astro-cid-vj4tpspi> <span class="block text-sm text-foreground/70" data-astro-cid-vj4tpspi>Previous Post</span> <div class="text-accent/85" data-astro-cid-vj4tpspi>UIAppearance for Custom Views</div> </div> </a> <a href="/posts/2012/using-subscripting-with-xcode-4_4-and-ios-4_3" class="flex gap-2 hover:opacity-75 items-start justify-end text-right ml-auto" id="next-post-link" data-astro-cid-vj4tpspi> <div class="min-w-0" data-astro-cid-vj4tpspi> <span class="block text-sm text-foreground/70" data-astro-cid-vj4tpspi>Next Post</span> <div class="text-accent/85" data-astro-cid-vj4tpspi>Using Subscripting With Xcode 4.4 And iOS 4.3+</div> </div> <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block flex-shrink-0 mt-0.5" data-astro-cid-vj4tpspi="true"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M9 6l6 6l-6 6" /></svg> </a> </div> </main> <footer class="w-full mt-auto"> <div class="max-w-3xl mx-auto px-0"> <hr class="border-border" aria-hidden="true"> </div> <div class="flex flex-col items-center justify-between py-6 sm:flex-row-reverse sm:py-4"> <div class="flex-wrap justify-center gap-1 flex"> <a href="https://github.com/steipete" class="group inline-block hover:text-accent p-2 hover:rotate-6 sm:p-1" title=" Peter Steinberger on Github"><svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block size-6 scale-125 fill-transparent stroke-current stroke-2 opacity-90 group-hover:fill-transparent sm:scale-110"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M9 19c-4.3 1.4 -4.3 -2.5 -6 -3m12 5v-3.5c0 -1 .1 -1.4 -.5 -2c2.8 -.3 5.5 -1.4 5.5 -6a4.6 4.6 0 0 0 -1.3 -3.2a4.2 4.2 0 0 0 -.1 -3.2s-1.1 -.3 -3.5 1.3a12.3 12.3 0 0 0 -6.2 0c-2.4 -1.6 -3.5 -1.3 -3.5 -1.3a4.2 4.2 0 0 0 -.1 3.2a4.6 4.6 0 0 0 -1.3 3.2c0 4.6 2.7 5.7 5.5 6c-.6 .6 -.6 1.2 -.5 2v3.5" /></svg><span class="sr-only"> Peter Steinberger on Github</span> </a><a href="https://x.com/steipete" class="group inline-block hover:text-accent p-2 hover:rotate-6 sm:p-1" title="Peter Steinberger on X"><svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block size-6 scale-125 fill-transparent stroke-current stroke-2 opacity-90 group-hover:fill-transparent sm:scale-110"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M4 4l11.733 16h4.267l-11.733 -16z" /><path d="M4 20l6.768 -6.768m2.46 -2.46l6.772 -6.772" /></svg><span class="sr-only">Peter Steinberger on X</span> </a><a href="https://bsky.app/profile/steipete.me" class="group inline-block hover:text-accent p-2 hover:rotate-6 sm:p-1" title="Peter Steinberger on BlueSky"><svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block size-6 scale-125 fill-transparent stroke-current stroke-2 opacity-90 group-hover:fill-transparent sm:scale-110">
  <path d="M12 11.5c-.87-1.69-3.24-4.84-5.44-6.4-2.1-1.48-2.89-1.24-3.42-1.01-.62.27-.72 1.17-.72 1.72 0 .55.3 4.52.5 5.18.65 2.19 2.97 2.93 5.1 2.69.11-.02.22-.03.33-.04-.11.01-.22.03-.33.04-3.13.47-5.91 1.61-2.26 5.67 4.01 4.15 5.5-.89 6.26-3.45.76 2.56 1.64 7.42 6.19 3.45 3.41-3.45.94-5.2-2.19-5.67-.11-.01-.22-.03-.33-.04.11.01.22.02.33.04 2.14.24 4.45-.5 5.1-2.69.2-.66.5-4.63.5-5.18 0-.55-.11-1.49-.72-1.77-.53-.24-1.33-.5-3.44.99-2.2 1.56-4.57 3.71-5.44 6.4Z" stroke-width="1.815" />
</svg><span class="sr-only">Peter Steinberger on BlueSky</span> </a><a href="https://www.linkedin.com/in/steipete/" class="group inline-block hover:text-accent p-2 hover:rotate-6 sm:p-1" title="Peter Steinberger on LinkedIn"><svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block size-6 scale-125 fill-transparent stroke-current stroke-2 opacity-90 group-hover:fill-transparent sm:scale-110"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M8 11v5" /><path d="M8 8v.01" /><path d="M12 16v-5" /><path d="M16 16v-3a2 2 0 1 0 -4 0" /><path d="M3 7a4 4 0 0 1 4 -4h10a4 4 0 0 1 4 4v10a4 4 0 0 1 -4 4h-10a4 4 0 0 1 -4 -4z" /></svg><span class="sr-only">Peter Steinberger on LinkedIn</span> </a><a href="mailto:peter@steipete.me" class="group inline-block hover:text-accent p-2 hover:rotate-6 sm:p-1" title="Send an email to Peter Steinberger"><svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="inline-block size-6 scale-125 fill-transparent stroke-current stroke-2 opacity-90 group-hover:fill-transparent sm:scale-110"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M3 7a2 2 0 0 1 2 -2h14a2 2 0 0 1 2 2v10a2 2 0 0 1 -2 2h-14a2 2 0 0 1 -2 -2v-10z" /><path d="M3 7l9 6l9 -6" /></svg><span class="sr-only">Send an email to Peter Steinberger</span> </a> </div> <div class="my-2 flex flex-col items-center whitespace-nowrap sm:flex-row"> <a href="https://github.com/steipete/steipete.me" target="_blank" rel="noopener noreferrer" class="hover:text-accent">
Steal this post ➜ CC BY 4.0 · Code MIT
</a> </div> </div> </footer>  <!-- Vercel Analytics --><script type="module" src="/_astro/Analytics.astro_astro_type_script_index_0_lang.iMTEyByK.js"></script> <!-- Vercel Speed Insights --><script type="module" src="/_astro/Analytics.astro_astro_type_script_index_1_lang.B9nzKlec.js"></script> </body></html> <script data-astro-rerun>
  /** Create a progress indicator
   *  at the top */
  function createProgressBar() {
    // Create the main container div
    const progressContainer = document.createElement("div");
    progressContainer.className =
      "progress-container fixed top-0 z-10 h-1 w-full bg-background";

    // Create the progress bar div
    const progressBar = document.createElement("div");
    progressBar.className = "progress-bar h-1 w-0 bg-accent";
    progressBar.id = "myBar";

    // Append the progress bar to the progress container
    progressContainer.appendChild(progressBar);

    // Append the progress container to the document body or any other desired parent element
    document.body.appendChild(progressContainer);
  }
  createProgressBar();

  /** Update the progress bar
   *  when user scrolls */
  function updateScrollProgress() {
    document.addEventListener("scroll", () => {
      const winScroll =
        document.body.scrollTop || document.documentElement.scrollTop;
      const height =
        document.documentElement.scrollHeight -
        document.documentElement.clientHeight;
      const scrolled = (winScroll / height) * 100;
      if (document) {
        const myBar = document.getElementById("myBar");
        if (myBar) {
          myBar.style.width = scrolled + "%";
        }
      }
    });
  }
  updateScrollProgress();

  /** Attaches links to headings in the document,
   *  allowing sharing of sections easily */
  function addHeadingLinks() {
    const headings = Array.from(
      document.querySelectorAll("h2, h3, h4, h5, h6"),
    );
    for (const heading of headings) {
      heading.classList.add("group");
      const link = document.createElement("a");
      link.className =
        "heading-link ml-2 opacity-0 group-hover:opacity-100 focus:opacity-100";
      link.href = "#" + heading.id;

      const span = document.createElement("span");
      span.ariaHidden = "true";
      span.innerText = "#";
      link.appendChild(span);
      heading.appendChild(link);
    }
  }
  addHeadingLinks();

  /** Attaches copy buttons to code blocks in the document,
   * allowing users to copy code easily. */
  function attachCopyButtons() {
    const copyButtonLabel = "Copy";
    const codeBlocks = Array.from(document.querySelectorAll("pre"));

    for (const codeBlock of codeBlocks) {
      const wrapper = document.createElement("div");
      wrapper.style.position = "relative";

      const copyButton = document.createElement("button");
      copyButton.className =
        "copy-code absolute right-3 -top-3 rounded bg-muted px-2 py-1 text-xs leading-4 text-foreground font-medium";
      copyButton.innerHTML = copyButtonLabel;
      codeBlock.setAttribute("tabindex", "0");
      codeBlock.appendChild(copyButton);

      // wrap codebock with relative parent element
      codeBlock?.parentNode?.insertBefore(wrapper, codeBlock);
      wrapper.appendChild(codeBlock);

      copyButton.addEventListener("click", async () => {
        await copyCode(codeBlock, copyButton);
      });
    }

    async function copyCode(block, button) {
      const code = block.querySelector("code");
      const text = code?.innerText;

      await navigator.clipboard.writeText(text ?? "");

      // visual feedback that task is completed
      button.innerText = "Copied";

      setTimeout(() => {
        button.innerText = copyButtonLabel;
      }, 700);
    }
  }
  attachCopyButtons();

  /** Scrolls the document to the top when
   * the "Back to Top" button is clicked. */
  function backToTop() {
    document.querySelector("#back-to-top")?.addEventListener("click", () => {
      document.body.scrollTop = 0; // For Safari
      document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
    });
  }
  backToTop();

  /* Go to page start after page swap */
  document.addEventListener("astro:after-swap", () =>
    window.scrollTo({ left: 0, top: 0, behavior: "instant" }),
  );

  /** Add lazy loading to all images in the article */
  function addLazyLoading() {
    const article = document.querySelector("#article");
    if (!article) return;
    
    const images = article.querySelectorAll("img:not([loading])");
    images.forEach((img) => {
      img.setAttribute("loading", "lazy");
    });
  }
  addLazyLoading();

  /** Keyboard navigation for posts */
  function setupKeyboardNavigation() {
    const navContainer = document.querySelector('[data-prev-url]');
    if (!navContainer) return;
    
    const prevUrl = navContainer.getAttribute('data-prev-url');
    const nextUrl = navContainer.getAttribute('data-next-url');
    
    document.addEventListener('keydown', (e) => {
      // Ignore if user is typing in an input, textarea, or contenteditable
      if (e.target.matches('input, textarea, [contenteditable="true"]')) return;
      
      // J key for next post
      if (e.key === 'j' && nextUrl) {
        window.location.href = nextUrl;
      }
      // K key for previous post
      else if (e.key === 'k' && prevUrl) {
        window.location.href = prevUrl;
      }
    });
  }
  setupKeyboardNavigation();

  /** Process YouTube and Twitter embeds */
  function processEmbeds() {
    const article = document.querySelector("#article");
    if (!article) return;

    // Look for YouTube embeds
    const youtubeEmbedRegex = /\{% youtube (https:\/\/[^\s]+|[a-zA-Z0-9_-]+) %\}/g;
    
    let foundYoutubeEmbeds = false;
    
    // Helper to extract a YouTube video ID from various URL formats
    const extractYouTubeId = (input) => {
      let videoId = input;
      try {
        if (videoId.includes("youtube.com/watch")) {
          const url = new URL(videoId);
          videoId = url.searchParams.get("v") || "";
        } else if (videoId.includes("youtu.be/")) {
          videoId = videoId.split("youtu.be/")[1]?.split("?")[0] || "";
        } else if (videoId.includes("youtube.com/embed/")) {
          videoId = videoId.split("youtube.com/embed/")[1]?.split("?")[0] || "";
        }
      } catch (_) {
        // If URL parsing fails, fall back to the raw input
      }
      return videoId;
    };

    // 1) Pre-pass: replace whole-paragraph shortcodes (most common case)
    const pNodes = article.querySelectorAll("p");
    pNodes.forEach((p) => {
      const text = (p.textContent || "").trim();
      const ytMatch = text.match(/\{% youtube (https:\/\/[^\s]+|[a-zA-Z0-9_-]+) %\}/);

      if (ytMatch && ytMatch[1]) {
        foundYoutubeEmbeds = true;
        const videoId = extractYouTubeId(ytMatch[1]);
        const container = document.createElement("div");
        container.className = "youtube-embed-container";
        container.innerHTML = `
          <iframe
            width="560"
            height="315"
            src="https://www.youtube.com/embed/${videoId}"
            title="YouTube video player"
            frameborder="0"
            allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
            allowfullscreen
          ></iframe>
        `;
        p.replaceWith(container);
      }
    });

    // 2) Fallback: process any remaining text nodes that still contain shortcodes
    const walker = document.createTreeWalker(
      article,
      NodeFilter.SHOW_TEXT,
      null,
      false,
    );

    const textNodes = [];
    let node;
    while ((node = walker.nextNode())) {
      textNodes.push(node);
    }

    textNodes.forEach((textNode) => {
      let content = textNode.textContent;
      let hasChanges = false;

      // Process YouTube embeds
      content = content.replace(youtubeEmbedRegex, (match, videoId) => {
        foundYoutubeEmbeds = true;
        hasChanges = true;
        videoId = extractYouTubeId(videoId);

        return `<div class="youtube-embed-container">
            <iframe
              width="560"
              height="315"
              src="https://www.youtube.com/embed/${videoId}"
              title="YouTube video player"
              frameborder="0"
              allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
              allowfullscreen
            ></iframe>
          </div>`;
      });

      if (hasChanges) {
        // Replace the text node with HTML content
        const tempDiv = document.createElement("div");
        tempDiv.innerHTML = content;

        // Replace the text node with the new HTML elements
        const parent = textNode.parentNode;
        while (tempDiv.firstChild) {
          parent.insertBefore(tempDiv.firstChild, textNode);
        }
        parent.removeChild(textNode);
      }
    });
  }

  // Run the embed processing
  processEmbeds();
  
  // Also run after astro navigation
  document.addEventListener("astro:page-load", processEmbeds);
</script> 
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall