Can't send 2 SMS at a time - ios

using xcode swift, I am trying to send several SMS to different persons in one loop. I used a "for X in Y", I checked and it is reading all the X, but I have only the first SMS sent, the others are not sent. I did not manage to wait until the fist SMS is sent to go to the next one.
Below my code:
for recette in Recettes {
var messageVC = MFMessageComposeViewController()
messageVC.body = "Pour demain: \(recette.Sujet)";
messageVC.recipients = [recette.Number]
messageVC.messageComposeDelegate = self;
self.presentViewController(messageVC, animated: false, completion: nil)
}

Related

Firebase Phone Authentication - Long Delay & Multiple OTPs

I'm working on an iOS app project that involves Firebase's phone authentication. I have it working fine on simulator, my iPhone, and my iPad. However, now that I am in the TestFlight stage , my external testers are experiencing long delays in receiving their OTPs as well as receiving duplicates when they reach the ViewController where they enter the OTP code (This is probably due to them hitting the button multiple times).
I also have APNs enabled and working properly.
I don't have much code to share as I followed Firebase's documentation.
What could be some reasons for a long delay in receiving the OTP code from Firebase? I will be including an activity spinner in the project when users tap the sign-in button. However, I also don't want it to be spinning for a minute as users wait for their OTP.
#objc func phoneSignIn() {
guard let phoneNumber = startVerificationView.phoneNumberTextField.text else { return }
let completePhoneNumber = "+1\(phoneNumber)"
Auth.auth().settings?.isAppVerificationDisabledForTesting = isVerificationDisabled
PhoneAuthProvider.provider().verifyPhoneNumber(completePhoneNumber, uiDelegate: nil) { (verificationId, error) in
if error == nil {
guard let verifyId = verificationId else { return }
UserDefaults.standard.set(verifyId, forKey: "verificationId")
let vc = CheckVerificationViewController()
vc.modalPresentationStyle = .fullScreen
vc.completePhoneNumber = completePhoneNumber
self.navigationController?.pushViewController(vc, animated: true)
}
}
}
Also isVerificationDisabled is set to false.

Why is Game Center matchmaking controller stuck on "connecting"?

Trying to solve a Game Center issue that seems to be happening within the GCMatchmakerViewController, but maybe I'm somehow causing it. There are two ways to match to players via the Invite Friends button: by typing their email (or selecting from contacts) or by tapping from presented list of people played with recently.
If I select via the list of people played with recently, a notification is sent to them, and if they accept, the app launches, goes to the proper screen, presents the GCMatchmakerViewController, connects, accepts, and away we go with the game.
If I select by typing/selecting their contact, an iMessage is sent, they go to Messages, tap the message bubble. If the game app is already running, it is brought to foreground, GCMatchmakerViewController is presented, connects, accepts, and away we go. But if the app is not running, it launches, goes to proper screen, presents GCMatchmakerViewController, tries connecting, and just sits there forever.
Why is the connection not happening in that latter scenario?
The screen that is displayed when it's stuck connecting is this:
The code that presents the vc that issues the invitation:
let localPlayer: GKLocalPlayer = GKLocalPlayer.local
if localPlayer.isAuthenticated {
let matchrequest = GKMatchRequest()
matchrequest.minPlayers = 2
matchrequest.maxPlayers = 4
matchrequest.defaultNumberOfPlayers = 2
matchrequest.inviteMessage = "Want to play: " + "some name"
if let mmVC = GKMatchmakerViewController(matchRequest: matchrequest) {
mmVC.matchmakerDelegate = self
self.present(mmVC, animated: true)
}
}
The code that accepts the invitation:
if let response = GKMatchmakerViewController.init(invite: invite) {
response.matchmakerDelegate = newGameVC
present(response, animated: true, completion: nil)
}
Best as I can tell, my app doesn't know which scenario is in play, and does not choose different behaviors based on that. Stumped, and not sure how to advise users. Thanks for any ideas.

swift 3 gamecenter: number of participants are always 4

