Application crashed when I am using PresentModelView in iphone sdk - iphone

Here is my code:
-(void)btnAction
{
mapController = [[MKMapController alloc] initWithNibName:#"MKMapController" bundle:nil];
mapController.delegate = self;
mapController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:mapController animated:YES];
[mapController release];
}
I am getting the crash message as:
-[MKMapController respondsToSelector:]: message sent to deallocated instance 0x79ccc90
Guy's please help me how to resolve this..!!
Thanks to all,
Monish.

Release your mapController in dealloc method.

Related

Send SMS iPhone

I was trying to send SMS from within my app. I wrote this piece of code but it seems not to work.
No crash, no error log, simply nothing happen (of course I tried to log canSendText and the result is 1).
- (void)viewDidLoad
{
[super viewDidLoad];
messageComposer = [[MFMessageComposeViewController alloc] init];
if ([MFMessageComposeViewController canSendText]) {
[messageComposer setBody:#"Messaggio generato da SMSTest"];
[messageComposer setRecipients:[NSArray arrayWithObject:#"3333333333"]];
[messageComposer setDelegate:self];
[self presentModalViewController:messageComposer animated:YES];
}
}
Can anyone explain me what I'm doing wrong?
The problem is that presentModalViewController does not work in viewDidLoad yet as the view is loaded but might not even be on screen yet. If you put your code in viewWillAppear:animated, this should work.
Edit: As per Saphrosit's comment: viewDidAppear: is an even better place to do this.
I use this successfully:
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
controller.messageComposeDelegate = self;
controller.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController:controller animated:YES];
Note that it's messageComposeDelegate, not 'delegate' as you do.
Tim
When i try the code on simulator i get an UIAlert saying text messaging is not available, because simulator canĀ“t send messages. Have you checked that your header file is a delegate of MFMessageComposeViewControllerDelegate ?
YourClassName : UIViewController <MFMessageComposeViewControllerDelegate>
//try this ... it will run ..
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
controller.body = #"Hello from Kartik";
controller.recipients = [NSArray arrayWithObjects:#"12356478", nil];
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}

MFMailComposer Exec_bad_access - might it have something to do with initialization

In a view controller i have a button which internally calls an IBAction and that internally calls the
[self showEmailModalView:FinalEmail];
now ... in the showEmailModalView i have
-(void) showEmailModalView:(NSString *)email{
MFMailComposeViewController *mvc =[[[MFMailComposeViewController alloc] init]autorelease];
mvc.mailComposeDelegate = self;
NSArray *mails = [[NSArray arrayWithObject:email]autorelease];
[mvc setToRecipients:mails];
NSString*emailBody =[NSString stringWithFormat:#" "];
[mvc setMessageBody:emailBody isHTML:YES];
mvc.navigationBar.barStyle = UIBarStyleBlack;
[[self navigationController] presentModalViewController:mvc animated:YES];
}
and then
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[[self navigationController] dismissModalViewControllerAnimated:YES];
}
Now it works alright... for all the scenarios of email or delete or anything like that but right after completion of the code the simulator just throws a exec_bad_access and then does nothing.... can you help.
PS: i also found out that that when ever you are giving a release to the mvc as declared above the error is called. ant thoughts.
Update:
I did install the app with only one change... i initialized the mvc in the header file and then just use the same statement. so now i am not getting the exec_bad_access errors anymore... i think it was frightened of the nsZOMBIES....Haha... thanks for your Help and support.. guys... Both of you....
You shouldn't autorelease this object, it's already autoreleased :
NSArray *mails = [[NSArray arrayWithObject:email ]autorelease];
Instead of change to:
NSArray *mails = [NSArray arrayWithObject:email];

EXC_BAD_ACCESS and Zombies, Yet not really sure why it keeps coming up

I don't know what's going wrong here. The crash happens when switching back and forth between views.
Here's what instruments gives me:
Clicking into it references this code with the first action :
-(IBAction)pushnews; {
NewsViewController *news = [[[NewsViewController alloc]init]autorelease];
news.title =#"Page";
[self.navigationController pushViewController:news animated:YES]; }
I use autorelease sometimes but usually I just release it my self. Should I get rid of autorelease and add [news retain]
What am I doing wrong?
Edit based on answers:
Following EmptyStack's Advice: ViewWillDisappear Code looks like this:
- (void)viewWillDisappear:(BOOL)animated {
webView.delegate = nil; }
This seems to resolve issues (pending more testing)
In viewdidload I said: webView.delegate = self;, which may have been the issue!
My guess is that, there is a UIWebView in NewsViewController, and it is causing the crash. It is possible that, a delegate method of web view is called after the web view is released. If so, try to setwebView.delegate = nil; in NewsViewController's viewWillDisappear: method.
try this instead :
-(IBAction)viewcontroller;
{
NewsViewController *news = [[NewsViewController alloc]init];
news.title =#"Page";
[self.navigationController pushViewController:news animated:YES];
[news release];
}

Can't set recipients of MFMessageComposeViewController?

I have a method like this:
void sendSMS{
if ([MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.recipients = [NSArray arrayWithObject:#"0933660805"];
[picker setBody:#"Message body"];
picker.messageComposeDelegate = self;
[self.navigationController presentModalViewController:picker animated:YES];
//[picker release];
return;
}
}
Message composer open but recipients and message body are empty (image below). Anybody know how can i fix it :(
Go for this ones and then check may be it will resolve your issue
void sendSMS
{
if ([MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self;
NSString *bodyString = nil;
NSMutableArray *toRecipients = [[NSMutableArray alloc]init];
[toRecipients addObject:#"0933660805"];
[picker setRecipients:(NSArray *)toRecipients];
[toRecipients release];
bodyString = [NSString stringWithFormat: #"Message body"];
[picker setBody:bodyString];
[self presentModalViewController:picker animated:YES];
[picker release];
}
Also take a look at this tutorial http://blog.mugunthkumar.com/coding/iphone-tutorial-how-to-send-in-app-sms/
Good Luck!
OK I answered my own question. Now I want no one else to have to go thru this. I was calling this method from just an NSObject. It was a delegate to MFMessageComposeViewControllerDelegate but that made no difference. I had to move this method to my MainViewController, then it worked.
iOS 10.0 is here and this is still a problem for me. So, I have fashioned a workaround.
According to previous comments that initializing the MFMessageComposeViewController in the viewDidLoad() won't solve the problem (which I can attest to), unless the view controller gets presented, it won't be cached. So, the hack here is to make a window, set its root view controller, present a dummy MFMessageComposeViewController instance and immediately dismiss it, somewhere before your actual need (like in viewDidLoad())
Here is a sample code I'm using (Swift 3.0 - Let me know if you were interested in Obj-C counterpart):
let window = UIWindow()
let vc = UIViewController()
window.rootViewController = vc
let messageCompose = MFMessageComposeViewController()
vc.present(messageCompose, animated: false) { [weak messageCompose] in
messageCompose?.dismiss(animated: false, completion: nil)
}
The thing here is that if you present it in the currently active window's view controller chain, it will mess up your UI by showing and hiding the keyboard abruptly (no matter how you try to hide the controller's view and what not), due to the message body selection on present. But adding it to a whole new window which is not in view cycle, it will be correctly initialized and there will be no trace of such transaction on view. Plus, you won't boggle the memory too much this way (because the scope of the controller should be minimal now) and you can initialize your actual MFMessageComposeViewController any time you want and get it much faster now. If your application heavily relies on MFMessageComposeViewController (which I doubt) you can move this to your AppDelegate to be ready anywhere around your app's life cycle.
Cheers,
M.
Try this
- (void)sendSMS
{
if ([MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self;
NSString *bodyString = nil;
NSArray *toRecipients = [NSArray arrayWithObject:#"NUMBER HERE"];
[picker setRecipients:toRecipients];
[self presentModalViewController:picker animated:YES];
[picker release];
}
In my case (on iPhone 3g s) the problem was when I called [self.navigationController pushViewController... ], when i tried call [self presentModalViewController ...] it worked, I dont know why, but it is. Try it.
set the MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
before if ([MFMessageComposeViewController canSendText]) {...}
Try this.
Try this.
- (void)forwardPromo
{
MFMessageComposeViewController *composeViewController = [[MFMessageComposeViewController alloc] init];
composeViewController.body = #"Message body";
composeViewController.recipients = [NSArray arrayWithObject:#"0933660805"];
composeViewController.messageComposeDelegate = self;
[self presentViewController:composeViewController animated:YES completion:nil];
}
You should have a "nil" at the end of the array:
composeViewController.recipients = [NSArray arrayWithObject:#"0933660805", nil];

Help Troubleshooting NSZombie Error Message

I have a modal view controller which is crashing when it dismisses itself. The error is EXC_BAD_ACCESS (yipee). I am tryin got use NSZombie to work out the problem. I get am getting the following :
2010-10-20 17:15:58.936 [24058:207] AddRunningClient starting device on non-zero client count
2010-10-20 17:16:06.846 [24058:207] * -[ViewController retain]: message sent to deallocated instance 0x6c2d4a0
What does this mean - does it mean that a message was sent to the Viewcontroller or that a message was sent to an object in the Viewcontroller ?
I am really stuck as the thread seems to be main :(
Thanks all in advance for any help,
Martin
EDIT
Thanks all for the quick replies. Here is how I am presenting the view controller :
-(IBAction)letsstartGame {
ViewController * sl = [[ViewController alloc] initWithNibName:#"ViewController" bundle:[NSBundle mainBundle]];
self.viewLink = sl;
[sl release];
[mainMenu stop];
[mainMenu setCurrentTime:0.0];
[self presentModalViewController:viewLink animated:NO];
[viewLink release];
self.viewLink = nil;
}
And dismiss like this :
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (waitingOver) {
[backgroundMain stop];
[fireworks stop];
[self dismissModalViewControllerAnimated:NO];
}
}
That means that you had an instance of an object of type ViewController, it was deallocated, and then you tried to retain it.
edit
You're over-releasing the object. Here's what you're doing:
ViewController * sl = [[ViewController alloc] initWithNibName:#"ViewController" bundle:[NSBundle mainBundle]]; //allocated, has a +1 retain count
self.viewLink = sl; //assuming a retain property, has a +2 retain count
[sl release]; //releasing, now has +1 retain count
....
[viewLink release]; //releasing, now has a 0 retain count
self.viewLink = nil; //attempting to release stale pointer, will result in a crash (perhaps not immediately, but eventually)
Get rid of the [viewLink release] line. It is wrong to have that in there.
It means you are sending a message to a deallocated instance.
So somewhere in your code you have failed to retain an object (probably ViewController) or have released it prematurely.
If you can post your code where you create the View Controller that might be helpful for us to debug.
The message is basically saying that you are trying to send a message (call a function) on/to an object that has already been dealloc'd (released and the memory freed). If you could send more code, I could perhaps attempt to determine why.