如何动态更改我的应用程序中的openURL实现?

编程入门 行业动态 更新时间:2024-10-26 16:25:52
本文介绍了如何动态更改我的应用程序中的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实现?

本文发布于:2023-11-12 03:31:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580386.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   动态   openURL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!