UIPopoverPresentationController disappear issue in iPad - ios

I have used UIPopoverPresentationController in iPad for selecting Camera or Photo Gallery to choose or capture image.
Below is my code,
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alertController;
UIAlertAction *destroyAction;
UIAlertAction *otherAction;
alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
destroyAction = [UIAlertAction actionWithTitle:#"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[imagePicker setDelegate:self];
if ([self respondsToSelector:#selector(presentViewController:animated:completion:)])
{
[imagePicker setShowsCameraControls:NO];
[self presentViewController:imagePicker animated:YES completion:^{
[imagePicker setShowsCameraControls:YES];
}];
} else
{
[self presentViewController:imagePicker animated:YES completion:nil];
}
}
else
{
UIAlertView *alertCamera = [[UIAlertView alloc]initWithTitle:ALRT message:#"Camera doesn't Support for this Device" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertCamera show];
}
}];
otherAction = [UIAlertAction actionWithTitle:#"Photo library" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
{
UIImagePickerController *picker;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
[self presentViewController:picker animated:YES completion:nil];
}];
[alertController addAction:destroyAction];
[alertController addAction:otherAction];
[alertController setModalPresentationStyle:UIModalPresentationPopover];
UIPopoverPresentationController *popPresenter = [alertController
popoverPresentationController];
popPresenter.sourceView = sender;
popPresenter.sourceRect = sender.bounds;
[self presentViewController:alertController animated:YES completion:nil];
});
The issue I am facing is, while disappearing UIPopoverPresentationController displays black out. Don't know what is wrong!

Related

How to Implement Delete Account functionality with objective c in xcode

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertController *alert = [[UIAlertView alloc] initWithTitle:#"Alert"
message:#"Are you sure you want to Delete your account ?"
delegate:self
cancelButtonTitle:#"Delete Account"
otherButtonTitles:#"Cancel", nil];
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Do Some action here
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"logged_in"];
// alert.tag = 104;
// [alert show];
[self displayAlertForRemoveAccountWithMessage:#"Are you sure you want to Delete your account ?"];
UINavigationController *nav = [self.storyboard instantiateViewControllerWithIdentifier:#"stratingNav"];
[nav setModalPresentationStyle: UIModalPresentationOverFullScreen];
[self presentViewController:nav animated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
}
- (void)displayAlertForRemoveAccountWithMessage :(NSString *)message
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert"
message:message
delegate:self
cancelButtonTitle:#"Delete Account"
otherButtonTitles:#"Cancel", nil];
alert.tag = 103;
[alert show];
}
i am trying to implement delete account function with objective c kindly please tell me what else need to be added in this code..

Create action when I click on a button than UIAlertcontroller

i Create two buttons of UIAlertcontroller:
One Button - "OpenCamera"
Two button - "OpenGallery"
I just can not understand how I create action when I click on one of them.
- (IBAction)takePic:(id)sender {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet]; // 1
UIAlertAction *openCamrea = [UIAlertAction actionWithTitle:#"open camrea"
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
}];
UIAlertAction *openGallery = [UIAlertAction actionWithTitle:#"open gallery"
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
}];
[alert addAction:openCamrea];
[alert addAction:openGallery];
[self presentViewController:alert animated:YES completion:nil];
}
The handler is the block to be executed on the selection of the item.
UIAlertAction *openGallery = [UIAlertAction
actionWithTitle:#"open gallery"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
// Code to run when the open gallery option is pressed.
}];
BTW I think the long unbroken lines in the question really don't help as they effectively hide the key parameter.
The complete code:
- (IBAction)takePic:(id)sender {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *openCamrea = [UIAlertAction actionWithTitle:#"open camrea"
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Error"message:#"Device has no camera"delegate:nil cancelButtonTitle:#"OK"otherButtonTitles: nil];
[myAlertView show];
}
else
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
}];
UIAlertAction *openGallery = [UIAlertAction actionWithTitle:#"open gallery"
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}];
[alert addAction:openCamrea];
[alert addAction:openGallery];
[self presentViewController:alert animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.img.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Put your code inside the handler blocks you are passing to [UIAlertAction actionWithTitle:style:handler:]
For example:
UIAlertAction *openCamrea = [UIAlertAction actionWithTitle:#"open camrea"
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
// openCamera action code goes here
}];
UIAlertAction *openGallery = [UIAlertAction actionWithTitle:#"open gallery"
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
// openGallery action code goes here
}];

How to fix default popup menu using uiactivityviewcontroller

I have problem, my app using default uiactivityviewcontroller, but it too big on iphone 6+. How to fix it ?
Button in my app too big
Button other app
My code
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
[self presentViewController:mail animated:YES completion:NULL];
UIAlertController *actionSheet= [UIAlertController
alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *actionSheetDeleteDraft = [UIAlertAction
actionWithTitle:#"Delete Draft"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSLog(#"Delete is pressed");
}];
UIAlertAction *actionSheetSaveDraft = [UIAlertAction
actionWithTitle:#"Save Draft"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSLog(#"Save pressed");
}];
UIAlertAction *actionSheetCancel = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
NSLog(#"Cancel pressed");
}];
[actionSheet addAction:actionSheetDeleteDraft];
[actionSheet addAction:actionSheetSaveDraft];
[actionSheet addAction:actionSheetCancel];
[self presentViewController:actionSheet animated:YES completion:nil];

UIActivityViewController completion handler shows 'completed' even if user wasn't able to log into Tumblr or other app

