在我的应用程序中,我已经在我的AppDelegate中实现了一个私有方法以覆盖默认的openURL:方法,以便在UIWebView中打开我的应用程序内部的链接.但是现在我也需要使用默认功能.
In my app, I have implemented a private method in my AppDelegate to override the default openURL: method in order to open links inside my app within UIWebView. But now I need the default functionalities in place too.
这就是我所做的:
@implementation UIApplication (Private) - (BOOL)customOpenURL:(NSURL*)url { AppDelegate *MyWatcher = (AppDelegate *)[[UIApplication sharedApplication] delegate]; if (MyWatcher.currentViewController) { [MyWatcher.currentViewController handleURL:url]; return YES; } return NO; } @end - (void)applicationDidBecomeActive:(UIApplication *)application { Method customOpenUrl = class_getInstanceMethod([UIApplication class], @selector(customOpenURL:)); Method openUrl = class_getInstanceMethod([UIApplication class], @selector(openURL:)); method_exchangeImplementations(openUrl, customOpenUrl); }我还在需要自定义打开URL处理的类中实现了handleURL:.但是,这妨碍了我的其他课程,在该课程中,我只想简单地在iTunes中打开iTunes链接.所以我不知道如何实现是如何使用原始的openURL:代替customOpenURL:.
I also implemented handleURL: in my class where the custom open URL handling was needed. However, this is hindering my other class in which I just want to do a simple open of an iTunes link in iTunes. So what I don't know how to achieve is how to use the original openURL: in place of customOpenURL:.
推荐答案您可以仅继承UIApplication的子类并直接覆盖openURL:.确保更改Info.plist中的Principle类以使用UIApplication子类.
You can just subclass UIApplication and override openURL: directly. Be sure to change the principle class in your Info.plist to use your UIApplication subclass.
示例:
@interface ECApplication : UIApplication @end @implementation ECApplication - (BOOL)openURL:(NSURL*)url { AppDelegate *MyWatcher = (AppDelegate *)[[UIApplication sharedApplication] delegate]; if (MyWatcher.currentViewController) { [MyWatcher.currentViewController handleURL:url]; return YES; } return NO; } @end然后,在您的Info.plist文件中,查找Principle Class类键,并将其值更改为ECApplication(或您命名为子类的任何名称).
Then, in your Info.plist file, look for the Principle Class key, and change the value to ECApplication (or whatever you name your subclass).
更多推荐
如何动态更改我的应用程序中的openURL实现?
发布评论