App crashes when mail account is not setup - objective-c

App is randomly crashing when mail account is not setup. Sometimes, I am getting error alert. Please help me.
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc addAttachmentData:myData mimeType:#"application/pdf" fileName:#"myPdf2.pdf"];
[self presentViewController:mc animated:YES completion:NULL];
}
else {
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:#"Alert"
message:#"Please set up your mail account!"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okButton = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle your yes please button action here
}];
[alert addAction:okButton];
[self presentViewController:alert animated:YES completion:nil];
}

Related

migrating to AFNetworking 3.0 in objective C

The code if for checking login data with mysql database using php. the code was working with AFNetworking 2.0 but not I'm migrating to AFNetworking 3.0.
I modified the code according to the GitHub documentation.
however I got this error!
if I remove this code, the errors goes away!
NSURL *URL = [NSURL URLWithString:stringURL];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:URL.absoluteString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject)
{
// dismiss uiviewController after data loading is done
[self dismissViewControllerAnimated:NO completion:nil];
//NSLog(#"success");
NSDictionary *temp = [[NSDictionary alloc]init ];
temp = (NSDictionary *)responseObject;
if(temp.count > 0)
{
NSDictionary *TeacherInfo = responseObject[0];
TeacherName = TeacherInfo[#"Teacher_Name"];
TeacherID = TeacherInfo[#"Teacher_NO"];
IsUserAdmin = TeacherInfo[#"IsAdmin"];
TeacherSubject1 = TeacherInfo[#"Subject1No"];
TeacherSubject2 = TeacherInfo[#"Subject2No"];
TeacherSubject3 = TeacherInfo[#"Subject3No"];
TeacherSubject4 = TeacherInfo[#"Subject4No"];
// ViewController *Navigate = (ViewController *)
// [self.storyboard instantiateViewControllerWithIdentifier:#"ChoicesViewController"];;
// [self presentViewController:Navigate animated:YES completion:nil];
}
else
{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#"wrong user name or password"
message:#"please enter correct data"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
}
// if failed to connect
failure:^(NSURLSessionTask *operation, NSError *error)
{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#"connection error"
message:#"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}];
It seems you don't use dependency manager like CocoaPods. Probably you add AFNetworking library manually. You must check your target name in
Show the File in inspector for the .m file of AFHTTPSessionManager.

How can I go back to the previous view controller?

When user click "OK" button from UIAlertController popup, it will go back to the previous view controller.I got stuck how to that.Below this is my code.
if (jsonData == nil){
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Error!" message:#"This Git repository is empty" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:ok];
[self presentViewController:alertController animated:YES completion:nil];
}
Try this -
if (jsonData == nil){
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Error!" message:#"This Git repository is empty" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
// Ok action example
[self.navigationController popViewControllerAnimated:YES];
}];
[alertController addAction:ok];
[self presentViewController:alertController animated:YES completion:nil];
}

iPad crashes when showing photos or taking photo--nil source view

The follow code crashes when run on an iPad with the error message:
(<UIPopoverPresentationController: 0x1377a19e0>) should have a non-nil
sourceView or barButtonItem set before the presentation occurs.'
However, in my code I have the following, and I checked with breakpoints and console prinouts to ensure sourceview and barbutton are not nil. What do I need to do to avoid this? The error only appears on an iPad. Here is the method that causes trouble:
- (void)showChooseImageOptions {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *chooseAction = [UIAlertAction actionWithTitle:#"Choose From Photos" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
[self showLibrary];
}];
UIAlertAction *takeAction = [UIAlertAction actionWithTitle:#"Take Photo" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
[self showCamera];
}];
self.directionsTextView.text=#"";
[alertController addAction:chooseAction];
[alertController addAction:takeAction];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
if (self.imageView.image) {
self.directionsTextView.text=#"Tap the circle to change the image";
} else{
self.directionsTextView.text=#"Tap the blue circle to choose or take an image";
}
}];
[alertController addAction:cancelAction];
alertController.view.tintColor=[ColorSuperclass returnApplicationMainColor];
alertController.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItem;
if ( [alertController respondsToSelector:#selector(popoverPresentationController)] ) {
// at least iOS8
alertController.popoverPresentationController.sourceView = self.view;
}
[self presentViewController:alertController animated:YES completion:nil];
}
And the method showCamera and showLibrary are nothing but:
-(void)showCamera {
if (TARGET_IPHONE_SIMULATOR) {
//do nothing, the simulator cannot handle pressing the take photos...
} else{
[self showImagePickerForSourceType:UIImagePickerControllerSourceTypeCamera];
}
}
- (void)showLibrary {
[self showImagePickerForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
and the show source type method:
- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationPopover; //this line is very important, because otherwise, the tab bar could go out of scope (a consequence of using modal segues and tab controllers!)
imagePickerController.sourceType = sourceType;
imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES;
[self presentViewController:imagePickerController animated:YES completion:nil];
}

UIAlertController not allowing click

I am generating a UIAlertController for use in iOS8 to replace a UIActionSheet function.
The controller presents correctly, but I cannot click anywhere in the controller to get a response. I put in simple NSLog into the handler, and it is absolutely not firing.
Can anyone shed some light on this problem?
BTW, in iOS8 the UIActionSheet is ALSO not generating click events, but in iOS7 this works fine with absolutely no code change.
here is the code:
NSString *alertMessage = #"How do you wish to share ?";
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Share Image"
message:alertMessage
preferredStyle:UIAlertControllerStyleAlert];
// You can add as many actions as you want
UIAlertAction *emailShare = [UIAlertAction actionWithTitle:#"Share using email"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(#"EMAIL");
}];
UIAlertAction *smShare = [UIAlertAction actionWithTitle:#"Share using twitter/facebook"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self actionSheet:nil didDismissWithButtonIndex:1];
}];
UIAlertAction *cancelShare = [UIAlertAction actionWithTitle:#"cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self actionSheet:nil didDismissWithButtonIndex:2];
}];
// Add actions to the controller so they will appear
[alertController addAction:emailShare];
[alertController addAction:smShare];
[alertController addAction:cancelShare];
// Finally present the action
[self presentViewController:alertController animated:YES completion:nil];
You have a few different issues with your code. I've commented on the changes that are necessary. Let me know if you have any questions.
// No need to create a NSString. Just set the message.
// NSString *alertMessage = #"How do you wish to share?";
// If you'd like a UIActionSheet use UIAlertControllerStyleActionSheet as the preferredStyle
// Opposed to UIAlertControllerStyleAlert, which presents a UIAlertView
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Share Image"
message:#"How do you wish to share?" // Message Set
preferredStyle:UIAlertControllerStyleActionSheet]; // Changed to show action sheet
UIAlertAction *emailShare = [UIAlertAction actionWithTitle:#"Share using email"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[alertController dismissViewControllerAnimated:YES completion:nil];
// Call your function here
// ie. [self emailShare];
NSLog(#"First Button");
}];
UIAlertAction *smShare = [UIAlertAction actionWithTitle:#"Share using twitter/facebook"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
// [self actionSheet:nil didDismissWithButtonIndex:1];
// Where did you create actionSheet?
// didDismissWithButtonIndex: is not neccessary
// Just simply dismiss the UIAlertController
[alertController dismissViewControllerAnimated:YES completion:nil];
// And then call your function here
// ie. [self twitterShare];
NSLog(#"Second Button");
}];
UIAlertAction *cancelShare = [UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel // Set style to cancel
handler:^(UIAlertAction *action) {
// [self actionSheet:nil didDismissWithButtonIndex:2];
// Where did you create actionSheet?
// didDismissWithButtonIndex: is not neccessary
// Just simply dismiss the UIAlertController
[alertController dismissViewControllerAnimated:YES completion:nil];
NSLog(#"Cancel Button");
}];
[alertController addAction:emailShare];
[alertController addAction:smShare];
[alertController addAction:cancelShare];
[self presentViewController:alertController animated:YES completion:nil];

UIAlertController showing with delay

I'm experiencing a problem with UIAlertController on my app now migrated to iOS8 with Date Picker inside.
Below is the code.
UIAlertController *AlertView = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *ok = [UIAlertAction actionWithTitle:#"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
{
[AlertView dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *set = [UIAlertAction actionWithTitle:NSLocalizedString(#"Set to today", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
{
[self set_to_today:nil];
[AlertView dismissViewControllerAnimated:YES completion:nil];
[self.tableView reloadData];
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
{
[AlertView dismissViewControllerAnimated:YES completion:nil];
}];
UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker setDate:data_appo];
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
[AlertView.view addSubview:datePicker];
[AlertView addAction:ok];
[AlertView addAction:set];
[AlertView addAction:cancel];
[self.view bringSubviewToFront:datePicker];
[self presentViewController:AlertView animated:YES completion:nil];
UIAlertController and Date Picker is shown when the user select a row from UITableViewController.
The problem is the following:
first time the users select the row everything works fine...but if the user select "Cancel" and then select de tate again the UIAlertController takes 2-3 seconds to show up...this happens also in the simulator...
I'm getting crazy....this makes my app have a bad user experience.
Any help will be strongly appreciated
Thanks
Alex
I was having the same issue with a UIAlertController presented by selecting a row from a UITableView. The first time everything worked fine, and then when the user triggered the alert again there was a few seconds delay before the alert was actually presented.
As a workaround I used GCD:
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:AlertView animated:YES completion:nil];
});
It is probably a bug since -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath is already executed on the main thread.
I submitted a bug report to Apple: rdar://19285091
DispatchQueue.main.async {
self.present(alertView, animated: true, completion:nil)
}
Swift 3.0 version. Alternatively, setting animated: false also solved my problem.