I made a TurnBased game that was working perfectly with swift 2.
I could successfully change the code to swift 3.2 and correct all the errors about 400, but there is one issue with the game centre that I can not figure out how to fix it.
When through gamecenter viewcontroler I set the number of the participant to “2Player” and then push “Invite Friends” the number of Player that I get is 4 instead of 2!
the gamecenter send a text message to the next participant in which the number of participant is 4 too.
This of course affect the rest of the program a problem that I didn’t have in Swift 2.
This is part of my code:
func joinTurnBasedMatch()
{
let request = GKMatchRequest()
request.minPlayers = 2
request.maxPlayers = 4
request.defaultNumberOfPlayers = 2
let tbvc = GKTurnBasedMatchmakerViewController(matchRequest: request)
tbvc.turnBasedMatchmakerDelegate = self
present(tbvc, animated: true, completion: nil)
}
func turnBasedMatchmakerViewController(_ viewController: GKTurnBasedMatchmakerViewController, didFind match: GKTurnBasedMatch)
{
self.dismiss(animated: true, completion: nil)
self.performSegue(withIdentifier: "To_ViewC_CG", sender: match)
print("*** Number OF Players ***")
print(match.participants!.count)
}
Do I need to add anything to my code or change anything in it?
The problem has been solved in the latest ios update.
Now it works perfectly.

Sending sms swift in simulator [duplicate]

