I have the concept of sharing to contacts in my App and used MFMessageComposeViewController.
-(IBAction)btnAddClicked:(id)sender {
#try {
selections = [[NSMutableArray alloc] init];
for(NSIndexPath *indexPath in arSelectedRows) {
NSMutableDictionary *searchable = [[NSMutableDictionary alloc
]init];
[searchable setObject:[[contactsArray objectAtIndex:indexPath.row]objectForKey:#"Phone"]forKey:#"Phone"];
[selections addObject:searchable];
}
if([selections count]>0)
{
NSString *temp1=#"";
for(int i=0;i<[selections count];i++)
{
toRecepients=[[selections objectAtIndex:i]objectForKey:#"Phone"];
temp1=[temp1 stringByAppendingString:toRecepients];
temp1=[temp1 stringByAppendingString:#","];
}
temp1 = [temp1 substringToIndex:[temp1 length]-1];
if(![MFMessageComposeViewController canSendText]) {
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Your device doesn't support SMS!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
return;
}
NSArray *recipents = [temp1 componentsSeparatedByString:#","];
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
messageController.navigationBar.topItem.leftBarButtonItem.title = #"Cancel";
[messageController setRecipients:recipents];
[messageController setBody:self.message];
[self presentModalViewController:messageController animated:YES];
}
else{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Select the contacts you would like to share to" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
return;
}
}
#catch (NSException *exception) {
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
{
ErrorView *errorView=[[ErrorView alloc]initWithNibName:#"ErrorView" bundle:nil];
if([[[UIDevice currentDevice]systemVersion]floatValue]<5.0)
{
[self presentModalViewController:errorView animated:YES];
}
else
{
[self presentViewController:errorView animated:YES completion:nil];
}
[errorView release];
}
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) {
ErrorView *errorView=[[ErrorView alloc]initWithNibName:#"ErrorView~iPad" bundle:nil];
if([[[UIDevice currentDevice]systemVersion]floatValue]<5.0)
{
[self presentModalViewController:errorView animated:YES];
}
else
{
[self presentViewController:errorView animated:YES completion:nil];
}
[errorView release];
}
} }
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result {
switch (result) {
case MessageComposeResultCancelled:
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Failed to send SMS!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
break;
}
case MessageComposeResultFailed:
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Failed to send SMS!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
break;
}
case MessageComposeResultSent:
{
NSString *messa=[NSString stringWithFormat:#"Shared to %lu contact(s)",(unsigned long)[selections count]];
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Succesful" message:messa delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
break;
}
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
This is the code Im using, which crashes sometimes and displays the error
Assertion failed: (result == KERN_SUCCESS), function +[XPCMachSendRight wrapSendRight:], file /SourceCache/XPCObjects/XPCObjects-46/XPCMachSendRight.m, line 27.
I have put breakpoint to debug, but xcode doesn't showup where the error is produced.
Any ideas/ suggestions would be appreciable..
Enable zombie Objects to find out the line of actual crash.
You can enable Zombie by following steps:
1.Select you project scheme and chose edit scheme.
2.A window will appear, now select diagnostics.
3.Select check mark for enable Zombie objects.
Now run your project.
I am receiving this error when trying to post an image to Twitter using the following code, cannot think what I am doing wrong any ideas for how to solve?;
- (void)PostToTwitter
{
UIImage* pxImage = [[self GetResultImage] retain];
TWTweetComposeViewController *twitter = [[[TWTweetComposeViewController alloc] init] autorelease];
[twitter setInitialText:#"Twitter Pic"];
[twitter addImage:pxImage];
FaceAppDelegate* app = (FaceAppDelegate*)[[UIApplication sharedApplication] delegate];
[app.mainCtrl presentViewController:twitter animated:YES completion:NULL];
twitter.completionHandler = ^(TWTweetComposeViewControllerResult res)
{
if(res == TWTweetComposeViewControllerResultDone)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Success" message:#"You have posted successfully." delegate:self cancelButtonTitle:#"Close" otherButtonTitles: nil];
[alert show];
[alert release];
}
else if(res == TWTweetComposeViewControllerResultCancelled)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Cancelled" message:#"You have cancelled posting to Twitter." delegate:NULL cancelButtonTitle:#"Close" otherButtonTitles: nil];
[alert show];
[alert release];
}
[app.mainCtrl dismissModalViewControllerAnimated:YES];
};
[pxImage release];
}
I think, that your view isn't presented. Try to use app.window.rootViewController, not property mainContr.
In my application ,i having 4 UIbuttons .On click each button showing same alert view contains learn and play option,while clicking alert view, want to show the different view depends on the uibutton selection.Here my code for alert view. please help me to perform diffrent view on click option.
-(IBAction)animals
{
UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:#"" message:#"" delegate:self cancelButtonTitle:nil otherButtonTitles:#"LEARN",#"PLAY",#"CANCEL",nil];
[alert1 show];
}
-(IBAction)birds
{
UIAlertView *alert2=[[UIAlertView alloc]initWithTitle:#"" message:#"" delegate:self cancelButtonTitle:nil otherButtonTitles:#"LEARN",#"PLAY",#"CANCEL",nil];
[alert2 show];
}
-(IBAction)direct
{
UIAlertView *alert3=[[UIAlertView alloc]initWithTitle:#"" message:#"" delegate:self cancelButtonTitle:nil otherButtonTitles:#"LEARN",#"PLAY",#"CANCEL",nil];
[alert3 show];
}
-(IBAction)fruit
{
UIAlertView *alert4=[[UIAlertView alloc]initWithTitle:#"" message:#"" delegate:self cancelButtonTitle:nil otherButtonTitles:#"LEARN",#"PLAY",#"CANCEL",nil];
[alert4 show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"LEARN"])
{
animallearn *aview=[[animallearn alloc]initWithNibName:#"animallearn" bundle:nil];
[self.navigationController pushViewController:aview animated:YES];
}
else if([buttonTitle isEqualToString:#"PLAY"])
{
birdslearn *bview=[[birdslearn alloc]initWithNibName:#"birdslearn" bundle:nil];
[self.navigationController pushViewController:bview animated:YES];
}
else
{
return;
}
}
-(IBAction)animals
{
UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:#"" message:#"" delegate:self cancelButtonTitle:nil otherButtonTitles:#"LEARN",#"PLAY",#"CANCEL",nil];
alert1.tag = 1;
[alert1 show];
}
-(IBAction)birds
{
UIAlertView *alert2=[[UIAlertView alloc]initWithTitle:#"" message:#"" delegate:self cancelButtonTitle:nil otherButtonTitles:#"LEARN",#"PLAY",#"CANCEL",nil];
alert2.tag = 2;
[alert2 show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if(alertView.tag == 1)
{
//animal alert
}
else if(alertView.tag == 2)
{
//birds alert
}
// and so on......
}
You have a tag attribute for UIAlertView. It's an int. You should assign your tag with different values.
-(IBAction)direct
{
UIAlertView *alert3=[[UIAlertView alloc]initWithTitle:#"" message:#"" delegate:self cancelButtonTitle:nil otherButtonTitles:#"LEARN",#"PLAY",#"CANCEL",nil];
[alert3 setTag:3];
[alert3 show];
}
And then clickedButtonAtIndex delegate method you should observe the tag of your current alert view:
if(alertView.tag == 3) //etc ...
And I don't know if you're using ARC but if not you must add an autorelease when you instantiate your alert view.
Declare your UIAlertView globally then you can compare them with UIAlertView delegates object like this-
UIAlertView *alert1;
UIAlertView *alert2;
-(IBAction)animals
{
alert1=[[UIAlertView alloc]initWithTitle:#"" message:#"" delegate:self cancelButtonTitle:nil otherButtonTitles:#"LEARN",#"PLAY",#"CANCEL",nil];
[alert1 show];
}
-(IBAction)birds
{
alert2=[[UIAlertView alloc]initWithTitle:#"" message:#"" delegate:self cancelButtonTitle:nil otherButtonTitles:#"LEARN",#"PLAY",#"CANCEL",nil];
[alert2 show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView ==alert1)
{
if (buttonIndex==1)
{
}
if(buttonIndex == 2)
{
}
}
if(alertView == alert2)
{
if (buttonIndex==1)
{
}
}
According to this you would not need tags :) Simply use the conditions
-(IBAction)animals
{
UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:#"" message:#"" delegate:self cancelButtonTitle:nil otherButtonTitles:#"LEARN",#"PLAY",#"CANCEL",nil];
[alert1 show];
[alert1 setTag:101];
}
-(IBAction)birds
{
UIAlertView *alert2=[[UIAlertView alloc]initWithTitle:#"" message:#"" delegate:self cancelButtonTitle:nil otherButtonTitles:#"LEARN",#"PLAY",#"CANCEL",nil];
[alert2 show];
[alert2 setTag:102];
}
-(IBAction)direct
{
UIAlertView *alert3=[[UIAlertView alloc]initWithTitle:#"" message:#"" delegate:self cancelButtonTitle:nil otherButtonTitles:#"LEARN",#"PLAY",#"CANCEL",nil];
[alert3 show];
[alert3 setTag:103];
}
-(IBAction)fruit
{
UIAlertView *alert4=[[UIAlertView alloc]initWithTitle:#"" message:#"" delegate:self cancelButtonTitle:nil otherButtonTitles:#"LEARN",#"PLAY",#"CANCEL",nil];
[alert4 show];
[alert4 setTag:104];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// learn has indextag = 0 ,play =1 cancel = 2
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if (alertView.tag == 101) {
switch (buttonIndex) {
case 0:
animallearn *aview=[[animallearn alloc]initWithNibName:#"animallearn" bundle:nil];
[self.navigationController pushViewController:aview animated:YES];
break;
case 1:
birdslearn *bview=[[birdslearn alloc]initWithNibName:#"birdslearn" bundle:nil];
[self.navigationController pushViewController:bview animated:YES];
break;
case 2:
return;
break;
default:
break;
}
}
if (alertView.tag == 102) {
switch (buttonIndex) {
case 0:
animallearn *aview=[[animallearn alloc]initWithNibName:#"animallearn" bundle:nil];
[self.navigationController pushViewController:aview animated:YES];
break;
case 1:
birdslearn *bview=[[birdslearn alloc]initWithNibName:#"birdslearn" bundle:nil];
[self.navigationController pushViewController:bview animated:YES];
break;
case 2:
return;
break;
default:
break;
}
}
// perform the same for tag = 103 and 104
}
How can I add an alert that displays instructions before game starts:
See code below:
- (void)viewDidLoad
{
[super viewDidLoad];
if (questions && configDictionary) {
[questionLabel setText:[[questions objectAtIndex:currentQuestonIndex] objectForKey:#"question"]];
NSArray *answers = [[questions objectAtIndex:currentQuestonIndex] objectForKey:#"answers"];
[answerLabel0 setText:[answers objectAtIndex:0]];
[answerLabel1 setText:[answers objectAtIndex:1]];
[answerLabel2 setText:[answers objectAtIndex:2]];
[answerLabel3 setText:[answers objectAtIndex:3]];
[pointsPerAnswerLabel setText:[NSString stringWithFormat:#"+%d points", [[configDictionary objectForKey:kPointsPerCorrectAnswer] intValue]]];
[currentQuestionNumberLabel setText:[NSString stringWithFormat:#"question %d", currentQuestonIndex+1]];
}
}
Use a UIAlertView:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Instructions"
message:#"Your Instructions..." delegate:self cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil, nil];
[alert show];
If you want to alert the user every time the app launches place it in the
- (void)applicationDidFinishLaunching:(UIApplication *)application {
}
Edit
You said you wanted to start the game after the dismiss button is pressed. So take advantage of the UIAlertView delegate:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0){
//Start your game!
}
}
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"How to play"
message:#"Answer the questions correctly to get points blablabla..."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
I try to send an e-mail from an app with an custom Content-Type.
Any ideas how to do this?
So i like to change
"Content-Type":
"multipart/mixed; boundary=Apple-Mail-1--209662383" --> "application/pkcs7-mime; name=smime.p7m; smime-type=enveloped-data"
and
"Content-Transfer-Encoding":
"7bit" --> "base64"
I used http://code.google.com/p/skpsmtpmessage/ for sending an custom mail with iOS.
(void)launchInAppMail {
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:#"In app email..."];
NSMutableString *messageString = [[NSMutableString alloc] init];
if (!(mNameField.text == nil || [mNameField.text isEqualToString:#""])) {
[messageString appendFormat:#"Name: %# \n",mNameField.text];
}
if (!(mPhoneNumberField.text == nil || [mPhoneNumberField.text isEqualToString:#""]))
{
[messageString appendFormat:#"Phone Number: %# \n",mPhoneNumberField.text];
}
if (!(mEmailField.text == nil || [mEmailField.text isEqualToString:#""])) {
[messageString appendFormat:#"Email ID: %# \n",mEmailField.text];
}
if (!(mTextView.text == nil || [mTextView.text isEqualToString:#""])) {
[messageString appendFormat:#"Desription: %# \n",mTextView.text];
}
if (!(mProblemNameButton.titleLabel.text == nil || [mProblemNameButton.titleLabel.text isEqualToString:#""])) {
[messageString appendFormat:#"Problem Name: %# \n",mProblemNameButton.titleLabel.text];
}
[messageString appendFormat:#"Address: %# \n", self.mAddress];
if (self.coordinateLoc)
[messageString appendFormat:#"Longitude: %f, Latitude: %f \n", self.coordinateLoc.coordinate.longitude, self.coordinateLoc.coordinate.latitude];
[controller setToRecipients:[NSArray arrayWithObject:#"service#emergencylocksmithleads.com"]];
[controller setMessageBody:messageString isHTML:NO];
[messageString release];
if (mImageView.image != nil) {
NSData *imageData = UIImageJPEGRepresentation(mImageView.image, 1);
[controller addAttachmentData:imageData mimeType:#"image/jpg" fileName:#"example.jpg"];
}
[self presentModalViewController:controller animated:YES];
[controller release];
}
pragma mark --------------------------------------------
pragma mark MFMailComposeViewController delegate Methods
(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError )error {
switch (result) {
case MFMailComposeResultCancelled:
NSLog(#"Mail send canceled.");
/
Execute your code for canceled event here ...
/
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved.");
/
Execute your code for email saved event here ...
*/
break;
case MFMailComposeResultSent: {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Mail Sent" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.tag = 1;
alert.delegate = self;
[alert show];
[alert release];
break;
}
case MFMailComposeResultFailed: {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Mail Sending Failed" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.tag = 2;
alert.delegate = self;
[alert show];
[alert release];
break;
}
default:
break;
}
[controller dismissModalViewControllerAnimated:YES];
}