Copy video from Resource to UIPasteboard - ios

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.

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

How do I send audio file send via iMessage ios 8

As we know with the launch of ios 8 the apple allow custom Keyboard extension.In keyboard extension we can send images,gif etc in SMS by using Copy image to clipboard.code
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSData *data= UIImagePNGRepresentation([UIImage imageNamed:#"so_close_disappointed_meme.png"]);
[pasteboard setData:data forPasteboardType:#"public.png"];
Now i am trying to send audio file in iMessage like this feature reference.don't know apple will allow us to send audio in iMessage?.so for i tried above approach but it did not show any paste option for audio in SMS window.
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSString *path = [[NSBundle mainBundle] pathForResource:#"tune"ofType:#"mp3"];
NSURL *url = [[NSURL alloc] initWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
[pasteboard setData:data forPasteboardType:#"public.mp3"];
Any one can suggest me how do we send audio file by using custom keyboard extension.is it possible?
I believe you could directly attach the file to MFMessageComposeViewController. Here is the documentation link of how it could be done.
Following would be the steps to do so.
Check if file can be send using file UTI using +
(BOOL)isSupportedAttachmentUTI:(NSString *)uti
Find File UTI. i.e. path for the file
Attach file to MFMessageComposeViewController using -
(BOOL)addAttachmentData:(NSData *)attachmentData
typeIdentifier:(NSString *)uti filename:(NSString *)filename
As the description for the method says
This method is especially useful when the attachment you want to add to a message does not have a file system representation. This can be the case, for example, for programmatically composed audiovisual content.
Note : You will have to convert audio file to NSData
The MFMessageComposeViewController isn't the solution in this scenario. A custom keyboard extension shouldn't present a new view controller, rather just paste the audio file to the pasteboard. Heres some swift code that worked for me
let path = NSBundle.mainBundle().pathForResource("audio", ofType:"wav")
let fileURL = NSURL(fileURLWithPath: path!)
let data = NSData(contentsOfURL: fileURL)
let wavUTI = "com.microsoft.waveform-audio"
UIPasteboard.generalPasteboard().setData(data!, forPasteboardType: wavUTI)

How can I open calendar .ics files in 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.

Display an ALAsset using QLPreviewController

I'm wanting to use QLPreviewController to display ALAssets from the Photo Stream
QLPreviewController needs an NSURL to display the item
This works great when it is a File URL such as
/var/mobile/Applications/5374......9E0/Documents/image33.png
I have the ALAsset
However using [[asset defaultRepresentation] url] gives me a NSURL type
assets-library://asset/asset.JPG?id=00000000-0000-0000-0000-000000000075&ext=JPG
But this does not display QLPreviewController just keeps showing loading ??
Any ideas ?
Thanks in advance
Maybe not the fastest and most efficient way, but it solves the problem. Please use NSFilemanager instead NSTemporaryDirectory as in documentation :-) Make sure to link against ImageIO.framework
#import <ImageIO/ImageIO.h>
....
NSURL *outURLToUseWithQLPreviewController = nil;
ALAsset *myAsset = ... // received somehow
ALAssetRepresentation *represent = myAsset.defaultRepresentation;
CGImageRef representFullScreen = represent.fullScreenImage;
NSString *tempDir = NSTemporaryDirectory();
NSString *imagePath = [tempDir stringByAppendingPathComponent:represent.filename];
NSURL *tempURL = [NSURL fileURLWithPath:imagePath];
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)(tempURL), (__bridge CFStringRef)(represent.UTI), 1, NULL);
CGImageDestinationAddImage(destination, representFullScreen, nil);
if ( CGImageDestinationFinalize(destination) ) {
outURLToUseWithQLPreviewController = tempURL;
}
return outURLToUseWithQLPreviewController;

Extracting data from an audio file

We have a web services function to which we are uploading an audio file into to be saved in the server. The code that I am using to create a data buffer (to pass to the web services) of the contents of the audio file is :
NSURL *soundFileURL = [[NSURL alloc] initFileURLWithPath:fnWithSlash];
audioData = [NSData dataWithContentsOfURL:soundFileURL];
NSLog(#"audiodata%d",audioData.length);
Here fnWithSlash contains the full path of the file. However audioData.length is always 0.
What am i missing?
Thanks in advance for your response.
Try using NSData's dataWithContentsOfFile: instead..If the file is in your bundle it will look like the example below, otherwise construct the filePath of the sound file to your desired location..
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"yourSound" ofType:#"wav"];
audioData = [NSData dataWithContentsOfFile:filePath];
NSLog(#"audiodata%d",audioData.length);

Resources