Create ICS with objective C - objective-c

I am writing an app for students at my university. At this point I want to export the lessons in the schedule as an ics-file. I have wrote something that works with csv-files, but now I want the same for ics.
The problem is that iCal at my Mac doesn't want the in-app-created ics-files from this code:
-(NSURL*)getFileUrl
{
NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [docsPath stringByAppendingPathComponent:[NSString stringWithFormat:#"Stundenplan-%#.ics", _MatrNr]];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
NSError *error = nil;
[[self getICSData] writeToURL:fileUrl atomically:YES];
if (error) {
NSLog(#"Error while writing to File: %#", [error localizedDescription]);
}
return fileUrl;
}
-(NSData*)getICSData
{
NSMutableString *erg = [[NSMutableString alloc] init];
NSDate *aktDatum = [NSDate date];
[erg appendString:[NSString stringWithFormat:#"BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//www.htw-dresden.de//iOS//DE\nMETHOD:PUBLISH\n"]];
for (Stunde *this in _daten) {
NSString *titel = [this.titel stringByReplacingOccurrencesOfString:#"," withString:#"\\, "];
NSString *dozent = [this.dozent stringByReplacingOccurrencesOfString:#"," withString:#"\\, "];
NSString *uuid = [[NSUUID UUID] UUIDString];
[erg appendString:[NSString stringWithFormat:#"BEGIN:VEVENT\nUID:%#\n", uuid]];
[erg appendString:[NSString stringWithFormat:#"DTSTART:%#T%#Z\n",[self nurTagFromDate:this.anfang], [self nurUhrzeigFromDate:this.anfang]]];
[erg appendString:[NSString stringWithFormat:#"DTEND:%#T%#Z\n",[self nurTagFromDate:this.ende], [self nurUhrzeigFromDate:this.ende]]];
[erg appendString:[NSString stringWithFormat:#"LAST-MODIFIED:%#T%#Z\nSEQUENCE:0\nSTATUS:CONFIRMED\n", [self nurTagFromDate:aktDatum], [self nurUhrzeigFromDate:aktDatum]]];
[erg appendString:[NSString stringWithFormat:#"SUMMARY:%#\nDESCRIPTION:%#\nLOCATION:%#\nEND:VEVENT\n", titel, dozent, this.raum]];
}
[erg appendString:#"END:VCALENDER"];
NSData *ret = [erg dataUsingEncoding:NSUTF8StringEncoding];
return ret;
}
In the getFileUrl I want to return a URL to the file created in there. This function calls the getICSData function that goes through my array (_daten) and creates for every Stunde object this ics-"code".
For help I have these NSDate formatting functions:
-(NSString*)nurTagFromDate:(NSDate*)date
{
NSDateFormatter *nurTag = [[NSDateFormatter alloc] init];
[nurTag setDateFormat:#"yyyyMMdd"];
return [nurTag stringFromDate:date];
}
-(NSString*)nurUhrzeigFromDate:(NSDate*)date
{
NSDateFormatter *nurTag = [[NSDateFormatter alloc] init];
[nurTag setDateFormat:#"HHmmss"];
return [nurTag stringFromDate:date];
}
As output I get something like this:
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//www.htw-dresden.de//iOS//DE
METHOD:PUBLISH
BEGIN:VEVENT
UID:CCCAC9B3-E056-47E3-A63B-F2FE2C3BA454
DTSTART:20140318T111000Z
DTEND:20140318T124000Z
LAST-MODIFIED:20140429T182723Z
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:Internet-Technologien I
DESCRIPTION:Vogt\, J.
LOCATION:Z 254
END:VEVENT
.
.
.
END:VCALENDER
But the calendar app at my Mac don't want to open the file...
It would be great if some of you will have an Idea :)

Replace this line:
[erg appendString:#"END:VCALENDER"];
With this:
[erg appendString:#"END:VCALENDAR"];
Notice the typo in VCALENDAR.
You may find this iCal file validator useful.

Related

getting ALAssets from photos library iOS

I am trying to get the time, date and location from a movie i took on my iPhone/iPad This is the code i used
//i get the video picked and then save it to app but i think i am getting the movie's asset data before i save it.
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{ NSError *error;
else if (picker.sourceType ==UIImagePickerControllerSourceTypePhotoLibrary) {
NSURL * movieURL = [info valueForKey:UIImagePickerControllerMediaURL] ;
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:movieURL
resultBlock:^(ALAsset*asset) {
NSDate *myDate = [asset valueForProperty:ALAssetPropertyDate];
NSLog(#"Date: %#", myDate);
} failureBlock:^(NSError *error) {
NSLog(#"Error");
}];
NSData * movieData = [NSData dataWithContentsOfURL:movieURL];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *slash = [documentsDirectory stringByAppendingPathComponent:#"/"];
NSString *documentsDirectory2 = [slash stringByAppendingPathComponent:self.user.text];
NSString *documentsDirectory21 = [documentsDirectory2 stringByAppendingPathComponent:#"/Movie"];
if (![[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory21])
[[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory21 withIntermediateDirectories:NO attributes:nil error:&error];
NSString *fullPath = [documentsDirectory21 stringByAppendingPathComponent:[[self imageNameTextField]text]];
fullPath = [fullPath stringByAppendingFormat:#".mp4"];
[ movieData writeToFile:fullPath atomically:YES];
}
however myDate is logged as null
what am i doing wrong
I just tried this code also (added 10/11)
CGImageSourceRef source = CGImageSourceCreateWithURL( (CFURLRef)movieURL, NULL);
NSDictionary* metadata = (NSDictionary *)CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source,0,NULL));
NSLog(#"image data %#", metadata);
but still metadata is null;
I read that videos don't have the same metadata as images. if this is true then how do I access the video metadata since I can see data tied to the video in photos.
any help would be greatly appreciated.
I have tried everything I could find on the internet to help me do this but nothing seems to work.
When I log my movieURL this is what I get
2016-10-13 09:58:51.045271 my little world app[2641:1388034] movieURL;file:///private/var/mobile/Containers/Data/Application/22A178E5-74C4- 48EF-B487-5E01321508AD/tmp/trim.04F0AC8A-2960-464D-8670-7E79662EAB9B.MOV
So since it is in a temporary file can I not get the metadata.
Does the Metadata exits in the movies NSData and can I get it from that?
I am pulling my hair out.
So I figured this out here is the code
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if (picker.sourceType ==UIImagePickerControllerSourceTypePhotoLibrary) {
NSURL * movieURL = [info valueForKey:UIImagePickerControllerMediaURL] ;
[self getMediaName:nil url:[info objectForKey:UIImagePickerControllerReferenceURL]];
}
}
- (void)getMediaName:(UIImage*)originalImage url:(NSURL*)url {
#try {
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset) {
if (asset == nil) return;
ALAssetRepresentation *assetRep = [asset defaultRepresentation];
NSString *fileName = [assetRep filename];
NSDate *myDate = [asset valueForProperty:ALAssetPropertyDate];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy"];
NSLog(#"%#",[dateFormatter stringFromDate:myDate]);
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *error) {
};
ALAssetsLibrary *library = [ALAssetsLibrary new];
[library assetForURL:url resultBlock:resultblock failureBlock:failureblock];
}
#catch (NSException *exception) {
}
}
you can use any valueForProperty

How to get data to write to a text file?

Here is the method that I'm using for a console output. How can I output to a text file?
#implementation GetMoves
- (id)initWithDevice:(Device *)device{
if(self = [super init]){
_device = device;
}
return self;
}
- (void)Render{
[super ClearConsole];
printf("MOVES INFO\n");
printf("==================================\n");
NSArray* data = [_device GetData:NULL];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
if(data != nil){
for(int i = 0; i < data.count; i++){
DataPoint* current = [data objectAtIndex:i];
NSMutableString* output = [[NSMutableString alloc] init];
[output appendFormat:#"%d",(i+1)];
[output appendString:#" - "];
[output appendString:[dateFormatter stringFromDate:current.RecordedAt]];
[output appendString:#" - "];
[output appendFormat:#"%d",current.StepCount];
printf("%s\n", [output UTF8String]);
}
}
else
printf("No new data found on device.\n");
printf("\n");
printf("==================================\n");
printf("\n");
printf("Press ENTER to continue.");
fgetc(stdin);
}
#end
You can simply write as :
NSString *path = [#"~" stringByExpandingTildeInPath] ;
//change your path here
path=[path stringByAppendingString:#"/Documents/*REPORTS/"];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:#"%#/TWCodeFile.txt",path];
//create content - four lines of text
NSString *content = #"The content of file, can be more by appending string.";
//save content to the documents directory
[content writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];

data needs 10 min to load data into core-database

I am working with a core database to save my data that I get back from a webservice into my app. But this is very very slow. Any idea how this comes? What I do is the following. I have a class for getting the data from the webservice. like you can see over here.
GenkData.h
+ (NSArray *)getNews;
GenkData.m
+ (NSDictionary *)executeGenkFetch:(NSString *)query
{
query = [NSString stringWithFormat:#"%#", query];
query = [query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// NSLog(#"[%# %#] sent %#", NSStringFromClass([self class]), NSStringFromSelector(_cmd), query);
NSData *jsonData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:query] encoding:NSUTF8StringEncoding error:nil] dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error] : nil;
if (error) NSLog(#"[%# %#] JSON error: %#", NSStringFromClass([self class]), NSStringFromSelector(_cmd), error.localizedDescription);
// NSLog(#"[%# %#] received %#", NSStringFromClass([self class]), NSStringFromSelector(_cmd), results);
return results;
}
+ (NSArray *)getNews
{
NSString *request = [NSString stringWithFormat:#"http://www.krcgenk.be/mobile/json/request/news/type/ipad"];
return [[self executeGenkFetch:request] valueForKey:#"news"];
}
Then in my first view controller that shows up I have a method that for fetching my data in my core database.
- (void)fetchGenkDataIntoDocument:(UIManagedDocument *)document
{
NSLog(#"Fetch data");
dispatch_queue_t fetchQ = dispatch_queue_create("Genk fetcher", NULL);
dispatch_async(fetchQ, ^{
NSArray *news = [GenkData getNews];
[document.managedObjectContext performBlock:^{ // perform in the NSMOC's safe thread (main thread)
int newsId = 0;
//int trainingIndex = -1;
for (NSDictionary *genkInfo in news) {
NSLog(#"Begint met nieuwsitem");
newsId++;
[News newsWithGenkInfo:genkInfo inManagedObjectContext:document.managedObjectContext withNewsId:newsId];
NSLog(#"Einde met nieuwsitem");
}
}];
});
NSLog(#"einde fethdata");
}
Next I have a category of the NSManagerd object subclass of news where I do the following.
+ (News *)newsWithGenkInfo:(NSDictionary *)genkInfo
inManagedObjectContext:(NSManagedObjectContext *)context
withNewsId:(int)newsId
{
News *news = nil;
news = [NSEntityDescription insertNewObjectForEntityForName:#"News"
inManagedObjectContext:context];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT+0:00"]];
NSDate *date = [dateFormatter dateFromString:[genkInfo objectForKey:NEWS_DATE]];
news.title = [genkInfo objectForKey:NEWS_TITLE];
news.date = date;
news.genk_description = [genkInfo objectForKey:NEWS_DESCRIPTION];
news.imgurl = [genkInfo objectForKey:NEWS_IMGURL];
news.shortdescription = [genkInfo objectForKey:NEWS_SHORTDESCRIPTION];
news.url = [genkInfo objectForKey:NEWS_URL];
news.imagecopyright = [genkInfo objectForKey:NEWS_IMAGECOPYRIGHT];
news.news_id = [NSNumber numberWithInt:newsId];
return news;
}
I am doing the following proces like 10 times for 10 different entities. All of those attributes are mostly strings. Still this takes up to 10 minutes to load in all the data. Can somebody help me ?
Kind regards.
Dima's answer of checking with the profiler helped me with this question and solved my problem

Fix unicode characters in NSString

I am getting a string response from the Google Directions API that contains characters that are supposed to represent a different-language character. Similiar to Ƨ³, similiar to these type of characters. I think the characters are portugese, but anyways my string contains like this:
\U00e3o
(I am not sure if those are zeroes or 'O's)
I am not sure what the technical term for these characters are, but how can I fix them in my string so they print properly.
Thank you
UPDATE:
I have updated my question title with the correct term 'unicode'.
I have checked a few questions:
NSString Unicode display
Detect Unicode characters in NSString on iPhone
iOS HTML Unicode to NSString?
And a few others. I have followed the answer, but the unicode characters are not fixed.
UPDATE:
Here is my code to get the response from GDirection.
Forming the request and getting a response:
AFHTTPClient *_httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:#"http://maps.googleapis.com/"]];
[_httpClient registerHTTPOperationClass: [AFJSONRequestOperation class]];
[_httpClient setDefaultHeader:#"Accept" value:#"application/json"];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setObject:[NSString stringWithFormat:#"%f,%f", location.coordinate.latitude, location.coordinate.longitude] forKey:#"origin"];
[parameters setObject:[NSString stringWithFormat:#"%f,%f", location2.coordinate.latitude, location2.coordinate.longitude] forKey:#"destination"];
[parameters setObject:#"false" forKey:#"sensor"];
[parameters setObject:#"driving" forKey:#"mode"];
[parameters setObject:#"metric" forKey: #"units"];
NSMutableURLRequest *request = [_httpClient requestWithMethod:#"GET" path: #"maps/api/directions/json" parameters:parameters];
request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
AFHTTPRequestOperation *operation = [_httpClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSInteger statusCode = operation.response.statusCode;
if (statusCode == 200) {
[self parseResponse:responseObject];
} else {
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { }];
[_httpClient enqueueHTTPRequestOperation:operation];
}
Retrieving information from response object:
- (void)parseResponse:(NSDictionary *)response {
NSString *status = [response objectForKey: #"status"];
if (![status isEqualToString: #"OK"]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:[NSString stringWithFormat: #"Google Directions Response Status: %#", status] delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil, nil];
[alert show];
}
else {
NSArray *routes = [response objectForKey:#"routes"];
NSDictionary *routePath = [routes lastObject];
if (routePath) {
NSString *overviewPolyline = [[routePath objectForKey: #"overview_polyline"] objectForKey:#"points"];
legs = [routePath valueForKey: #"legs"];
if (legs) {
/* DIRECTION SET ================================================================================================================================
*/
directionOverview = [[NSMutableDictionary alloc] init];
NSString *legsDis = [NSString stringWithFormat: #"%#", [[legs valueForKey: #"distance"] valueForKey: #"text"]];
NSString *kmDistance = [self cutStringToPreference: legsDis];
if (kmDistance) {
[directionOverview setObject:kmDistance forKey: #"distance"];
milesLabel.text = kmDistance;
milesLabel.font = [UIFont fontWithName:#"interstate" size: 20.0];
}
NSString *durationText = [NSString stringWithFormat: #"%#", [[legs valueForKey: #"duration"] valueForKey: #"text"]];
durationText = [self cutStringToPreference: durationText];
if (durationText) {
[directionOverview setObject:durationText forKey: #"duration"];
}
NSString *startAddress = [NSString stringWithFormat: #"%#", [legs valueForKey: #"start_address"]];
startAddress = [self cutStringToPreference: startAddress];
NSString *endAddress = [NSString stringWithFormat: #"%#", [legs valueForKey: #"end_address"]];
endAddress = [self cutStringToPreference: endAddress];
[directionOverview setObject:startAddress forKey: #"origin"];
[directionOverview setObject:endAddress forKey: #"destination"];
NSArray *steps = [legs valueForKey: #"steps"];
if (steps) {
instructionArray = [[NSMutableArray alloc] init];
durationArray = [[NSMutableArray alloc] init];
distanceArray = [[NSMutableArray alloc] init];
int number = [[[steps lastObject] valueForKey: #"html_instructions"] count];
for (int i = 1; i <= number; ++i) {
NSString *instruction = [[[steps lastObject] valueForKey: #"html_instructions"] objectAtIndex: i-1];
instruction = [self cutStringToPreference: instruction];
instruction = [self flattenHTML: instruction];
instruction = [self stringByDecodingHTMLEntitiesInString: instruction];
[instructionArray addObject: instruction];
NSString *distance = [[[[steps lastObject] valueForKey: #"distance"] objectAtIndex: i-1] valueForKey: #"text"];
[distanceArray addObject: distance];
NSString *duration = [[[[steps lastObject] valueForKey: #"duration"] objectAtIndex: i-1] valueForKey: #"text"];
[durationArray addObject: duration];
}
}
}
_path = [self decodePolyLine:overviewPolyline];
NSInteger numberOfSteps = _path.count;
CLLocationCoordinate2D coordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++) {
CLLocation *location = [_path objectAtIndex:index];
CLLocationCoordinate2D coordinate = location.coordinate;
coordinates[index] = coordinate;
}
polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
[self.mapView addOverlay:polyLine];
}
}
}
Displaying the text in a label:
NSString *overviewAddressText = [NSString stringWithFormat: #"%# to %#", [directionOverview objectForKey: #"origin"], [directionOverview objectForKey: #"destination"]];
overviewAddress.text = overviewAddressText;
UPDATE:
Image of label
So as you can see, in the label, the text contains this kind of substring: S/U00e3o, which is a word that has an unsupported character. How can I fix that so that unicode turns into this: São

Parse JSON in Objective-C with SBJSON

I just want to parse this JSON string in Objective-C using the SBJSON framework, and retrieve the three units of data:
{"x":"197","y":"191","text":"this is a string"}
How can this be done?
NSString * jsonString = #"{\"x\":\"197\",\"y\":\"191\",\"text\":\"this is a string\"}";
SBJSON *jsonParser = [[SBJSON alloc] init];
NSDictionary * dictionary = [jsonParser objectWithString:jsonString];
NSLog(#"x is %#",[dictionary objectForKey:#"x"]);
[jsonParser release];
Here's an example:
NSString *jsonText = #"...";
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *dict = [parser objectWithString:jsonText];
for (NSString *key in [#"x y text" componentsSeparatedByString:#" "]) {
NSLog(#"%# => %#", key, [dict objectForKey]);
}
Here's something similar for SBJson4Parser:
id parser = [SBJson4Parser parserWithBlock:^(id v, BOOL *stop) {
for (NSString *key in [#"x y text" componentsSeparatedByString:#" "]) {
NSLog(#"%# => %#", key, [v objectForKey]);
}
}
allowMultiRoot:NO
unwrapRootArray:NO
errorHandler:^(NSError *err) {
// handle error here
}];
NSString *jsonText = #"...";
[parser parse: [jsonText UTF8String]];