Skip to content

Commit

Permalink
imUrlDataApp - Refactor Message Sending from App for easy reuse in ot…
Browse files Browse the repository at this point in the history
…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
AndyDentFree committed Aug 6, 2022
1 parent e740382 commit 7db1b77
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 53 deletions.
16 changes: 15 additions & 1 deletion imUrlDataApp/imUrlDataApp Code Change Diary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,18 @@ ViewController.swift
- add protocol MFMessageComposeViewControllerDelegate
- appSendButton added and connected
- onAppSendButton added to invoke displayMessageInterface
- displayMessageInterface added
- displayMessageInterface added


-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Refactor Message Sending from App for easy reuse in other sample
2022-08-05

ViewController.swift
- var messager added
- displayMessageInterface and messageComposeViewController moved to MessageComposerInApp
- viewDidLoad use messager to set state if !canSendText
- onAppSendButton call messager to display

MessageComposerInApp.swift
- added as NSObject, MFMessageComposeViewControllerDelegate
4 changes: 4 additions & 0 deletions imUrlDataApp/imUrlDataApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
42199E9C21F1CA5700D4377E /* imUrlDataApp IM.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = 42199E8D21F1CA5600D4377E /* imUrlDataApp IM.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
42199EA521F22CA400D4377E /* SharedData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42199EA421F22CA400D4377E /* SharedData.swift */; };
42199EA621F22CA400D4377E /* SharedData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42199EA421F22CA400D4377E /* SharedData.swift */; };
4276811F289D2D8B008FDF6C /* MessageComposerInApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4276811E289D2D8B008FDF6C /* MessageComposerInApp.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -62,6 +63,7 @@
42199EA121F22A1300D4377E /* imUrlDataApp.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = imUrlDataApp.entitlements; sourceTree = "<group>"; };
42199EA221F22A1700D4377E /* imUrlDataApp IM.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "imUrlDataApp IM.entitlements"; sourceTree = "<group>"; };
42199EA421F22CA400D4377E /* SharedData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SharedData.swift; sourceTree = "<group>"; };
4276811E289D2D8B008FDF6C /* MessageComposerInApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageComposerInApp.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -113,6 +115,7 @@
4201A9D621F0763C0043E369 /* Assets.xcassets */,
4201A9D821F0763C0043E369 /* LaunchScreen.storyboard */,
4201A9DB21F0763C0043E369 /* Info.plist */,
4276811E289D2D8B008FDF6C /* MessageComposerInApp.swift */,
);
path = imUrlDataApp;
sourceTree = "<group>";
Expand Down Expand Up @@ -260,6 +263,7 @@
files = (
42199EA521F22CA400D4377E /* SharedData.swift in Sources */,
4201A9D221F0763B0043E369 /* ViewController.swift in Sources */,
4276811F289D2D8B008FDF6C /* MessageComposerInApp.swift in Sources */,
4201A9D021F0763B0043E369 /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
64 changes: 64 additions & 0 deletions imUrlDataApp/imUrlDataApp/MessageComposerInApp.swift
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)
}
}
56 changes: 4 additions & 52 deletions imUrlDataApp/imUrlDataApp/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
//

import UIKit
import MessageUI
import Messages

extension UISwitch {
func toggle() {
self.setOn(!self.isOn, animated: true)
}
}

class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {
class ViewController: UIViewController {

@IBOutlet fileprivate weak var happyBtn: UIButton!
@IBOutlet fileprivate weak var quizzicalBtn: UIButton!
Expand All @@ -31,6 +29,7 @@ class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {
lazy var toggles = [happySwitch, quizzicalSwitch, distraughtSwitch, angrySwitch]
lazy var buttons = [happyBtn, quizzicalBtn, distraughtBtn, angryBtn]
var lastToggled : controlIndexes? = nil
var messager = MessageComposingHelper()

// array of flags instead of storing state in the switch so can easily save and load
var enabled = [Bool]()
Expand All @@ -48,7 +47,7 @@ class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {
lastToggled = isOn ? lastToggled : controlIndexes(rawValue: i)
}
}
if !MFMessageComposeViewController.canSendText() {
if !messager.canSendText() {
appSendButton.isEnabled = false
appSendButton.titleLabel?.text = "Not allowed to send texts"
}
Expand Down Expand Up @@ -117,55 +116,8 @@ class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {

/// see Readme and https://developer.apple.com/documentation/messageui/mfmessagecomposeviewcontroller
@IBAction func onAppSendButton(_ sender: Any) {
displayMessageInterface()
messager.displayMessageInterface(onVC: self)
}

func displayMessageInterface() {
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() {
self.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)

}
}

0 comments on commit 7db1b77

Please sign in to comment.