How to use local push notification in cocos2d-x - cocos2d-x

I am making a game using cocod2d-x, i am a beginner so i don't know how to push local notification in my game.
Can you help me resolve this problem?
Thanks all.

to set a local notification in Ios, you have to edit applicationDidEnterBackground method in your appcontroller.mm class.. take a look at the code below...
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date
//NSDate *pickerDate = [self.datePicker date];
NSDate *now = [NSDate date];
//NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:now];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:now];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
[dateComps setMinute:[timeComponents minute]+_notificationTimeMinutes];
[dateComps setSecond:([timeComponents second]+_notificationTimeSeconds)];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody=#"Your local notification message";
// Set the action button
localNotif.alertAction = #"View";
NSLog(#"setAlarm Called : %#",itemDate);
//localNotif.hasAction = NO;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber+1;
localNotif.repeatInterval = NSWeekCalendarUnit;
//[UIApplication sharedApplication].applicationIconBadgeNumber += 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"someValue" forKey:#"someKey"];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
In above code _notificationTimeSeconds is in seconds and _notificationTimeMinutes is in minutes similarly you can add hours and days or months after which you want your notification to appear.. you add your message in "your local notification message"

Related

export data in html table format in ios7 and make email body

I am new in iphone application development. In my application i am using two different NSMutable array's. Both have dictionary with 4 fields like name, address etc at each index. Now my problem is I want to export that data in HTML table format and make email body. So please, can any one help me.
thank you
For example items are:
NSMutableArray *arrItem = [NSMutableArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:#"sam",#"name",#"london",#"address",nil], nil];
Now create html string like this :
NSString *strHTML = #"<!DOCTYPE html>"
#"<html><body>"
#"<table border=\"1\" style=\"width:300px\">"; //change width according to your requirement
for (NSDictionary *dict in arrItem) {
NSString *strName = [dict objectForKey:#"name"];
NSString *strAddress = [dict objectForKey:#"address"];
strHTML = [strHTML stringByAppendingString:#"<tr>"];
//add table column value
strHTML = [strHTML stringByAppendingString:[NSString stringWithFormat:#"<td>%#</td>",strName]];
strHTML = [strHTML stringByAppendingString:[NSString stringWithFormat:#"<td>%#</td>",strAddress]];
strHTML = [strHTML stringByAppendingString:#"</tr>"];
}
strHTML = [strHTML stringByAppendingString:#"</table>"
#"</body>"
#"</html>"];
Use hmtl string in MailcompserViewController :
[controller setMessageBody:strHTML isHTML:YES];
EDIT : formatted
MailcompserViewController *controller = [[MailcompserViewController alloc] init];//Allocating mailComposer
NSString *eMailBody=#""; //Value for row
for (int i=0; i<[arrMailList count]; i++) { //Get all the row value
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
MailData *data = [arrMailList objectAtIndex:i]; //Get array value in NSObject class
NSDate *date = [dateFormat dateFromString:data.date];
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:#"EEEE, dd MMMM yyyy"]; //Date NSDateFormatter
NSString *strDate = [dateFormat1 stringFromDate:date];
NSString *numbers = [data.setName stringByTrimmingCharactersInSet:[NSCharacterSet letterCharacterSet]];
eMailBody = [eMailBody stringByAppendingFormat:#"<tr><td width=\"200px\">%#</td><td >%#</td><td >%#</td><td >%#</td><td>%#</td><td >%#</td></tr>",strDate,data.Name,data.email,data.address,data.phoneNumber];//Create row
}
NSString *html = [NSString stringWithFormat:#"<html><style>body{font-family:Helvetica;font-size:14px;}th{fontcolor:#ffffff;font-size:16px;font-weight:BOLD;}</style><body><table cellpadding=5px border=1px bordercolor=#000000><tr bgcolor=#cccccc><th width=200>Date</th><th>Workout Name</th><th>Excercise Name</th><th>Set</th><th>Weight</th><th>Reps</th>%#</table></body></html>",eMailBody]; //Add row in table view
[controller setMessageBody:html isHTML:YES];
[self presentViewController:controller animated:YES completion:nil];
var str = "\n"
str=str.stringByAppendingString("<tr><th>")
str=str.stringByAppendingString("Task")
str=str.stringByAppendingString("</th>")
str=str.stringByAppendingString("<th>")
str=str.stringByAppendingString("SpareParts")
str=str.stringByAppendingString("</th>")
str=str.stringByAppendingString("<th>")
str=str.stringByAppendingString("Labour")
str=str.stringByAppendingString("</th></tr>\n")
var i:Int=0
for i=0;i<carImageList.count;i++
{
str=str.stringByAppendingString("<tr><td>")
str=str.stringByAppendingString(carImageList[i] as! String)
str=str.stringByAppendingString("</td>")
str=str.stringByAppendingString("<td>")
str=str.stringByAppendingString(carImageList[i] as! String)
str=str.stringByAppendingString("</td>")
str=str.stringByAppendingString("<td>")
str=str.stringByAppendingString(carImageList[i] as! String)
str=str.stringByAppendingString("</td></tr>\n")
}
//SubTotal
str=str.stringByAppendingString("<tr><td>")
str=str.stringByAppendingString("Sub Total")
str=str.stringByAppendingString("</td>")
str=str.stringByAppendingString("<td>")
str=str.stringByAppendingString("100")
str=str.stringByAppendingString("</td>")
str=str.stringByAppendingString("<td>")
str=str.stringByAppendingString("100")
str=str.stringByAppendingString("</td></tr>\n")
//Total
str=str.stringByAppendingString("<tr><td>")
str=str.stringByAppendingString("Total")
str=str.stringByAppendingString("</td>")
str=str.stringByAppendingString("<td colSpan='2'>")
str=str.stringByAppendingString("300")
str=str.stringByAppendingString("</td></tr>\n")
str=str.stringByAppendingString("</table></body></html>\n")
NSLog("TEST %#",str)
mailComposerVC.setMessageBody(str, isHTML: true)

Sending base64 UIImages comes out empty

I'm trying to send an HTML email with embedded UIImages.
This is my code:
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
[mailComposer setSubject:NSLocalizedString(#"Subject", #"Default Subject")];
// Build email body (HTML)
UIImage *emailImage = [UIImage imageNamed:#"image.png"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(emailImage)];
NSString *base64String = [imageData base64EncodedString];
NSMutableString *emailBody = [[NSMutableString alloc] initWithString:#"<HTML>Hey,<br><br>Here's the image!"];
[emailBody appendString:[NSString stringWithFormat:#"<img src='data:image/png;base64,%#'></html>",base64String]];
[mailComposer setMessageBody:emailBody isHTML:YES];
[delegate.viewController presentModalViewController:mailComposer animated:YES];
When the Email Modal View Controller comes up I see the images just fine. But when I tried to view this email on my Mac and on my iPhone and the images comes out empty.

Three20 Navigation: View don't show

I rework my app to use Three20 and I wanna use Three20 navigation now.
Here is my code, that works perfectly before:
ENSListViewController *vc = [ENSListViewController alloc];
NSArray *ensArray;
NSDictionary *dic;
NSInteger folder_id;
NSString* folder_type;
NSString* barTitle;
NSString* folderName;
if (indexPath.section == 0)
{
ensArray = [ensFolderList objectForKey:#"an"];
dic = [ensArray objectAtIndex:indexPath.row];
folder_type = #"an";
barTitle = [NSString stringWithFormat:#"%#", [dic objectForKey:#"name"]];
folder_id = [[dic objectForKey:#"ordner_id"] intValue];
folderName = [dic objectForKey:#"name"];
}
else
{
ensArray = [ensFolderList objectForKey:#"von"];
dic = [ensArray objectAtIndex:indexPath.row];
folder_type = #"von";
barTitle = [NSString stringWithFormat:#"%#", [dic objectForKey:#"name"]];
folder_id = [[dic objectForKey:#"ordner_id"] intValue];
folderName = [dic objectForKey:#"name"];
}
vc.folder_id = folder_id;
vc.folder_type = folder_type;
vc.barTitle = barTitle;
vc.folderName = folderName;
[vc initWithNibName:#"ENSListViewController" bundle:nil];
[self.view addSubview:vc.view];
It works perfectly.
It allocs a ViewController, sets a lot of data in the ViewController (Properties) and then show the view.
Here is my code now:
NSArray *ensArray;
NSDictionary *dic;
NSInteger folder_id;
NSString* folder_type;
NSString* barTitle;
NSString* folderName;
if (indexPath.section == 0)
{
ensArray = [ensFolderList objectForKey:#"an"];
dic = [ensArray objectAtIndex:indexPath.row];
folder_type = #"an";
barTitle = [NSString stringWithFormat:#"%#", [dic objectForKey:#"name"]];
folder_id = [[dic objectForKey:#"ordner_id"] intValue];
folderName = [dic objectForKey:#"name"];
}
else
{
ensArray = [ensFolderList objectForKey:#"von"];
dic = [ensArray objectAtIndex:indexPath.row];
folder_type = #"von";
barTitle = [NSString stringWithFormat:#"%#", [dic objectForKey:#"name"]];
folder_id = [[dic objectForKey:#"ordner_id"] intValue];
folderName = [dic objectForKey:#"name"];
}
/*
vc.folder_id = folder_id;
vc.folder_type = folder_type;
vc.barTitle = barTitle;
vc.folderName = folderName;
[vc initWithNibName:#"ENSListViewController" bundle:nil];
//[self.view addSubview:vc.view];
*/
NSString *url = [NSString stringWithFormat:#"tt://ensList/%#/%#/%d/%#/%#/%#", #"ENSListViewController", nil, folder_id, folder_type, barTitle, folderName];
TTURLAction *action = [TTURLAction actionWithURLPath:url];
[[TTNavigator navigator] openURLAction:action];
Here is my Navigator:
navigator = [TTNavigator navigator]; // create the navigator
navigator.persistenceMode = TTNavigatorPersistenceModeAll; // and he will save the data :)
TTURLMap* map = navigator.URLMap;
[map from: #"tt://ens"
toSharedViewController: [ENSOverviewViewController class]];
[map from: #"tt://ensList/(initWithNibName:)/(bundle:)/(folderId:)/(folderType:)/(barTitle:)/(folderName:)" toViewController:[ENSListViewController class]
transition:3];
And here is my new Constructor method:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self LoadENSList];
}
return self;
}
- (void) initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil folderId:(NSInteger)folder_id2 folderType:(NSString*)folder_type2 barTitle:(NSString*)barTitle2 folderName:(NSString*)folderName2
{
self.folder_id = folder_id2;
self.folder_type = folder_type2;
self.barTitle = barTitle2;
self.folderName = folderName2;
[self initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
}
So, if you read it up to here: big thanks!
Now my problem is: The view doesn't open. Nothing happens.
I think there is a mistake in my self made constructor, the order of calling my constructor or something like this. Im on it since 2 hours but can't find the error.
I know Three20 is much undocumented and I am not expacting a fast answer, but if anyone have an idea: please comment or answer.
Found the solution:
1) I forget the return-value in my constructur.
After adding this (changing (void) to (id) and add "return self") it goes on...
2) After he changes in 1) the system crashes because initWithNibName throws an NSInvalidArgument error.
After changing this to init, it works perfectly

CABasicAnimation - animationDidStop not called

I am making a CABasicAnimation and the problem is that the animationDidStop delegate method does not get called. I am not sure why but hopefully someone knows.
Here is my animation:
CABasicAnimation *theAnimation;
theAnimation = [CABasicAnimation animationWithKeyPath: #"transform.translation.x"];
theAnimation.duration = 54;
//theAnimation.repeatCount = 6;
theAnimation.autoreverses = YES;
theAnimation.removedOnCompletion = YES;
theAnimation.fromValue = [NSNumber numberWithFloat: (self.myImageView.frame.size.height / 5) + (self.view.frame.size.height - self.myImageView.center.x)];
theAnimation.toValue = [NSNumber numberWithFloat: 6.0 - (self.myImageView.frame.origin.y + self.myImageView.frame.size.height)];
//[self.myImageView.layer addAnimation:theAnimation forKey: #"animateLayer"];
[theAnimation setValue:#"Animation1" forKey:#"animateLayer"];
[[self.myImageView layer] addAnimation:theAnimation forKey:nil];
Also here is my animationDidStop method:
- (void)animationDidStop:(CABasicAnimation *)theAnimation finished:(BOOL)flag {
NSString* value = [theAnimation valueForKey:#"animateLayer"];
if ([value isEqualToString:#"Animation1"])
{
[self themethod];
return;
}
if ([value isEqualToString:#"Animation2"])
{
[self themethod];
return;
}
}
Does anyone knows why this is happening?
You need to set delegate to call animationDidStop.
theAnimation.delegate = class (self)

MFMailComposeViewController csv attachment not being attached, but showing inline instead

I am having a problem with sending csv attachments via MFMailComposeViewController.
Sometimes they come through just fine, but for other users they don't come through as attachments, but rather as text inline in the email (with <br/> instead of line returns.) It's very strange. Anybody know what I'm doing wrong?
Here is a snippet of my code:
MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
mailComposeViewController.mailComposeDelegate = self;
NSString *csv = #"foo,bar,blah,hello";
NSData *csvData = [csv dataUsingEncoding:NSUTF8StringEncoding];
[mailComposeViewController addAttachmentData:csvData mimeType:#"text/csv" fileName:#"testing.csv"];
[mailComposeViewController setSubject:#"testing sending csv attachment"];
[mailComposeViewController setMessageBody:#"csv file should be attached" isHTML:NO];
[self presentModalViewController:mailComposeViewController animated:YES];
-(IBAction)btnPressed:(id)sender {
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docDir = [arrayPaths objectAtIndex:0];
NSString *Path = [docDir stringByAppendingString:#"/CSVFile.csv"];
NSData *csvData = [NSData dataWithContentsOfFile:Path];
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:#"For csv file..."];
[controller setMessageBody:#"...csv file is hear.." isHTML:NO];
[controller addAttachmentData:csvData mimeType:#"text/csv" fileName:#"CSVFile.csv"];
[self presentModalViewController:controller animated:YES];
[controller release];
}
Hi I put sample code for Creating CSV file and attach it with mail but make sure you have to add MessageUI.Framework and import its related header "MessageUI/MessageUI.h"
"MessageUI/MFMailComposeViewController.h" and deligate "MFMailComposeViewControllerDelegate"...I hope this wl useful for others
- (void)viewDidLoad {
arrCsv=[[NSArray alloc]initWithObjects:#"Hello",#"Hi",#"traun",#"fine",nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:#"%#/try.csv", documentsDirectory];
[[arrCsv componentsJoinedByString:#","] writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:NULL];
}
-(ibAction)btnMail {
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docDir = [arrayPaths objectAtIndex:0];
NSString *Path = [docDir stringByAppendingString:#"/CSVFile.csv"];
NSData *csvData = [NSData dataWithContentsOfFile:Path];
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:#"For csv file..."];
[controller setMessageBody:#"...csv file is hear.." isHTML:NO];
[controller addAttachmentData:csvData mimeType:#"text/csv" fileName:#"CSVFile.csv"];
[self presentModalViewController:controller animated:YES];
[controller release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{ message.hidden = NO;
switch (result)
{
case MFMailComposeResultCancelled:
message.text = #"Result: canceled";
break;
case MFMailComposeResultSaved:
message.text = #"Result: saved";
break;
case MFMailComposeResultSent:
message.text = #"Result: sent";
break;
case MFMailComposeResultFailed:
message.text = #"Result: failed";
break;
default:
message.text = #"Result: not sent";
break;
}
[self dismissModalViewControllerAnimated:YES];
}
set the mime type as "application/octet-stream" and that should do the trick to remove inline attachments (I still named the extension of my file i.e. pdf)
I believe the second parameter to setMessageBody:isHTML: must be YES for attachments to not show up inline.
Even if you set isHTML param to YES, your message body can be sent as plain/text if the message body can be represented as such. And attachments in plain/text messages are not always recognized correctly by some email clients (Outlook).
In my case adding a link in the message body helped. Formatting text as bold with HTML tags works too. Tricky!
Tested on iPod 1G 3.1.3.
This may not be the case here, but one thing to watch out for is that:
[NSString dataUsingEncoding:]
returns a valid but empty NSData object if the conversion to the specified encoding is not possible. Better to use the full version:
[NSString dataUsingEncoding: s allowLossyConversion: YES]
Or check the length of the returned data. It appears that zero-length data attachments are trimmed somewhere in the mail process.