时,能够进行自定义动作时用户触摸在自动检测的UITextView电话链路。请不要建议使用的UIWebView来代替。

和请你从苹果类参考不仅仅是重复文本   - 当然我已经阅读

感谢。

有帮助吗?

解决方案

更新:从

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

和后来UITextView具有代表方法:

- (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

您将需要实现你的委托openURL:

现在,让应用程序开始你的新UIApplication的子类,在您的项目文件的main.m。在这种小文件,自举应用程式中,通常是这样的线:

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

第三个参数是为应用程序的类名。所以,更换此线为:

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

这奏效了我。

其他提示

在iOS 7以上版本

可以使用下面的UITextView委托方法:

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

在文本视图调用此方法,如果用户的抽头或长按压URL链接。这种方法的实现是可选的。默认情况下,文本视图中打开负责处理该URL类型的应用程序,并通过它的URL。可以使用此方法来触发的替代作用,如在当前的应用程序内的web视图显示在该URL的网页内容。

重要:

  

在文本视图链接是只有当文本视图是交互式   可选,但是不可编辑。也就是说,如果UITextView的价值   可选择的属性为是,并且isEditable属性是NO。

有关夫特3

textView.delegate = self

extension MyTextView: UITextViewDelegate 

    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {

        GCITracking.sharedInstance.track(externalLink: URL)
        return true
    }
}

,或者如果目标是> = 10 IOS

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool

夫特版本:

您的标准UITextView的设置应该是这个样子,不要忘了代表和dataDetectorTypes。

var textView = UITextView(x: 10, y: 10, width: CardWidth - 20, height: placeholderHeight) //This is my custom initializer
textView.text = "dsfadsaf www.google.com"
textView.selectable = true
textView.dataDetectorTypes = UIDataDetectorTypes.Link
textView.delegate = self
addSubview(textView)

在你的类的端部添加此片:

class myVC: UIViewController {
    //viewdidload and other stuff here
}

extension MainCard: UITextViewDelegate {
    func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
        //Do your stuff over here
        var webViewController = SVModalWebViewController(URL: URL)
        view.presentViewController(webViewController, animated: true, completion: nil)
        return false
    }
}

使用夫特5和iOS 12,可以使用以下三种模式中的一个,以便与交互在UITextView链接。


#1。使用UITextView dataDetectorTypes 属性。

在一个UITextView与电话号码,URL或地址进行交互的最简单的方法是使用dataDetectorTypes属性。下面给出了示例代码如何实现它。与此代码,当在电话号码用户抽头,一个UIAlertController弹出。

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let textView = UITextView()
        textView.text = "Phone number: +33687654321"
        textView.isUserInteractionEnabled = true
        textView.isEditable = false
        textView.isSelectable = true
        textView.dataDetectorTypes = [.phoneNumber]
        textView.isScrollEnabled = false

        textView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(textView)
        textView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        textView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        textView.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor).isActive = true
    }

}

#2。使用UITextViewDelegate textView(_:shouldInteractWith:in:interaction:) 方法

如果您要改为执行制作,当你的电话号码挖掘,同时使用UIAlertController一个dataDetectorTypes弹出的一些自定义操作,你必须让你的UIViewController符合UITextViewDelegate协议和实施textView(_:shouldInteractWith:in:interaction:)。下面示出了代码如何实现它:

import UIKit

class ViewController: UIViewController, UITextViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let textView = UITextView()
        textView.delegate = self
        textView.text = "Phone number: +33687654321"
        textView.isUserInteractionEnabled = true
        textView.isEditable = false
        textView.isSelectable = true
        textView.dataDetectorTypes = [.phoneNumber]
        textView.isScrollEnabled = false

        textView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(textView)
        textView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        textView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        textView.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor).isActive = true
    }

    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        /* perform your own custom actions here */
        print(URL) // prints: "tel:+33687654321"

        return false // return true if you also want UIAlertController to pop up
    }

}

#3。使用 NSAttributedString NSAttributedString.Key.link

