Skip to content

Commit

Permalink
[ios][expo-sms] make requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
alanjhughes committed Dec 6, 2022
1 parent 503a651 commit 8e6bc3f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
8 changes: 4 additions & 4 deletions packages/expo-sms/ios/ExpoSMSModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ExpoSMSModule: Module, SMSResultHandler {
return MFMessageComposeViewController.canSendText()
}

AsyncFunction("sendSMSAsync") {(addresses: [String], message: String, options: SMSOptions, promise: Promise) in
AsyncFunction("sendSMSAsync") { (addresses: [String], message: String, options: SMSOptions, promise: Promise) in
try sendSMSAsync(addresses: addresses, message: message, options: options, promise: promise)
}.runOnQueue(.main)
}
Expand All @@ -47,11 +47,11 @@ public class ExpoSMSModule: Module, SMSResultHandler {
kUTTagClassMIMEType, attachment.mimeType as CFString, nil)

if utiRef == nil {
throw SMSAttachmentException("Failed to find UTI for mimeType: \(attachment.mimeType)")
throw SMSMimeTypeException(attachment.mimeType)
}

guard let url = URL(string: attachment.uri) else {
throw SMSAttachmentException("Invalid file uri: \(attachment.uri)")
throw SMSUriException(attachment.uri)
}

do {
Expand All @@ -61,7 +61,7 @@ public class ExpoSMSModule: Module, SMSResultHandler {
typeIdentifier: attachment.mimeType,
filename: attachment.filename)
if !attached {
throw SMSAttachmentException("Failed to attach file: \(attachment.uri)")
throw SMSFileException(attachment.uri)
}
} catch {
context.promise.reject(error)
Expand Down
35 changes: 14 additions & 21 deletions packages/expo-sms/ios/SMSDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,21 @@ class SMSDelegate: NSObject, MFMessageComposeViewControllerDelegate {
_ controller: MFMessageComposeViewController,
didFinishWith result: MessageComposeResult
) {
var resolveData = [String: String]()
var rejectMessage = ""

switch result {
case .cancelled:
resolveData["result"] = "cancelled"
case .sent:
resolveData["result"] = "sent"
case .failed:
rejectMessage = """
User's attempt to save or send an SMS was unsuccessful.
This can occur when the device loses connection to Wifi or Cellular
"""
default:
rejectMessage = "SMS message sending failed with unknown error"
}

controller.dismiss(animated: true) {
if !rejectMessage.isEmpty {
self.handler.onFailure(rejectMessage)
} else {
self.handler.onSuccess(resolveData)
switch result {
case .sent, .cancelled:
self.handler.onSuccess([
"result": result == .sent ? "sent" : "cancelled"
])
case .failed:
self.handler.onFailure(
"""
User's attempt to save or send an SMS was unsuccessful.
This can occur when the device loses connection to WiFi or Cellular
"""
)
default:
self.handler.onFailure("SMS message sending failed with unknown error")
}
}
}
Expand Down
16 changes: 14 additions & 2 deletions packages/expo-sms/ios/SMSExceptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,20 @@ internal class SMSSendingException: GenericException<String> {
}
}

internal class SMSAttachmentException: GenericException<String> {
internal class SMSFileException: GenericException<String> {
override var reason: String {
param
"Failed to attach file: \(param)"
}
}

internal class SMSMimeTypeException: GenericException<String> {
override var reason: String {
"Failed to find UTI for mimeType: \(param)"
}
}

internal class SMSUriException: GenericException<String> {
override var reason: String {
"Invalid file uri: \(param)"
}
}

0 comments on commit 8e6bc3f

Please sign in to comment.