2016-11-27 6 views
0

私はUIButton経由でトリガーされるUIAlertViewを持っています。iOS電子メールアプリをUIAlertViewボタンで開く

UIAlertViewには、「電子メールを開く」と「キャンセル」という2つのボタンが表示されます。

"キャンセル"をクリックすると、UIAlertがビューから削除されます。ユーザーが「電子メールを開く」をタップすると、そのデバイスは既定の電子メールアプリケーションの作成画面を開き、電子メールアドレスは「送信者」セクションに既に表示されます。

スウィフト3.

感謝を使用!

私はそれがこのコードで作業してしまった
import UIKit 
import Kingfisher 

class SettingsViewController: UIViewController { 

@IBAction func copyrightInfo(_ sender: Any) { 


    let alert = UIAlertController(title: "Copyright Info", message: "text", preferredStyle: UIAlertControllerStyle.alert) 
    alert.addAction(UIAlertAction(title: "I understand", style: UIAlertActionStyle.default, handler: nil)) 

    self.present(alert, animated: true, completion: nil) 



} 


@IBAction func helpfeedbackAlert(_ sender: Any) { 

    let alertController = UIAlertController(title: "Help & Feedback", message: "text", preferredStyle: .alert) 

    let cancel = UIAlertAction(title: "Cancel", style: .destructive, handler: nil) 

    let openEmail = UIAlertAction(title: "Open Email", style: .default, handler: nil) 


    alertController.addAction(openEmail) 
    alertController.addAction(cancel) 


    self.present(alertController, animated: true, completion: nil) 


} 



@IBAction func clearCache(_ sender: Any) { 


    //  SDImageCache.shared().clearMemory() 
    //  SDImageCache.shared().clearDisk() 

    // Clear memory cache right away. 
    ImageCache.default.clearMemoryCache() 

    // Clear disk cache. This is an async operation. 
    ImageCache.default.clearDiskCache() 


} 

@IBAction func rateApp(_ sender: Any) { 

    if let url = URL(string: "https://www.google.com") { 
     if #available(iOS 10.0, *) { 
      UIApplication.shared.open(url, options: [:]) { 
       boolean in 
       // do something with the boolean 
      } 
     } else { 
      // Fallback on earlier versions 
     } 
    } 

} 

@IBAction func purchasePhotos(_ sender: Any) { 

    if let url = URL(string: "https://google.com") { 
     if #available(iOS 10.0, *) { 
      UIApplication.shared.open(url, options: [:]) { 
       boolean in 
       // do something with the boolean 
      } 
     } else { 
      // Fallback on earlier versions 
     } 
    } 



} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 


} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

override var prefersStatusBarHidden: Bool { 
    get { 
     return true 
    } 
} 


/* 
// MARK: - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) { 
    // Get the new view controller using segue.destinationViewController. 
    // Pass the selected object to the new view controller. 
} 
*/ 

} 

答えて

0

まず、私はあなたがあなたのコードスニペットに追加する必要がないことはこの部分のみであると仮定します、とにかく

@IBAction func helpfeedbackAlert(_ sender: Any) { 

    let alertController = UIAlertController(title: "Help & Feedback", message: "text", preferredStyle: .alert) 

    let cancel = UIAlertAction(title: "Cancel", style: .destructive, handler: nil) 

    let openEmail = UIAlertAction(title: "Open Email", style: .default, handler: nil) 


    alertController.addAction(openEmail) 
    alertController.addAction(cancel) 


    self.present(alertController, animated: true, completion: nil) 
} 

あなたは何が必要なのですかあなたはUIAlertActionのインスタンスを作成するときにhandlerを満たすことです。 init(title:style:handler:)のドキュメントを参照:

ハンドラ

ユーザがアクションを選択したときに実行するブロック。この ブロックは戻り値を持たず、選択されたアクションオブジェクトをそのパラメータの として受け取ります。

だから、あなたのopenEmailは次のようにする必要があります:、

let openEmail = UIAlertAction(title: "Open Email", style: .destructive, handler: { (actionSheetController) in 
      // send your email here... 

     }) 

私はあなたが電子メールを送信したいですかのメカニズムのかなりわからないんだけど、私はあなたがMFMailComposeViewControllerをチェックしたい場合がありますthik this questionはあなたがそれを扱うのに役立ちます。

希望しました。

+0

ありがとう! UIAlertAction内のハンドラにMFMailComposeコードを追加する方法について少し混乱します。これは、UIButtonに接続されているとうまくいきますが、UIAlertActionのハンドラに置かれても機能しないようです。 – Miles

+0

ボタンアクションで何をやっているのとまったく同じですが、クロージャー(ここでは「あなたのメールをここに送る...」というブロックと同じブロックにあります)。 –

+0

今すぐご利用ください!ありがとうございました。私は「自己」を加えなければならなかった。 (メール、アニメーション:真)の行に表示されます。電子メール画面が開き、「キャンセル」または電子メールを送信すると、メールビューは消えません。 – Miles

0

@IBAction func helpfeedbackAlert(_ sender: Any) { 

    let alertController = UIAlertController(title: "Help & Feedback", message: "text", preferredStyle: .alert) 

    let cancel = UIAlertAction(title: "Cancel", style: .destructive, handler: nil) 

    let openEmail = UIAlertAction(title: "Open Email", style: .default, handler: { (actionSheetController) -> Void in let email = "[email protected]" 
     let url = NSURL(string: "mailto:\("[email protected]")") 
     UIApplication.shared.openURL(url as! URL)}) 


    alertController.addAction(openEmail) 
    alertController.addAction(cancel) 


    self.present(alertController, animated: true, completion: nil) 



} 
+0

あなたは答えをすでに見つかっているが、あなたは、標準のiOSメールアプリ以外のメールクライアントを含めるように機能を拡張したい場合、私はここにこれを行うために正確にどのように詳細にブログ記事を書いているように見えます。https: //medium.com/@harrybloom18/ios-email-picker-99045d940a3d –

+0

@ beehive-bedlamありがとうございました! – Miles

2

あなたは電子メールを送信するためにMFMailComposerを使用する必要があり、これはあなたがそれを使用する方法

if MFMailComposeViewController.canSendMail() { 
     let mail = MFMailComposeViewController() 
     mail.mailComposeDelegate = self 
     mail.setToRecipients(["[email protected]"]) 
     mail.setMessageBody("you email body", isHTML: false) 
     present(mail, animated: true) 
    } else { 
     // email is not added in app 
    } 

でもimport MessageUIフレームワークとに準拠MFMailComposeViewControllerDelegate

また、あなたのアプリから別のアプリにリダイレクトしているときにあなたの答えに記載されているようにopenURLを使用すると、MFMailComposerを使用する必要はありません。

+0

私はこれを試してみましょう!ありがとう。 MFMailComposeViewControllerDelegateに準拠させるにはどうすればよいですか? – Miles

+0

クラスのViewController:ViewControllerを、MFMailComposeViewControllerDelegate { – Rajat

+0

感謝。これはUIButtonでうまく動作しますが、これをUIAlertView内のボタンにどのようにアタッチするかを調べようとしています。 (UIAlertAction)。 – Miles

関連する問題