Open .ics file in iOS App - ios

I need to open a .ics file in my app.
I tried to open it like this:
NSURL *path = [[NSBundle mainBundle] URLForResource:#"myIcsName" withExtension:#"ics"];
[[UIApplication sharedApplication] openURL:path];
But nothing is happening.
How do I open the .ics file?

I just used the following code:
- (IBAction)displayICS:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"ics"];
NSURL *url = [NSURL fileURLWithPath:path];
UIDocumentInteractionController *dc = [UIDocumentInteractionController interactionControllerWithURL:url];
dc.delegate = self;
[dc presentPreviewAnimated:YES];
}
-(UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
return self;
}

I have found there are two projects that enable you to open an ICS file and get events, dates and other information out of it as Objective-C objects.
MXLCalendarManager seems to be a bit further along and will allow you to pull out the events from the ICS file search on date and add the events to the users iOS calendar. This might be what your looking for. GitHub Project MXLCalendarManager It also supports writing new ICS files.
I've created a project that uses the libical library written in C to parse the complete ICS file. The project is still in the early stages and could use some more tender care before its ready to just drop into a project. You can check it out at Github Project XbICalendar More details on libical can be found at GitHub Project libical

Related

Get URL to a PDF thats not in documents directory - UIDocumentInteractionController

I have a PDF that is dynamically generated from saved core data every time, rather than stored in the documents directory as NSData self.pdfData, rendered in a UIWebView. Rather than using UIActivityController to share the PDF i'd like to use UIDocumentInteractionController to get a full range of sharing options. The issue is this only seems to be for saved PDF's in the documents directory.
NSURL *PDFUrl = [[NSBundle mainBundle] URLForResource:#"sample" withExtension:#"pdf"];
Id rather not save it to documents directory then delete it, I was hoping for something more elegant. I tried to get the URL directly from the UIWebview of the PDF using self.webView.request.mainDocumentURL however this returns about:blank
NSURL *PDFUrl = [[NSBundle mainBundle] URLForResource:#"sample" withExtension:#"pdf"]; <-----
if (PDFUrl) {
DebugLog(#"Loading document");
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:PDFUrl];
[self.documentController setDelegate:self];
[self.documentController presentPreviewAnimated:YES];
}
UIDocumentInteractionController only works with a file on the local file system. This means you must save the PDF to a file before you can use UIDocumentInteractionController.
Write the PDF data to a file in the caches folder (NSCachesDirectory), get its file URL, use the UIDocumentInteractionController, and then delete the file when complete.
Or use UIActivityViewController and pass the NSData of the PDF as the activity item.

How to open a web (.html) ressource file with native code (ios app)

I have an ios app built with Cordova.
I would like to open a web ressource file (.html) after a deep linking, in a UIWebView with the handleURL ios method.
I know I'm supposed to do this within my cordova JS files, but I would like to know the way to do it natively?
Let's say I define a my-app URL scheme and want to open file1.html.
What are the ways for doing that?
I have found this:
// Load the html as a string from the file system
NSString *path = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
// Tell the web view to load it
[WebView loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]];
But nothing happens, like it can't find that file.
Why is that?
put all files in a folder and while adding drag drop in project. and select create folder references and copy items if needed. and try to load in UIWebView
You make a new class that inherits from NSURLProtocol.
You need to call [NSURLProtocol registerClass:YOURCLASS] in your app
In your class, implement + (BOOL) canInitWithRequest:(NSURLRequest *) to return true if the scheme matches your scheme (e.g. my-app)
See the docs for NSURLProtocol to see how to implement the rest of the functionality. For simple file system reads, you can implement startLoading and call self.client callbacks (e.g. URLProtocol:didLoadData:). For more complex things, you might need to override more.
See this tutorial: http://www.raywenderlich.com/59982/nsurlprotocol-tutorial

WCErrorDomain 7013 when trying to send image using Watch OS 2

I´m testing Apple Watch OS 2 and I´m trying to send a image from the application to the watch. According to Apple, I shall use WCSession transferFile to do this.
Use the transferFile:metadata: method to transfer files in the background. Use this method in cases where you want to send more than a simple dictionary of values. For example, use this method to send images or file-based documents.
for example:
NSString *string = [[NSBundle mainBundle] pathForResource:#"my_image" ofType:#"png"];
NSURL *path = [NSURL URLWithString:string];
[[WCSession defaultSession] transferFile:path metadata:#{#"meta1":#"meta2"}];
It all looks ok in the debugger, the path is correct and the file is accessible (checked with NSFileManager) and readable.
However, everytime I try I get a callback to the didFinishFileTransfer function, including an error:
Error Domain=WCErrorDomain Code=7013 "The operation couldn’t be completed. (WCErrorDomain error 7013.)"
Looking up the error:
WCErrorCodeFileAccessDenied
An error indicating that a file could not be transferred because it was inaccessible.
Available in watchOS 2.0 and later.
It seems the file is not accessible by the send function? I have tried things like resaving the file to another directory etc, but nothing seems to work.
Anyone got an idea?
The URL you are creating is not a fileURL. Try:
NSURL *path = [NSURL fileURLWithPath:string];
I managed to solve the issue!
It was because my path did not start with file://
The following code worked just fine:
NSString *string = [[NSBundle mainBundle] pathForResource:#"my_image" ofType:#"png"];
string = [NSString stringWithFormat:#"file://%#", string];
NSURL *path = [NSURL URLWithString:string];
[[WCSession defaultSession] transferFile:path metadata:#{#"meta1":#"meta2"}];
So it´s quite picky regarding the path.

How to get file in a library iOS

I am trying to construct a library in IOS.
In my library ,I use a method -(void)loadNewData to load some data EX:test.csv.
But when I export my library and let the other app use my library to excute the loadNewData method.
I found it can not be load correctly. It seems that because there is not such a file test.csv in the app.
How should I revise my code? Or where should I notice when I export the library?
Thank you for watch my question.
And sorry for my poor English.
Here is the a part of code of the loadNewData:
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *Rawsurveys=[resourcePath stringByAppendingPathComponent:#"test.csv"];
NSString *RawsurveyResults;
if([[NSFileManager defaultManager]fileExistsAtPath:Rawsurveys])
{
NSLog(#"find file");
NSFileHandle *RawfileHandle=[NSFileHandle fileHandleForReadingAtPath:Rawsurveys];
RawsurveyResults=[[NSString alloc]initWithData:[RawfileHandle availableData] encoding:NSUTF8StringEncoding];
[RawfileHandle closeFile];
}
You could probably create a bundle within your library and add you test.csv there.
Then you can access your test.csv from your own bundle.
Example of how I use images from my bundle. [UIImage imageNamed:#"myBundle.bundle/myImage"]. You could use the somewhat same for your file

How to open a PDF file in my iPad/iPhone using my iOS application?

How can I open a PDF file that was stored in my iPad/iPhone, using my own application?
You can use UIwebview to load it. It is very simple. If you want more flexibility you should use Quartz framework classes.
EDIT:
To view downloaded PDF, you can provide open-in functionality in your app. This is how you add "open-in" to your app.
Look here for complete tutorial.
There is a good tutorial available here which demonstrates how to open .pdf, .xls files in your application.
The main class you have to refer for this is QLPreviewController. here
This is the Datasource Method you would have to call for that
- (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
// Break the path into it's components (filename and extension)
NSArray *fileComponents = [[arrayOfDocuments objectAtIndex: index] componentsSeparatedByString:#"."];
// Use the filename (index 0) and the extension (index 1) to get path
NSString *path = [[NSBundle mainBundle] pathForResource:[fileComponents objectAtIndex:0] ofType:[fileComponents objectAtIndex:1]];
return [NSURL fileURLWithPath:path];
}
Also someone would like to refer this SO question :iPhone - Opening word and excel file without using UIWebview.
You can use Core Graphics for this task.
Look at PDFView sample from apple resources

Resources