How can I open calendar .ics files in ios? - ios

I am trying to download and open .ics file in my app.
I found few question, and here's some code I am using
// NSString *path = [[NSBundle mainBundle] pathForResource:#"http://www.nmsd.wednet.edu//site/handlers/icalfeed.ashx?MIID=607" ofType:#"ics"];
NSURL *url = [NSURL fileURLWithPath:#"http://www.nmsd.wednet.edu//site/handlers/icalfeed.ashx?MIID=607"];
UIDocumentInteractionController *dc = [UIDocumentInteractionController interactionControllerWithURL:url];
dc.delegate = self;
[dc presentPreviewAnimated:YES];
Nothing at all happens. no errors at all. Would it be easier to look for a library to achieve this.

If you have a link to remote .ics file, then prepare it for Safari.
Let's suppose that you have a link to iCal file:
"pl-dev2.office.org/events/sync.ics?t=90613b6c7f8".
Just add prefix webcal:// to your link.
Swift
let link = "webcal://pl-dev2.office/events/sync.ics?t=90613b6c7f8"
let webcal = NSURL(string: link)
UIApplication.sharedApplication().openURL(webcal)
Objective-C
NSString *link = "webcal://pl-dev2.office/events/sync.ics?t=90613b6c7f8"
NSUrl *webcal = [NSURL URLWithString:link];
[[UIApplication sharedApplication] openURL:webcal];
Safari will sync it in your Calendar app for You.

The #Bartłomiej Semańczyk is adding a calendar, if you want to add a event you can open url with SFSafariController
let link = "https://www.apple.com/v/apple-events/home/p/built/assets/event.ics?d=3213214324313"
let url = URL(string: link)!
let safariVC = SFSafariViewController(url: url)
present(safariVC, animated: true, completion: nil)

You use NSURL fileURLWithPath with an URL string as parameter although it expects file path. Change it to NSURL URLWithString.

Related

How to get path extension in iOS

If I have a file name like this how do I get the path extension?
http://example.com/image.png?key=value
If I use -pathExtension for NSString it returns jpg?key=value.
I want to get the true extension from the path so just jpg.
Try your code in swift. It works fine.
let url = URL(string: "http://example.com/image.png?key=value")
print(url?.pathExtension) //Optional("png")
Try this:
NSURL *url = [NSURL URLWithString:#"http://example.com/image.png?key=value"];
NSString *ext = [[url lastPathComponent] pathExtension];
NSLog(#"%#", ext) ; // png

Copy video from Resource to UIPasteboard

I am Developing a custom keyboard with Keyboard Extension.
I want to copy video from my resource folder to UIPasteboard. For that i am using below code but no luck
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"SampleVideo_1280x720_1mb" ofType:#"mp4"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
NSData *data = [NSData dataWithContentsOfURL:movieURL];
[[UIPasteboard generalPasteboard]setPersistent:YES];
[[UIPasteboard generalPasteboard] setData:data forPasteboardType:(NSString *)kUTTypeVideo];
Can you please help me this?
And one more thing is it Possible to do?
Thanks in advance.
The pasteboardType you are using (kUTTypeVideo) is for a type of movie that has no sound. This will not work. If you copied an mp4 file to the pasteboard using that pasteboardType, and then say, pasted it into an iMessage chat field, it'll show that it's an unknown and unplayable format.
Instead, use kUTTypeQuickTimeMovie or kUTTypeMPEG4. This will work.
Also, no need to setPersistent to Yes; this is the general pasteboard which is a system pasteboard that transcends the space-time of any app, and so you don't need to do that. :)
Try this code. It worked for me.
let pb = UIPasteboard.generalPasteboard()
let url: NSURL = NSBundle.mainBundle().URLForResource("\(tempArray[indexPath.row])", withExtension: "mp4")!
let data1: NSData = try! NSData(contentsOfURL: url, options: NSDataReadingOptions.DataReadingMappedIfSafe)
let data: NSData = NSData(contentsOfURL: url)!
print(data)
pb.persistent = true
pb.setData(data1, forPasteboardType: "public.mpeg-4")
And also if u want to get the URL of the video from the NSBundle you need to add the video locally.

Open .ibooks file automatically from iOS application (Swift)

I want to open specific file in iBooks App. I'm using UIDocumentsIntractionController to send this file to iBooks App. My code:
class ViewController : UIViewController {
...
...
#IBOutlet var iBooksBtn: UIButton!
#IBAction func book(sender: AnyObject) {
let ibookPath = NSBundle.mainBundle().pathForResource("test1",ofType: "ibooks")!
interactionController = UIDocumentInteractionController(URL: NSURL(fileURLWithPath:ibookPath))
interactionController.UTI = "com.apple.ibooks"
interactionController.presentOpenInMenuFromRect(CGRectZero,inView:self.iBooksBtn, animated:false)
}
...
...
}
So, for now i store this file in mainBundle, but in future i want to download it from the Internet. So when i press button, i got few results to
choose:
But i want to open this file ONLY with iBooks and without requests to choose App (as on picture). As far as i know i need to use UTI for *.ibooks file, i guess UTI, that i used is wrong. Thanks for any help. Also custom URL scheme (ibooks:// and etc.) is not working for me.
To open iBooks you should use custom URL scheme:
To open the library:
NSString *stringURL = #"ibooks://";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
To open the iBooks store at a particular book page, you have to add the complete URL to that particular book:
NSString *stringURL = #"itms-books://itunes.apple.com/de/book/marchen/id436945766";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
NSString *stringURL = #"itms-bookss://itunes.apple.com/de/book/marchen/id436945766";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];

iOS open iBook file in App Bundle

I have a iBook file (say SampleiBook.iba) within the app bundle (not in documents directory).
Now i just want to open SampleiBook from within app. I tried this,
NSString *path = [[NSBundle mainBundle] pathForResource:#"SampleiBook" ofType:#".iba"];
NSString *stringURL = [#"ibooks://" stringByAppendingPathComponent:path];
NSURL *url = [NSURL URLWithString:stringURL];
if ([[UIApplication sharedApplication] canOpenURL:url])
{
NSLog(#"Yes");
}
[[UIApplication sharedApplication] openURL:url];
the iBook application gets opened, showing only the files within the iBook application. But i want the one in app bundle to be opened.
Please guide me in achieving this.
It appears that you are trying to load the .iba (iBooks Author Book) file from your app. This not going to work because .iba is the file that contains your source for the .ibooks file type that I think you really want to load.
I am writing in swift these days. Here is a swift example I just ran. Assumes that the unbutton is wired from a nib or storyboard
#IBOutlet weak var iBooksBtn: UIButton!
#IBAction func iBooksBtnPressed(sender: AnyObject)
{
let ibookPath = NSBundle.mainBundle().pathForResource("Cupertino",
ofType: "ibooks")!
let url = NSURL(fileURLWithPath: ibookPath)
let interactionController = UIDocumentInteractionController(URL:url!)
interactionController.presentOptionsMenuFromRect(CGRectZero,
inView:self.iBooksBtn, animated:false)
}
You will have to use the UIDocumentInteractionController to open the iBook in iBooks.
You can easily create an instance for the iBook in mian bundle:
NSURL *iBookResourceURL = [[NSBundle mainBundle] URLForResource:#"SampleiBook" withExtension:#"iba"];
UIDocumentInteractionController *interactionController =
[UIDocumentInteractionController interactionControllerWithURL: iBookResourceURL];

how to share a text string by UIDocumentInteractionController

we can use UIDocumentInteractionController to share a file to other apps like this:
NSURL *url = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource: #"test.txt" ofType: nil]];
UIDocumentInteractionController *dic = [[UIDocumentInteractionController interactionControllerWithURL: url] retain];
[dic presentOpenInMenuFromRect: CGRectZero inView: self.view animated: YES];
but how to share a text string(not a text file) by UIDocumentInteractionController?
Android Device can do this.
Thanks.
You can't, UIDocumentInteractionController is for documents. It is even in the name.
A solution would be to save the string to text file and share that file, or if you want to share a string, for example to post on a social network you should use UIActivityViewController

Resources