Simulating sent SMS on iPhone - iphone

Since debugging is extremely slow with Xcode 4.3 on iOS 5.1 when starting/installing the app on the device I use the simulator which starts much faster. (see my question regarding this issue here https://stackoverflow.com/questions/11541288/xcode-4-3-with-ios5-1-pauses-about-10secs-when-debug-starts-simulator-starts-i)
So all I need to do is something like this:
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = delegate;
NSString *s = #"1234567";
picker.recipients =[NSArray arrayWithObject: s];
picker.body =smsTxt;
if (simulationMode) {
MessageComposeResult result = MessageComposeResultSent; <-----------
[delegate messageComposeViewController:picker didFinishWithResult: result];
} else
[delegate presentModalViewController:picker animated:YES];
Here the problem is now that when executing on iOS-Simulator the MFMessageComposeViewController can't be instantiated and always yields nil.
Is there a way to create another object MyOwnMFMessageComposeViewController on iOS simulator which is compatible to MFMessageComposeViewController and can be passed in the same method like MFMessageComposeViewController?
Something like this:
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = delegate;
NSString *s = #"1234567";
picker.recipients =[NSArray arrayWithObject: s];
picker.body =smsTxt;
if (simulationMode) {
MyOwnMFMessageComposeViewController *mypicker = [[MFMessageComposeViewController alloc] init];
mypicker.messageComposeDelegate = delegate;
NSString *s = #"1234567";
mypicker.recipients =[NSArray arrayWithObject: s];
mypicker.body =smsTxt;
MessageComposeResult result = MessageComposeResultSent;
picker = (MFMessageComposeViewController) mypicker;
[delegate messageComposeViewController:picker didFinishWithResult: result];
} else
[delegate presentModalViewController:picker animated:YES];

What you are looking for is called a 'mock object' and is often used in test driven development. Basically what you do is create a subclass of MFMessageComposeViewController. This subclass works exactly the same as mfmessagecomposeviewcontroller except you also create instance variables to show that something has happened.
So for example when your delegate calls messageComposeViewController:didFinishWithResult. The mock object would likely store the result and a flag that that method had been fired. Note that this won't actually send anything, but simply tells you that the delegate fired and on a real object will work.

Related

MFMessageComposeViewController shows blank white screen in iOS7

there's a problem when I try to send a large recipients list (e.g more than 40) using MFMessageComposeViewController. In iOS7, it will show a blank white screen for 20s or more before displaying the SMS compose view. This does not occur for iOS5 and iOS6.
Below is the existing code that I'm using,
NSArray * recipients;
for (NSIndexPath * index in selectedRows)
{
NSDictionary *dictionary = [data objectAtIndex:index.row];
NSString *phoneNum = [dictionary objectForKey:#"contactNum"];
recipients = [NSArray arrayWithObjects:phoneNum, nil]];
}
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
controller.body = bodyOfMessage;
controller.recipients = recipients;
controller.messageComposeDelegate = self ;
controller.wantsFullScreenLayout = NO;
[(id)_delegate presentModalViewController:controller animated:YES];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
Below are the output message that I received when I try to send to many.
timed out waiting for fence barrier from com.apple.mobilesms.compose
Received memory warning.
Received memory warning.
Received memory warning.
Received memory warning.
Received memory warning.
Received memory warning.
Received memory warning.
Received memory warning.
Received memory warning.
I had a similar problem where I got the message in the console "
timed out waiting for fence barrier from com.apple.mobilesms.compose
The problem was that I tried in my app to add the number as a string, but because of the localization request, I have put it in a form:NSArray *recipents = #[NSLocalizedString(#"numberForRegistrationViaSms", #"")];
and
[messageController setRecipients:#[recipents]];
That didn't work for some reason but, when I put just simply, [messageController setRecipients:#[#"123456789"]];, the SMS composer appears without any problem.
I to had the same problem then figured
controller.recipients = // should always be an array of strings.
Make sure the phone numbers you send to controller.recipients are NSString.
I think I maybe resolve this:
//must initiate new NSString object
NSString *phoneStr = [NSString stringWithFormat:#"%#",... ];
MFMessageComposeViewController *aCtrl = [[MFMessageComposeViewController alloc] init];
aCtrl.recipients = #[phoneStr];
...
Then OK.
I had same problem.
timed out waiting for fence barrier from com.apple.mobilesms.compose
Message Cancelled
Instead of this:
NSString *phoneNumber = #"888888888";
[picker setRecipients:#[phoneNumber]];
Try this:
NSString *phoneNumber = person.phoneNumber;
[picker setRecipients:#[[NSString stringWithFormat:#"%#", phoneNumber]]];
This worked for me.
The problem has been resolved in iOS 7.0.3.

Memory management creating an NSDictionary with NSMutableArrays

I'm having problem understanding memory management when creating a dictionary with mutable arrays. I'm using the ios6 SDK with deployment target 5.1.
In the implementation of the class "Group" the method "namesAndEmails" builds an array "emails" that contains the emails addresses for Person objects with an email. If the Person object does not have an email the Person name is added to another array "namesWithNoEmail". The arrays are returned in a dictionary.
#import "Group.h"
#implementation Group
-(NSDictionary*) namesAndEmails {
NSMutableArray *emails = [[NSMutableArray alloc] initWithCapacity:0] ;
NSMutableArray *namesWithNoEmail = [[NSMutableArray alloc] initWithCapacity:0];
NSString *email;
NSString *name;
for (Person *p in allPersons) {
email = p.email;
name = p.name;
if ([email length]==0) {
[namesWithNoEmail addObject:name];
} else {
[emails addObject:email];
}
}
NSArray *keys = [NSArray arrayWithObjects:#"emails",#"names", nil];
NSArray *objects = [NSArray arrayWithObjects:emails, namesWithNoEmail, nil];
//[emails release];
//[namesWithNoEmail release];
return [NSDictionary dictionaryWithObjects:objects forKeys:keys];
}
Somewhere else in the code I wish to send an email to a group of people so I call the emailGroup method which gets a dictionary out by calling "namesAndEmails" on the group.
-(void) emailGroup:(Group*) g {
NSDictionary *emailInfo = [g namesAndEmails];
guestsWithNoEmail = [emailInfo objectForKey:#"names"];
guestEmails = [emailInfo objectForKey:#"emails"];
int nGuestsWithNoEmail = [guestsWithNoEmail count];
if (nGuestsWithNoEmail > 0) {
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"No emails" message:#"" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] autorelease];
[alert show];
}
// some more code here
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:subject];
[picker setMessageBody:#"" isHTML:NO];
[picker setToRecipients:guestEmails];
[[self delegate ] presentModalViewController:picker animated:YES];
[picker release];
}
As far as I understand [NSDictionary dictionaryWithObjects:objects forKeys:keys] in "namesAndEmails" returns an autoreleased dictionary. But why does my code crash if I release the "emails" and "namesWithNoEmail" arrays? I thought that the dictionary would have ownership of the array after they are added and therefore it would be safe to release the arrays in the method. I guess that's not correct, but why?
Is the a more clean way of doing this? Thank you for any advice!
My first suggestion would be to use the "Product->Analyze" feature. If you leaking or over releasing somewhere, it will probably give you the exact chain of events.
Secondly, I can't see the linking between your methods nameAndEmails and emailGroup:. Because I can't see the connection, I can't tell you if the autorelease is causing the problem.
Autoreleased objects get released when the the main run loop cycles. So it's very possible your NSDictionary is getting released. You could test this by doing anything from setting the memory location as a "watch" in the debugger to putting printing something in the console lines each time the runloop your in cycles (I made the assumption your in the main run loop, so correct me if that's not true).
Other things you can do to track the problem would be to use "Zombies" in instruments or NSZombieEnable=YES in your configuration

iPhone SMS Url Scheme with Body Content?

I am looking to pre-fill the SMS body with content given "smsto:555-555-5555:Hello World"
I have done my research and everybody says it cannot be done but I have a QR reader on my phone right now that can take "sms:555-555-5555:Hello world" and pre-fills the content with the correct phone number scanned.
So my question is. How do i do this? it is pretty clear that the URL scheme provided doesn't handle the body. What other methods are out there to pre-fill the body? it can clearly be done with the QuickMark Reader.
You can use the modalViewController for SMS to fill the body of a text.
add the MessageUI framework to your project.
set you viewController as the MFMessageComposeDelegate
Create the modal view and present it:
-(void) showMessageComposerWithText:(NSString*)messageText telNumber:(NSString*)telNumber composeDelegate:(id)delegate
{
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText]){
controller.body = messageText;
controller.recipients = [NSArray arrayWithObject:telNumber];
controller.messageComposeDelegate = delegate;
[delegate presentModalViewController:controller animated:YES];
}
}
You can't launch the Messages app with a predefined message according to Apple Documentation.
What you can do is implement the handling yourself and parse the URL like the following:
NSURL *url = /* the url you get from the web (in webview delegate) or after QR Code scan */;
if ([url.scheme isEqualToString:#"sms"]) // is it a sms ?
{
if([MFMessageComposeViewController canSendText]) // can I send text message ?
{
NSArray *parts = [url.absoluteString componentsSeparatedByString:#":"];
NSString *messageText = parts.count == 3 ? [parts objectAtIndex:2] : #"";
NSString *telNumber = parts.count >= 2 ? [parts objectAtIndex:1] : #"";
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
controller.body = messageText;
controller.recipients = [NSArray arrayWithObject:telNumber];
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
[controller release];
}
}
And voilĂ  !

UIImagePickerController mediaTypes kUTTypeMovie causes small memory leak in NSCFNumber

Xcode 4.0.2
iPhone4 iOS 4.3.5
CoreLocation and MobileCoreServices frameworks are implemented.
imported MobileCoreServices/UTCoreTypes.h
- (void) displayVideoPicker
{
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
ipc.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
ipc.videoMaximumDuration = 45.0f;
ipc.videoQuality = UIImagePickerControllerQualityTypeMedium;
ipc.delegate = self;
[self presentModalViewController:ipc animated:YES];
[ipc release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[[picker parentViewController] dismissModalViewControllerAnimated: YES];
}
Instruments is reporting small 16 Bytes leaks on NSCFNumber, possible Frame FigRemote_CreatePropertyListFromBinaryPListData:
Leaked Object # Address Size Responsible Library Responsible Frame
NSCFNumber,8 128 Bytes MediaToolbox FigRemote_CreatePropertyListFromBinaryPListData
If I remove this line the leak goes away:
ipc.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
I've tried this and it didn't get rid of the leak.
NSArray *myMediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
ipc.mediaTypes = myMediaTypes;
[myMediaTypes release];
The leak occurs just after picking the video and returning to the initial view. Any thoughts?
I'm getting a couple of leaks around the UIImagePickerController and this was one of them. Total leak was 208 bytes. I found that if the media types are specified using CoreFoundation C code, this leak goes away.
So the following code leaks:
imagePickerController.mediaTypes = [NSArray arrayWithObjects:(NSString*)kUTTypeImage, (NSString*)kUTTypeMovie, nil];
Can be replaced by the following code which does not cause the same leak:
CFStringRef mTypes[2] = { kUTTypeImage, kUTTypeMovie };
CFArrayRef mTypesArray = CFArrayCreate(CFAllocatorGetDefault(), (const void**)mTypes, 2, &kCFTypeArrayCallBacks);
imagePickerController.mediaTypes = (NSArray*)mTypesArray;
CFRelease(mTypesArray);
My theory is that the leaking statement is not providing the information that the final argument of the working code provides. kCFTypeArrayCallBacks provides the CFArray created with the information it needs to properly retain and release the objects it contains (using CFRetain and CFRelease). These objects it contains are CFStringRefs... The leaking statement casts these CFStringRefs to NSStrings and I think the NSArray is using Objective-C standard retain and release messages on its objects.
This theory would seem to be bogus because CFArray and NSArray are toll-free bridged and it should "just work". I'm sticking with it for now though - it's the only significant difference between the two code snippets above that jumps out at me.
First, you need to be sure to include the MobileCoreServices Framework into your Xcode project, then #import <MobileCoreServices/MobileCoreServices.h> which gives you access to the predefined media type constants such as kUTTypeImage and KUTTypeMovie.
Then, you need to know that mediaType property expects an array, even if it contains only one element.
The view controller you are putting this code in needs to be the delegate for these two: UINavigationControllerDelegate and UIImagePickerControllerDelegate
Then you can put this code somewhere to have it be executed (like inside a button action):
UIImagePickerController *camera = [[UIImagePickerController alloc] init];
camera.sourceType = UIImagePickerControllerSourceTypeCamera;
camera.delegate = self;
camera.mediaTypes = #[(NSString *)kUTTypeImage, (NSString *)kUTTypeMovie];
[self presentViewController:camera animated:YES completion:nil];
Also, on line 4 of my code above where it says camera.mediaTypes, you could just easily do this as well:
camera.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];

iPhone application lags after launching and dismissing MFMailComposeViewController

I have an application that uses a table view controller to display some items, after clicking on one of those items you may select to email this item. Once that happens I use the code provided by apple "MailComposer", and send the mail. However after this the scrolling in the table view is not as smooth as before.
I checked with "Leaks" and there are no leaks in my code, however there is a great deal of object allocation when the modal view controller for the MFMailComposeViewController, and when i dismiss my controller, all that object allocation is still there. How can i get rid of all that object allocation?. Any help will be greatly appreciated. Thank you.
-Oscar
UPDATE:
I have realized the lag only happens once you click on the To: textfield on the MFMailComposeViewController and type something, once something has been typed there will be a memory leak and the application will be sluggish. This exact same thing also happens in Apple's Mail Composer. I am using the simulator maybe this is why?. Does anyone else have a simmilar experience?
The way I am pressenting my controller is:
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSString *mailSubject = appDelegate.mailTitle;
NSString *mailBody = appDelegate.mailLink;
NSString *formattedString = [NSString stringWithFormat:#"<a href='%#'>%#</a>", mailBody, mailBody];
[picker setSubject:mailSubject];
// Set up recipients
//NSArray *toRecipients = [NSArray arrayWithObject:#"somemail#hotmail.com"];
//NSArray *ccRecipients = [NSArray arrayWithObjects:#"second#example.com", #"third#example.com", nil];
//NSArray *bccRecipients = [NSArray arrayWithObject:#"fourth#example.com"];
//[picker setToRecipients:toRecipients];
//[picker setCcRecipients:ccRecipients];
//[picker setBccRecipients:bccRecipients];
// Attach an image to the email (Warning this causes a memory leak aknowledged by Apple)
//NSString *path = [[NSBundle mainBundle] pathForResource:#"news_icon" ofType:#"png"];
//NSData *myData = [NSData dataWithContentsOfFile:path];
//[picker addAttachmentData:myData mimeType:#"image/png" fileName:#"rainy"];
// Fill out the email body text
[picker setMessageBody:formattedString isHTML:YES];
[self presentModalViewController:picker animated:YES];
[picker release];
}
and dimissing it here:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
....
[self dismissModalViewControllerAnimated:YES];
}
It's a known memory leak in MFMailComposeViewController class (as of iOS 4.2 SDK). The leaks can be even seen in the MailComposer sample project by Apple. Try to run the app with Allocations instrument and notice the Overall Bytes growing up every time you click cancel and show the composer again.
See below for the similar discussion:
http://discussions.apple.com/thread.jspa?threadID=2158170
https://devforums.apple.com/thread/23510?tstart=15
https://devforums.apple.com/message/121093#121093
Make sure you use
controller.mailComposeDelegate = self;
and not
controller.delegate = self;