2017-01-18 12 views
2

プログラムで作成する必要があるリンクされたテキストビューがあります。SwiftでUITextViewをiOS 10とiOS 9の両方で開くリンク

現在、リンクはiOS 10ではクリック可能ですが、iOS 9.0ではクリックできません。

これは、これまでのコードです:

import UIKit 

class ViewController: UIViewController, UITextViewDelegate { 
    // Empty view container 
    @IBOutlet weak var innerView: UIView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Create text view 
     let textView = UITextView(frame: CGRect.zero) 

     // Set the link attribute 
     let linkAttributes = [ 
      NSLinkAttributeName: NSURL(string: "https://www.apple.com")!, 
      NSForegroundColorAttributeName: UIColor.blue 
      ] as [String : Any] 

     let attributedString = NSMutableAttributedString(string: "Just click here to register") 
     attributedString.setAttributes(linkAttributes, range: NSMakeRange(5, 10)) 

     textView.delegate = self 
     textView.attributedText = attributedString 
     textView.isEditable = false 
     textView.isSelectable = true 
     textView.dataDetectorTypes = .link 

     // Add to view 
     textView.sizeToFit() 
     innerView.addSubview(textView) 
    } 

    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool { 
     if #available(iOS 10.0, *) { 
      UIApplication.shared.open(URL, options: [:]) 
     } else { 
      // Fallback on earlier versions 
      UIApplication.shared.openURL(URL) 
     } 
     return true 
    } 
} 

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {内部のブレークポイントの設定では、すべてのiOS 9.ここ

ためと呼ばれていないアプリが

enter image description here

次のようになります。

ストーリーボードは、ビューが内部にあるデフォルトのシングルビューアプリケーションテンプレートです。

enter image description here

+0

のTextViewの選択可能なプロパティがはい??ある対のリンクをアクティブにするには、シミュレータでより深いクリックを必要としています。 –

+0

@KumarKLはそれです更新コード – user2560886

+0

@ user2560886あなたのコードはiOS 9と10の両方で動作します –

答えて

0

は私が間違ってリンクをクリックして判明します。 iOSの9オン

あなたは、iOS 10.

関連する問題