gpt4 book ai didi

iphone - 如何拦截点击 UITextView 中的链接?

转载 作者:行者123 更新时间:2023-11-28 08:06:35 24 4
gpt4 key购买 nike

当用户触摸 UITextView 中自动检测到的电话链接时,是否可以执行自定义操作。请不要建议改用 UIWebView。

并且请不要只是重复 apple classes 引用中的文本 - 当然我已经读过了。

谢谢。

最佳答案

更新:来自 ,

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction;

来自 后来 UITextView 有委托(delegate)方法:

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange *NS_DEPRECATED_IOS(7_0, 10_0, "Use textView:shouldInteractWithURL:inRange:forInteractionType: instead");*

拦截对链接的点击。这是最好的方法。

对于 之前一个很好的方法是通过子类化 UIApplication 并覆盖 -(BOOL)openURL:(NSURL *)url

@interface MyApplication : UIApplication {

}

@end

@implementation MyApplication


-(BOOL)openURL:(NSURL *)url{
if ([self.delegate openURL:url])
return YES;
else
return [super openURL:url];
}
@end

您需要在您的委托(delegate)中实现openURL:

现在,要让应用程序以 UIApplication 的新子类开始,请在您的项目中找到文件 main.m。在这个引导您的应用程序的小文件中,通常有这一行:

int retVal = UIApplicationMain(argc, argv, nil, nil);

第三个参数是您的应用程序的类名。因此,将此行替换为:

int retVal = UIApplicationMain(argc, argv, @"MyApplication", nil);

这对我有用。

关于iphone - 如何拦截点击 UITextView 中的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44913728/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com