-->

UIApperance and various crashes

2019-02-15 22:12发布

问题:

I'm getting so frustrated while customizing my app. I've already created and styled almost the whole app, including Navigation bar, toolbar, tabBar etc, but everytime a MFMailComposeViewController, a MFMessageComposerViewController, Twitter or Facebook sharers or even a QuickLook View Controller comes in play, the app crashes with the message:

*** Assertion failure in -[UICGColor encodeWithCoder:].
*** Terminating app due to uncaught exception 'NSInternalInconsistencyExceptionì, reason: 'Only RGBA or White color spaces are supported in this situation.'

I've read around that this is because iOS 6 manages the composers as Remote Controllers, but I really haven't any idea how to get a fix to this problem.

I don't want to remove the mail composing features or the message composing features because of this.

Anyone encountered this bug too?

I already have the code wrote. The problem is that UIAppearance is making the app crash because of the custom UINavigationBars elements. Code.

-(void)message{
    if (_progressHUD){
        [_progressHUD hide:YES];
    }
    MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init] ;
    [controller setMessageComposeDelegate:self];

    if([MFMessageComposeViewController canSendText])
    {
        controller.body = descriptionString;
        controller.recipients = nil;
        [self presentViewController:controller animated:YES completion:nil];
    }

}

-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)email {

    if (_progressHUD){
        [_progressHUD hide:YES];
    }

    MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
    [composer setMailComposeDelegate:self];
    if ([MFMailComposeViewController canSendMail]) {
        [composer setToRecipients:nil];
        [composer setSubject:[NSString stringWithFormat:@"%@",nameString]];

        [composer setMessageBody:[NSString stringWithFormat:@"%@",descriptionString] isHTML:NO];        [composer addAttachmentData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageString]] mimeType:@"png" fileName:imageString];
        [composer setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
        [self presentViewController:composer animated:YES completion:nil];
    }
}


-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    if (error) {
        SIAlertView *alert = [[SIAlertView alloc] initWithTitle:@"Error"
                                                        andMessage:[NSString stringWithFormat:@"Error %@", [error description]]];
        [alert addButtonWithTitle:@"OK" type:SIAlertViewButtonTypeDestructive handler:^(SIAlertView *alertView){}];
        [alert show];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

Appearance

- (void)customizeAppearance
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

    UINavigationBar Appearance
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBarBackground"] forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setTitleTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys:
      [UIColor whiteColor],
      UITextAttributeTextColor,
      [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0],
      UITextAttributeTextShadowColor,
      [NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
      UITextAttributeTextShadowOffset,
      [UIFont fontWithName:@"HelveticaNeue" size:0.0],
      UITextAttributeFont,
      nil]];

    //ToolBar Appearance
    [[UIToolbar appearance] setTintColor:[UIColor whiteColor]];


    //Switch Appearance
    [[UISwitch appearance] setOnTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"greenBackground"]]];

    //Search Bar Appearance
    [[UISearchBar appearance] setTintColor:[UIColor whiteColor]];

    //Tab Bar Appearance
    [[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"tabBarBackground"]];
    [[UITabBar appearance] setTintColor:[UIColor whiteColor]];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal];
    [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"transparent"]];

}

回答1:

After various debugging session, I sorted out that the line of code that was giving me those crashes was

//Switch Appearance
[[UISwitch appearance] setOnTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"greenBackground"]]];

So I deleted it and everything was working fine. Apparently, I can't set the appearance directly in the AppDelegate, but I had to do it in the class where the switch was in.

Same thing for the refreshControl appearance: had to set it in the tableView's class.



回答2:

You need to add necessary frameworks for twitter, facebook.
For email do following :

Add MessageUI.framework to your project

In your .h file

#import <MessageUI/MessageUI.h>

@interface CustomController : UIViewController<MFMailComposeViewControllerDelegate>

In your .m file

- (IBAction)actionEmail:(id)sender
{
NSLog(@"actionEmail Called");

MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[self presentViewController:mc animated:YES completion:NULL];


}

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
    case MFMailComposeResultCancelled:
        NSLog(@"Mail cancelled");
        break;
    case MFMailComposeResultSaved:
        NSLog(@"Mail saved");
        break;
    case MFMailComposeResultSent:
        NSLog(@"Mail sent");
        break;
    case MFMailComposeResultFailed:
        NSLog(@"Mail sent failure: %@", [error localizedDescription]);
        break;
    default:
        break;
}

// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}


回答3:

In your .h file add delegate <MFMessageComposeViewControllerDelegate,MFMailComposeViewControllerDelegate>

In your .m file add following

 -(void)ShareByEmail:(NSString *)strEmail {
        MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
        mail.mailComposeDelegate = self;
        if ([MFMailComposeViewController canSendMail]) {

            NSString *bodyData = @"Boy data place here";


                NSString *strRecipients = [NSString stringWithFormat:@"%@",strEmail];
                strRecipients = [strRecipients stringByReplacingOccurrencesOfString:@"mailto:" withString:@""];
                NSArray * arrayRecipients = [strRecipients componentsSeparatedByString:@""];

                [mail setToRecipients:arrayRecipients];

            [mail setSubject:@"Subject"];
            [mail setMessageBody:bodyData isHTML:NO];
            [self presentViewController:mail animated:YES completion:nil];
        }
        mail = nil;
        return NSLog(@"%@",strEmail);
    }

-(void)shareBySMS:(NSString *)strSMS {
   if([strSMS length] > 0) {
            MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
            if(picker) {
                picker.messageComposeDelegate = self;

                    picker.recipients = @"Youre Recipients";
                    //picker.recipients = [NSArray arrayWithObject:tel];
                    picker.body = strSMS;

                [self presentViewController:picker animated:NO completion:nil];
                picker = nil;
            }
            NSLog(@"SMS fired");
        }
}


#pragma mark Mail Composer Delegate Methods

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {

    // Notifies users about errors associated with the interface
    switch (result) {
        case MFMailComposeResultCancelled:
            break;
        case MFMailComposeResultSaved:
            break;
        case MFMailComposeResultSent:
            break;
        case MFMailComposeResultFailed:
            break;
        default:
            break;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

    - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
        switch (result) {
            case MessageComposeResultCancelled:
                NSLog(@"Result: canceled");
                break;
            case MessageComposeResultSent:
                NSLog(@"Result: sent");
                break;
            case MessageComposeResultFailed:
                NSLog(@"Result: failed");
                break;
            default:
                NSLog(@"Result: not sent");
                break;
        }
        [self dismissViewControllerAnimated:YES completion:nil];
    }