質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

577閲覧

Swift メール送信画面が開いてすぐに閉じてしまう

yuki84

総合スコア23

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2019/08/10 14:16

開発環境 Xcode10.3 / Swift5/ iOS12.3

カスタムキーボード上で、ボタンタップ時の座標を取得し、配列に格納し、CSVに変換してメールで送信するコードを以下のように実装しました。しかし、送信ボタンを押してもメール画面が現れてすぐに閉じてしまいます。
その際、以下のようにログにエラーが出ていますが、調べても対処法がわかりません。
1ヶ月ほど前に同様の方法で実装した時はちゃんと送信できていたのですが。。。
解決策をご教授いただけないでしょうか。よろしくお願いします。

Swift

1import UIKit 2import MessageUI 3 4class KeyboardViewController: UIInputViewController,MFMailComposeViewControllerDelegate { 5 6 @IBOutlet var nextKeyboardButton: UIButton! 7 8 let button1 = UIButton() 9 let button2 = UIButton() 10 var csvData = [[String]]() 11 let label = UILabel() 12 13 override func updateViewConstraints() { 14 super.updateViewConstraints() 15 16 // Add custom view sizing constraints here 17 } 18 19 20 // キーボードのサイズを変更するコード 21 override func viewWillAppear(_ animated: Bool) { 22 super.viewDidAppear(animated) 23 24 //constantで高さ変更 25 let heightConstraint = NSLayoutConstraint(item: view, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 500) 26 27 self.view.addConstraint(heightConstraint) 28 29 } 30 31 32 override func viewDidLoad() { 33 super.viewDidLoad() 34 35 // Perform custom UI setup here 36 self.nextKeyboardButton = UIButton(type: .system) 37 self.nextKeyboardButton.setTitle(NSLocalizedString("Next Keyboard", comment: "Title for 'Next Keyboard' button"), for: []) 38 self.nextKeyboardButton.sizeToFit() 39 self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false 40 self.nextKeyboardButton.addTarget(self, action: #selector(handleInputModeList(from:with:)), for: .allTouchEvents) 41 self.view.addSubview(self.nextKeyboardButton) 42 43 self.nextKeyboardButton.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true 44 self.nextKeyboardButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true 45 46 47 // ボタンの設置座標とサイズを設定する. 48 button1.frame = CGRect(x: 100, y: 0, width: 200, height: 100) 49 button1.backgroundColor = UIColor.cyan 50 button1.setTitle("ボタン", for: .normal) 51 button1.addTarget(self, action: #selector(KeyboardViewController.onMyButton(_:forEvent:)), for: .touchDown) 52 view.addSubview(button1) 53 54 // ボタンの設置座標とサイズを設定する. 55 button2.frame = CGRect(x: 100, y: 130, width: 200, height: 100) 56 button2.backgroundColor = UIColor.cyan 57 button2.setTitle("送信", for: .normal) 58 button2.addTarget(self, action: #selector(KeyboardViewController.onMyButton2(_:forEvent:)), for: .touchDown) 59 view.addSubview(button2) 60 61 csvData = ([["x座標","y座標"]]) 62 63 } 64 65 @objc func onMyButton(_ sender: UIButton, forEvent event: UIEvent) { 66 67 let locationX = event.touches(for: sender)!.first!.location(in: view).x 68 let locationY = event.touches(for: sender)!.first!.location(in: view).y 69 70 71 print(Float(locationX),Float(locationY)) 72 csvData.append([String(Float(locationX)),String(Float(locationY))]) 73 74 print(csvData) 75 76 } 77 78 //CSVへの変換 79 func toCSV(input: [[String]]) -> String { 80 return input.map { 81 $0.map { 82 $0.contains(",") || $0.contains("\"") 83 ? "\"" + $0.replacingOccurrences(of: "\"", with: "\"\"") + "\"" 84 : $0 85 }.joined(separator: ",") 86 }.joined(separator: "\r\n") 87 } 88 89 90 @objc func onMyButton2(_ sender: UIButton, forEvent event: UIEvent) { 91 92 //メールを送信できるかチェック 93 if MFMailComposeViewController.canSendMail()==false { 94 print("Email Send Failed") 95 return 96 } 97 sendMailWithCSV("実験データ", message: "", csv1: csvData) 98 99 } 100 101 102 //メールでCSVを送信する 103 func sendMailWithCSV(_ subject: String, message: String, csv1: [[String]]) { 104 105 let mailViewController = MFMailComposeViewController() 106 mailViewController.mailComposeDelegate = self 107 let toRecipients = ["******@gmail.com"] //送信先アドレスの入力 108 109 mailViewController.setSubject(subject) 110 mailViewController.setToRecipients(toRecipients) 111 mailViewController.setMessageBody(message, isHTML: false) 112 //csvを添付、ファイル名の指定 113 mailViewController.addAttachmentData(toCSV(input: csv1).data(using: String.Encoding.shiftJIS, allowLossyConversion: false)!,mimeType: "csv", fileName: "keyboardTapData.csv") 114 115 116 self.present(mailViewController, animated: true) {} 117 } 118 119 //メールの送信確認 120 func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { 121 switch result { 122 case .cancelled: 123 print("キャンセル") 124 case .saved: 125 print("下書き保存") 126 case .sent: 127 print("送信成功") 128 default: 129 print("送信失敗") 130 } 131 dismiss(animated: true, completion: nil) 132 } 133 134 135 136 137 override func viewWillLayoutSubviews() { 138 self.nextKeyboardButton.isHidden = !self.needsInputModeSwitchKey 139 super.viewWillLayoutSubviews() 140 } 141 142 override func textWillChange(_ textInput: UITextInput?) { 143 // The app is about to change the document's contents. Perform any preparation here. 144 } 145 146 override func textDidChange(_ textInput: UITextInput?) { 147 // The app has just changed the document's contents, the document context has been updated. 148 149 var textColor: UIColor 150 let proxy = self.textDocumentProxy 151 if proxy.keyboardAppearance == UIKeyboardAppearance.dark { 152 textColor = UIColor.white 153 } else { 154 textColor = UIColor.black 155 } 156 self.nextKeyboardButton.setTitleColor(textColor, for: []) 157 } 158 159} 160
2019-08-10 23:10:05.488301+0900 timer kakunin[1937:23395] [General] #CompositionServices _serviceViewControllerReady: NSError Domain=NSCocoaErrorDomain Code=4099 キャンセル

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

カスタムキーボードのフルアクセスを許可したところできるようになりました。

投稿2019/08/13 15:09

yuki84

総合スコア23

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問