I'm using completionWithItemsHandler to determine if selectedItems were posted to Tumblr. I want to present a 'Success!' alert if the post was complete but completionHandler returns 'completed' even if the user wasn't able to log into Tumblr. That is,user goes through the steps of selecting items, opening activity view, and selecting Tumblr --> this opens the Tumblr app and asks for a log in. If the user presses 'cancel' and goes back to my app, the completionHandler says the activity was 'completed'. Is there any way to determine if post was successful? Any way to test if the user was able to log into the app? Any suggestions for dealing with 'success' alerts when there can be these types of problems?
Here is the completion Handler
activityController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *error){
if (error)
{
//NSError *error;
alertController = [UIAlertController alertControllerWithTitle:#"Error" message:#"Error sharing items" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *errorAction = [UIAlertAction actionWithTitle:NSLocalizedString(#"OK", #"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
NSLog(#"Error sharing items: %#", error);
}];
[alertController addAction:errorAction];
[self presentViewController:alertController animated:YES completion:nil];
}
else if (completed)
{
[self.activityIndicator stopAnimating];
if ([self.giftID isEqualToString:#"general"]||[self.giftEntity.giftThanked isEqualToString:#"YES"])
{
alertController = [UIAlertController alertControllerWithTitle:alertTitle message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(#"OK", #"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
// NSLog(#"OK action");
[self dismissViewControllerAnimated:YES completion:nil];
}];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
}
if (![self.giftID isEqualToString:#"general"]&&[self.giftEntity.giftThanked isEqualToString:#"NO"])
{
alertController = [UIAlertController alertControllerWithTitle:alertTitle message:#"Mark gift as 'Thanked'?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *yesAction = [UIAlertAction actionWithTitle:NSLocalizedString(#"YES", #"yes action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
self.giftEntity.giftThanked = #"YES";
[self.giftBusObj saveEntities];
[self dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *noAction = [UIAlertAction actionWithTitle:NSLocalizedString(#"NO", #"no action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
self.giftEntity.giftThanked = #"NO";
[self.giftBusObj saveEntities];
[self dismissViewControllerAnimated:YES completion:nil];
}];
[alertController addAction:noAction];
[alertController addAction:yesAction];
[self presentViewController:alertController animated:YES completion:nil];
}
//Deselect all items in stvc
self.textShare = 0;
self.cardButton.selected = NO;
self.textImageBackground.backgroundColor = [UIColor clearColor];
self.cardImageBackground.backgroundColor = [UIColor clearColor];
[spcvc selectPhotosNone];
[self viewDidLoad];
}
else
{
[self dismissViewControllerAnimated:YES completion:nil];
NSLog(#"activity controller - not 'completed' nor 'error'" );
}
};

MFMailComposeViewController: issues with UITextField and UIAlertController after dismissal

I have two issues after usage of MFMailComposeViewController in my application:
After the MFMailComposeViewController is dismissed, the UITextField text on my initial ViewController is cleared
When attempting to display a UIAlertController to indicate a MFMailComposeResult, it fails to display giving a error in the log.
For issue 1: This is my MFMailComposeViewController code. It is an action of a UIAlertController (with UIAlertControllerStyleActionSheet).
UIAlertAction *email = [UIAlertAction
actionWithTitle:#"Email the link"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
// composes new mail message with link
NSString *messageBody = self.link.text;
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setMessageBody:messageBody isHTML:NO];
// customises UI to match application
[[mc navigationBar] setTintColor:[UIColor whiteColor]];
[self presentViewController:mc animated:YES completion:^{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[self setNeedsStatusBarAppearanceUpdate];
}];
[view dismissViewControllerAnimated:YES completion:nil];
}];
For issue 2: this is my code for displaying an appropriate UIAlertController for each relevant MFMailComposeResult.
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:{
UIAlertController *alert= [UIAlertController
alertControllerWithTitle:#"SearchMe"
message:#"The composition of this email has been cancelled."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *done = [UIAlertAction
actionWithTitle:#"Done"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:done];
[self presentViewController:alert animated:YES completion:nil];
[alert dismissViewControllerAnimated:YES completion:nil];
}
break;
case MFMailComposeResultSaved:{
UIAlertController *alert= [UIAlertController
alertControllerWithTitle:#"SearchMe"
message:#"The composition of this email has saved as a draft."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *done = [UIAlertAction
actionWithTitle:#"Done"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:done];
[self presentViewController:alert animated:YES completion:nil];
[alert dismissViewControllerAnimated:YES completion:nil];
}
break;
case MFMailComposeResultSent:{
UIAlertController *alert= [UIAlertController
alertControllerWithTitle:#"SearchMe"
message:#"The composition of this email has been sent."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *done = [UIAlertAction
actionWithTitle:#"Done"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:done];
[self presentViewController:alert animated:YES completion:nil];
[alert dismissViewControllerAnimated:YES completion:nil];
}
break;
case MFMailComposeResultFailed:
{
UIAlertController *alert= [UIAlertController
alertControllerWithTitle:#"SearchMe"
message:#"The composition of this email has failed. Please try again."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *done = [UIAlertAction
actionWithTitle:#"Done"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:done];
[self presentViewController:alert animated:YES completion:nil];
[alert dismissViewControllerAnimated:YES completion:nil];
}
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
The error given in the log is this:
Warning: Attempt to present <UIAlertController: 0x14ed58b90> on <ViewController: 0x14ee1f7f0> whose view is not in the window hierarchy!

Resources