This question already has an answer here:
Difficulty obtaining capability to SMS via MFMessageComposeViewController in the iPhone simulator
(1 answer)
Closed 7 years ago.
If it possible to simulate sending sms in iPhone Simulator. I am not expecting to really send sms, just to show sms sending panel.
This is my code:
if MFMessageComposeViewController.canSendText() {
let messageVC = MFMessageComposeViewController()
messageVC.messageComposeDelegate = self
messageVC.recipients = ["1111111111", "2222222222"]
messageVC.body = "hello phone"
self.presentViewController(messageVC, animated: true, completion: nil)
} else {
print("Not using messages")
It always just print "Not using messages". Can someone advice, am I doing something wrong?
Thanks!
No it is not possible to show sms sending panel in iOS simulator. You'll have to register for Apple's developer program and test that on the device.

I have REAL misunderstanding with MFMailComposeViewController in Swift (iOS8) in Simulator

I create a CSV file and try to send it by e-mail. Displays a window to send mail, but is not filled with the body of the email, and no attached file. Application hangs with this screen:
button "Cancel" does not work. After a few seconds in the console appears:
viewServiceDidTerminateWithError: Error Domain=_UIViewServiceInterfaceErrorDomain Code=3 "The operation couldn’t be completed. (_UIViewServiceInterfaceErrorDomain error 3.)" UserInfo=0x7f8409f29b50 {Message=Service Connection Interrupted}
<MFMailComposeRemoteViewController: 0x7f8409c89470> timed out waiting for fence barrier from com.apple.MailCompositionService
There is my code:
func actionSheet(actionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int) {
if buttonIndex == 0 {
println("Export!")
var csvString = NSMutableString()
csvString.appendString("Date;Time;Systolic;Diastolic;Pulse")
for tempValue in results { //result define outside this function
var tempDateTime = NSDate()
tempDateTime = tempValue.datePress
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
var tempDate = dateFormatter.stringFromDate(tempDateTime)
dateFormatter.dateFormat = "HH:mm:ss"
var tempTime = dateFormatter.stringFromDate(tempDateTime)
csvString.appendString("\n\(tempDate);\(tempTime);\(tempValue.sisPress);\(tempValue.diaPress);\(tempValue.hbPress)")
}
let fileManager = (NSFileManager.defaultManager())
let directorys : [String]? = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.AllDomainsMask, true) as? [String]
if ((directorys) != nil) {
let directories:[String] = directorys!;
let dictionary = directories[0];
let plistfile = "bpmonitor.csv"
let plistpath = dictionary.stringByAppendingPathComponent(plistfile);
println("\(plistpath)")
csvString.writeToFile(plistpath, atomically: true, encoding: NSUTF8StringEncoding, error: nil)
var testData: NSData = NSData(contentsOfFile: plistpath)
var myMail: MFMailComposeViewController = MFMailComposeViewController()
if(MFMailComposeViewController.canSendMail()){
myMail = MFMailComposeViewController()
myMail.mailComposeDelegate = self
// set the subject
myMail.setSubject("My report")
//Add some text to the message body
var sentfrom = "Mail sent from BPMonitor"
myMail.setMessageBody(sentfrom, isHTML: true)
myMail.addAttachmentData(testData, mimeType: "text/csv", fileName: "bpmonitor.csv")
//Display the view controller
self.presentViewController(myMail, animated: true, completion: nil)
}
else {
var alert = UIAlertController(title: "Alert", message: "Your device cannot send emails", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
else {
println("File system error!")
}
}
}
Trying instead to send mail using UIActivityViewController:
let fileURL: NSURL = NSURL(fileURLWithPath: plistpath)
let actViewController = UIActivityViewController(activityItems: [fileURL], applicationActivities: nil)
self.presentViewController(actViewController, animated: true, completion: nil)
See approximately the same screen to send e-mail, which after a while returning to the previous screen. In the console, now another error:
viewServiceDidTerminateWithError: Error Domain=_UIViewServiceInterfaceErrorDomain Code=3 "The operation couldn’t be completed. (_UIViewServiceInterfaceErrorDomain error 3.)" UserInfo=0x7faab3296ad0 {Message=Service Connection Interrupted}
Errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo=0x7faab3005890 {NSLocalizedDescription=query cancelled}
<MFMailComposeRemoteViewController: 0x7faab3147dc0> timed out waiting for fence barrier from com.apple.MailCompositionService
There was something about PlugInKit.
Trying instead UIActivityViewController using UIDocumentInteractionController:
let docController = UIDocumentInteractionController(URL: fileURL)
docController.delegate = self
docController.presentPreviewAnimated(true)
...
func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController!) -> UIViewController! {
return self
}
I see this screen with contents a CSV-file:
I press button export in top-right and see this screen:
where I choose MAIL and for several seconds I see:
Then returns to displaying the contents of the file! In the console the same messages as when using UIActivityViewController.
* * IMPORTANT - DO NOT USE THE SIMULATOR FOR THIS. * *
Even in 2016, the simulators very simply do not support sending mail from apps.
Indeed, the simulators simply do not have mail clients.
But! Do see the message at the bottom!
Henri has given the total answer. You MUST
-- allocate and initiate MFMailComposeViewController in an earlier stage, and
-- hold it in one static variable, and then,
-- whenever it's needed, get the static MFMailComposeViewController instance and use that.
AND you will almost certainly have to cycle the global MFMailComposeViewController after each use. It is not reliable to re-use the same one.
Have a global routine which releases and then re-initializes the singleton MFMailComposeViewController. Call to that global routine, each time, after you are finished with the mail composer.
Do it in any singleton. Don't forget that your app delegate is, of course, a singleton, so do it there...
#property (nonatomic, strong) MFMailComposeViewController *globalMailComposer;
-(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
........
// part 3, our own setup
[self cycleTheGlobalMailComposer];
// needed due to the worst programming in the history of Apple
.........
}
and...
-(void)cycleTheGlobalMailComposer
{
// cycling GlobalMailComposer due to idiotic iOS issue
self.globalMailComposer = nil;
self.globalMailComposer = [[MFMailComposeViewController alloc] init];
}
Then to use the mail, something like this ...
-(void)helpEmail
{
// APP.globalMailComposer IS READY TO USE from app launch.
// recycle it AFTER OUR USE.
if ( [MFMailComposeViewController canSendMail] )
{
[APP.globalMailComposer setToRecipients:
[NSArray arrayWithObjects: emailAddressNSString, nil] ];
[APP.globalMailComposer setSubject:subject];
[APP.globalMailComposer setMessageBody:msg isHTML:NO];
APP.globalMailComposer.mailComposeDelegate = self;
[self presentViewController:APP.globalMailComposer
animated:YES completion:nil];
}
else
{
[UIAlertView ok:#"Unable to mail. No email on this device?"];
[APP cycleTheGlobalMailComposer];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError *)error
{
[controller dismissViewControllerAnimated:YES completion:^
{ [APP cycleTheGlobalMailComposer]; }
];
}
{nb, fixed typo per Michael Salamone below.}
Have the following macro in your Prefix file for convenience
#define APP ((AppDelegate *)[[UIApplication sharedApplication] delegate])
Also here's a "minor" problem which can cost you days: https://stackoverflow.com/a/17120065/294884
Just for 2016 FTR here's the basic swift code to send an email IN APP,
class YourClass:UIViewController, MFMailComposeViewControllerDelegate
{
func clickedMetrieArrow()
{
print("click arrow! v1")
let e = MFMailComposeViewController()
e.mailComposeDelegate = self
e.setToRecipients( ["help#smhk.com"] )
e.setSubject("Blah subject")
e.setMessageBody("Blah text", isHTML: false)
presentViewController(e, animated: true, completion: nil)
}
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?)
{
dismissViewControllerAnimated(true, completion: nil)
}
However! Note!
These days it is crappy to send an email "in app".
It's much better today to simply cut away to the email client.
Add to plist ...
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram</string>
</array>
and then code like
func pointlessMarketingEmailForClient()
{
let subject = "Some subject"
let body = "Plenty of <i>email</i> body."
let coded = "mailto:blah#blah.com?subject=\(subject)&body=\(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
if let emailURL:NSURL = NSURL(string: coded!)
{
if UIApplication.sharedApplication().canOpenURL(emailURL)
{
UIApplication.sharedApplication().openURL(emailURL)
}
else
{
print("fail A")
}
}
else
{
print("fail B")
}
}
These days, that is much better than trying to email from "inside" the app.
Remember again the iOS simulators simply do not have email clients (nor can you send email using the composer within an app). You must test on a device.
It has nothing to do with Swift. It's an issue with the mail composer that's been around forever it seems. That thing is extremely picky, from failing with timeouts to sending delegate messages even when cancelled.
The workaround everyone uses is to create a global mail composer (for example in a singleton), and every single time reinitializing it when you need it. This ensures the mail composer is always around when the OS needs it, but also that it is free of any crap when you want to reuse it.
So create a strong (as global as possible) variable holding the mail composer and reset it every time you want to use it.
XCode 6 Simulator has problems managing Mailcomposer and other things.
Try testing the code with a real device. Likely it will work.
I have problems when running MailComposer from actionSheet button, also with real test. With IOS 7 worked fine, the same code in IOS 8 does not work. For me Apple must depurated the XCode 6. ( too many different simulated devices with Objective-C and Swift together ...)
Not sure if the recycling proposed in above solution is necessary or not. But you do need use proper parameters.
The delegate receives a MFMailComposeViewController* parameter. And you need to use that instead of self when dismissing the controller. I.e.
The delegate receives the (MFMailComposeViewController *) controller. And you need to use that instead of self when dismissing the MFMailComposeViewController controller. That is what you want to dismiss after all.
-(void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError *)error
{
[controller dismissViewControllerAnimated:YES completion:^
{ [APP cycleTheGlobalMailComposer]; }
];
}
Create a property for the mail composer and instantiate it in view did load than call it when ever you need a mail composer.
#property (strong, nonatomic) MFMailComposeViewController *mailController;
self.mailController = [[MFMailComposeViewController alloc] init];
[self presentViewController:self.mailController animated:YES completion:^{}];
Hey this is solved with iOS 8.3 released 2 days ago.
A simple helper class for handling mail in Swift. Based on Joe Blow's answer.
import UIKit
import MessageUI
public class EmailManager : NSObject, MFMailComposeViewControllerDelegate
{
var mailComposeViewController: MFMailComposeViewController?
public override init()
{
mailComposeViewController = MFMailComposeViewController()
}
private func cycleMailComposer()
{
mailComposeViewController = nil
mailComposeViewController = MFMailComposeViewController()
}
public func sendMailTo(emailList:[String], subject:String, body:String, fromViewController:UIViewController)
{
if MFMailComposeViewController.canSendMail() {
mailComposeViewController!.setSubject(subject)
mailComposeViewController!.setMessageBody(body, isHTML: false)
mailComposeViewController!.setToRecipients(emailList)
mailComposeViewController?.mailComposeDelegate = self
fromViewController.presentViewController(mailComposeViewController!, animated: true, completion: nil)
}
else {
print("Could not open email app")
}
}
public func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?)
{
controller.dismissViewControllerAnimated(true) { () -> Void in
self.cycleMailComposer()
}
}
}
Place as instance variable in AppDelegate-class and call when needed.

Resources