作为替代方案,可以使用NSAttributedString并设置其URL attribute.The示例代码示出了下面的可能实施它的一个NSAttributedString.Key.link。与此代码,当在归属用户串抽头,一个UIAlertController弹出。

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let attributedString = NSMutableAttributedString(string: "Contact: ")
        let phoneUrl = NSURL(string: "tel:+33687654321")! // "telprompt://+33687654321" also works
        let attributes = [NSAttributedString.Key.link: phoneUrl]
        let phoneAttributedString = NSAttributedString(string: "phone number", attributes: attributes)
        attributedString.append(phoneAttributedString)

        let textView = UITextView()
        textView.attributedText = attributedString
        textView.isUserInteractionEnabled = true
        textView.isEditable = false
        textView.isSelectable = true
        textView.isScrollEnabled = false

        textView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(textView)
        textView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        textView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        textView.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor).isActive = true
    }

}

我还没有试过,我自己,但你可以尝试实施application:handleOpenURL:方法在应用程序中的委托 - 它看起来像所有openURL请求穿过这个回调

不知道你将如何拦截检测到的数据链路,或者你需要运行哪些类型的功能。但是,你可能能够利用didBeginEditing文本字段的方法来运行测试/通过文本框的扫描,如果你知道你想找for..such作为比较的文本字符串满足### - ### - ####格式,或开头的“www。”抓住这些领域,但你需要通过文本框串写一些代码来嗅,reconize你需要什么,然后提取它的功能的使用。我不认为这将是困难的,一旦你缩小它到底是什么,你想,然后集中你的if()语句过滤下来的,你需要什么样的非常具体的匹配模式。

病程由此意味着该用户要触摸该文本框在以激活didBeginEditing()。如果不是用户交互的类型,你正在寻找你可以只使用一个触发定时器,即根据需要和贯穿文本框串上ViewDidAppear()或其他开始,然后在最后的您通过文本框串运行方法是你建,你只是把定时器后退。

另一个应用程序打开时application:handleOpenURL:叫的的用一个方案您的应用支持打开一个URL的应用程序。当你的应用程序开始打开URL不调用它。

我觉得做弗拉基米尔想要什么的唯一方式是使用一个UIWebView而不是一个UITextView的。让您的视图控制器实现UIWebViewDelegate,在一个UIWebView的委托设置为视图控制器,并在视图控制器实现视图,而不是退出你的应用程序,并在移动Safari浏览器中打开它webView:shouldStartLoadWithRequest:navigationType:到开放[request URL]

夫特4:

1)创建以下类(子类的UITextView):

import Foundation

protocol QuickDetectLinkTextViewDelegate: class {
    func tappedLink()
}

class QuickDetectLinkTextView: UITextView {

    var linkDetectDelegate: QuickDetectLinkTextViewDelegate?

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)

    }

    required init?(coder aDecoder: NSCoder) {
         super.init(coder: aDecoder)
    }

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let glyphIndex: Int? = layoutManager.glyphIndex(for: point, in: textContainer, fractionOfDistanceThroughGlyph: nil)
        let index: Int? = layoutManager.characterIndexForGlyph(at: glyphIndex ?? 0)
        if let characterIndex = index {
            if characterIndex < textStorage.length {
                if textStorage.attribute(NSLinkAttributeName, at: characterIndex, effectiveRange: nil) != nil {
                    linkDetectDelegate?.tappedLink()
                    return self
                }
            }
        }

        return nil
    }
}

,点击 2)无论你设置你的TextView,这样做:点击

//init, viewDidLoad, etc
textView.linkDetectDelegate = self

//outlet
@IBOutlet weak var textView: QuickDetectLinkTextView!

//change ClassName to your class
extension ClassName: QuickDetectLinkTextViewDelegate {
    func tappedLink() {
        print("Tapped link, do something")
    }
}

,点击 如果您使用的故事板,确保TextView看起来像这样在右窗格中的身份检查:结果 “在这里输入的图像描述”

搜索结果 瞧!现在您可以链接水龙头立即而不是当URL shouldInteractWith URL方法

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top