简体   繁体   中英

How do I weak link an iOS app delegate method?

I want to add support for the new 3D Touch Quick Actions introduced in iOS 9 to my iOS 8 app. I know that I need to use weakly linked methods, but the problem is that I don't know the correct syntax for doing this for app delegate methods.

To handle Quick Actions my app delegate needs to implement the new application:performActionForShortcutItem:completionHandler: method, but what's the syntax for doing this in a weakly linked way that won't crash on iOS 8? I got this far:

// iOS 9 only...
if ([UIApplicationDelegate instancesRespondToSelector:
     @selector(application:performActionForShortcutItem:completionHandler:)]) {

    // How do I access the method's shortcutItem and completionHandler arguments from here?
}

As you can see I'm using Objective-C rather than Swift.

Individual methods like this aren't really subject to weak linking; thanks to ObjC's runtime dispatch of methods, respondsToSelector: (or instancesRespondToSelector: ) is the right way to test that you can send a message before sending it. Weak linking only applies to entire classes (and C functions).

In this case, though, and for any method like it that's called by the framework, it simply won't be called on anything earlier than iOS 9. If you implement the method, your code will never run, so it will never cause a problem. You could equally well implement - (void)applicationWillDrunkDialYourEx:(UIApplication *)application in your app delegate without any ramifications other than a slight increase in binary size.

So you can just go ahead and implement it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM