-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
imUrlDataApp - Refactor Message Sending from App for easy reuse in ot…
…her sample ViewController.swift - var messager added - displayMessageInterface and messageComposeViewController moved to MessageComposerInApp - viewDidLoad use messager to set state if !canSendText - onAppSendButton call messager to display
- Loading branch information
1 parent
e740382
commit 7db1b77
Showing
4 changed files
with
87 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// MessageComposerInApp.swift | ||
// imUrlDataApp | ||
// | ||
// Created by AndyDent on 5/8/2022. | ||
// Copyright © 2022 Touchgram Pty Ltd. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import MessageUI | ||
import Messages | ||
|
||
class MessageComposingHelper: NSObject, MFMessageComposeViewControllerDelegate { | ||
func canSendText() -> Bool { | ||
MFMessageComposeViewController.canSendText() | ||
} | ||
|
||
func displayMessageInterface(onVC vc: UIViewController) { | ||
let composeVC = MFMessageComposeViewController() | ||
composeVC.messageComposeDelegate = self | ||
|
||
// Configure the fields of the interface. | ||
composeVC.recipients = ["123456"] | ||
composeVC.body = "Sending a custom message" | ||
// | ||
/* | ||
According to | ||
https://developer.apple.com/documentation/messageui/mfmessagecomposeviewcontroller/2213331-message | ||
If your app has an iMessage app extension, you can display your iMessage app within the message compose view, just as you would in the Messages app. To display your iMessage app, create and assign an MSMessage object to this property. | ||
|
||
By default, this property is set to nil. | ||
*/ | ||
if #available(iOS 10, *) { // necessary if clause to make XCode happy to use composeVC.message | ||
let message = MSMessage(session: MSSession()) | ||
// fake building a smiley using hardcoded stuff to match MessagesViewController.send | ||
guard var urlComps = URLComponents(string:"data:,") else { | ||
fatalError("Invalid base URL") | ||
} | ||
urlComps.queryItems = [URLQueryItem(name:"mood", value:"happy")] | ||
message.url = urlComps.url | ||
composeVC.message = message | ||
} | ||
|
||
// Present the view controller modally. | ||
if MFMessageComposeViewController.canSendText() { | ||
vc.present(composeVC, animated: true, completion: nil) | ||
} else { | ||
print("Can't send messages.") | ||
} | ||
} | ||
|
||
//MARK - conform to MFMessageComposeViewControllerDelegate | ||
func messageComposeViewController(_ controller: MFMessageComposeViewController, | ||
didFinishWith result: MessageComposeResult) { | ||
// Check the result or perform other tasks. | ||
let msgStr = result == .cancelled ? | ||
"Cancelled" : result == .failed ? | ||
"Failed" : | ||
"Sent" | ||
print(msgStr) | ||
// Dismiss the message compose view controller. | ||
controller.dismiss(animated: true, completion: nil) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters