えむじぃのアプリ開発

えむじぃのアプリ開発

元大手IT企業SE、現ベンチャー企業CTOのブログです。

【Swift】Twitterのツイート機能を実装する方法

今回はTwitterのツイート機能を実装する方法をこの記事で説明します。

この記事のポイント・Socialを使用

Twitterのツイート機能を実装する方法

ツイート機能を実装するには以下のように設定します。

import Social

@IBAction func btnTweet(_ sender: Any) {

    // ダイアログを表示する
    var text = "おはようございます〜"
    let encodedTxt = text.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
    
    if let encodedText = encodedText,
        let url = URL(string: "https://twitter.com/intent/tweet?text=\(encodedTxt)") {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }
    
}

これでTwitterのツイート機能が表示されます。