如何动态更改我的应用程序中的openURL实现? [英] How to dynamically change the openURL implementation in my app?

查看:60
本文介绍了如何动态更改我的应用程序中的openURL实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我已经在我的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实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