문제

Is it possible to perform custom action when user touch autodetected phone link in UITextView. Please do not advice to use UIWebView instead.

And please don't just repeat text from apple classes reference - certainly I've already read it.

Thanks.

도움이 되었습니까?

해결책

Update: From ,

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

From and Later UITextView has the delegate method:

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

to intercept the clicks to links. And this is the best way to do it.

For and earlier a nice way to do this is to by subclassing UIApplication and overwriting the -(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

You will need to implement openURL: in your delegate.

Now, to have the application start with your new subclass of UIApplication, locate the file main.m in your project. In this small file that bootstraps your app, there is usually this line:

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

The third parameter is the class name for your application. So, replacing this line for:

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

This did the trick for me.

다른 팁

In iOS 7 or Later

You can use the following UITextView delegate Method:

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

The text view calls this method if the user taps or long-presses the URL link. Implementation of this method is optional. By default, the text view opens the application responsible for handling the URL type and passes it the URL. You can use this method to trigger an alternative action, such as displaying the web content at the URL in a web view within the current application.

Important:

Links in text views are interactive only if the text view is selectable but noneditable. That is, if the value of the UITextView the selectable property is YES and the isEditable property is NO.

For Swift 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
    }
}

or if target is >= IOS 10

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

Wiki 페이지 콘텐츠에 JavaScript를 추가하면 CEWP에 추가하는 것과 같습니다.최종 결과는 SharePoint가 코드를 제거하고 JavaScript로 하이퍼 링크를 쓸모없는 태그로 변환 할 것입니다.

방해가 가장 좋은 해결책은 페이지에 CEWP *를 추가하고 외부 JavaScript 파일을 링크하는 것입니다 (사이트의 Doc 라이브러리에 업로드 한 것) 콘텐츠링크 상자.

[*] CEWP를 사용하는 일반적으로 제안 된 해결 방법의 대안으로 HTML 양식 웹 파트를 사용하여 JavaScript 코드를 넣을 수있게 해줍니다.코드가 제거됩니다.

With Swift 5 and iOS 12, you can use one of the three following patterns in order to interact with links in a UITextView.


#1. Using UITextView's dataDetectorTypes property.

The simplest way to interact with phone numbers, urls or addresses in a UITextView is to use dataDetectorTypes property. The sample code below shows how to implement it. With this code, when the user taps on the phone number, a UIAlertController pops up.

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. Using UITextViewDelegate's textView(_:shouldInteractWith:in:interaction:) method

If you want to perform some custom action instead of making a UIAlertController pop up when you tap on a phone number while using dataDetectorTypes, you have to make your UIViewController conform to UITextViewDelegate protocol and implement textView(_:shouldInteractWith:in:interaction:). The code below shows how to implement it:

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. Using NSAttributedString and NSAttributedString.Key.link

As an alternative, you can use NSAttributedString and set a URL for its NSAttributedString.Key.link attribute.The sample code below shows a possible implementation of it. With this code, when user taps on the attributed string, a UIAlertController pops up.

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
    }

}

코드로 이것을 해결할 수는 있지만 상자 솔루션을 원한다고 가정합니다.타협해야 할 것입니다 ...

귀하의 접근 방식에 대한 문제는 이벤트가 가입 양식에있는 이벤트가 열려 있는지 여부를 확인하는 것입니다.다른 접근 방식은 다음과 같을 수 있습니다 :

이벤트 목록에는 이벤트 목록이 포함되어 있습니다

각 이벤트에는 등록 된 사용자가 등록 된 사용자를 보유한 자체 관련 목록이 있습니다

이벤트 목록에는 수식이 포함 된 계산 된 열이 있습니다.수식은 상태 필드를보고 상태가 열려있는 경우 계산 된 열에 URL이 반환됩니다.상태가 닫히면 수식은 아무 것도 반환되지 않거나 등록이 닫히는 메시지가 표시됩니다.

이 작업을 수행하려면 수식이 유효한 URL을 일관되게 생성하려면 일관된 이름 지정 규칙이 필요합니다.

tal

에릭이 말했듯이, 직접 데이터베이스를 쿼리해서는 안됩니다.PowerShell을 사용하여 CanReceiveEmailEmailAliasSPList 속성을 확인하여 목록 위치를 정확하게 찾을 것을 권장합니다.아래 스크립트를 참조하십시오 :

$SPsite = Get-SPSite "http://mysitecollection"
foreach($SPweb in $SPsite.AllWebs)  # get the collection of sub sites
{
    foreach ($SPList list in $SPweb.Lists)
    {
        if ( ($splist.CanReceiveEmail) -and ($SPlist.EmailAlias -eq "yourEmailAliasForList") )
        {
            WRITE-HOST "E-Mail: " $SPList.EmailAlias + “, List: ” + $SPlist.Title +”, Web: ” + $SPweb.Url
        }
    }
}
.

application:handleOpenURL: is called when another app opens your app by opening a URL with a scheme your app supports. It's not called when your app begins opening a URL.

I think the only way to do what Vladimir wants is to use a UIWebView instead of a UITextView. Make your view controller implement UIWebViewDelegate, set the UIWebView's delegate to the view controller, and in the view controller implement webView:shouldStartLoadWithRequest:navigationType: to open [request URL] in a view instead of quitting your app and opening it in Mobile Safari.

Swift 4:

1) Create the following class (subclassed 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) Wherever you set up your textview, do this:

//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")
    }
}


If you're using storyboard, make sure your textview looks like this in the right pane identity inspector:
enter image description here



Voila! Now you get the link tap immediately instead of when the URL shouldInteractWith URL method

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top