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)
Related
Im facing an issue of PDF images not displaying in UIWebView
The way of the logic is,
1. UIWebView receives a base64 string
2. Convert the string to NSData
3. Load the NSData to the UIWebView
Here is the part of the code
NSString *b64 = #"JVBERi0xLjMNCiW9vrwNCjEgMCBvYmoNCjw8DQoJL0Jhc2VGb25....";
NSData *d =[[NSData alloc] initWithBase64EncodedString:b64 options:nil];
[uiWebview loadData:d MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];
There is a workaround that i manage to show the images in UIWebView, the workaround is
Copy the base64 string to an online converter to convert it to a pdf file
Export there pdf file again using Mac Application Preview using 'Export as PDF'
Convert the file again to base64 string and display at the UIWebView
But this workaround is not feasible, as i do not have the Preview application in the app
I also notice that, before i 'Export as PDF' in Preview application the pdf is editable such as the images can be drag or deleted.
So my question would be,
Did I implement UIWebView code wrongly?
Is the any library for 'Export as PDF' for iOS application? I did explore around on PDFKit, but it does seem to have any export function
b64 is of type NSString
initWithBase64EncodedData:b64 options:nil needs as first Parameter NSData.
Try:
NSString *b64 = #"JVBERi0xLjMNCiW9vrwNCjEgMCBvYmoNCjw8DQoJL0Jhc2VGb25....";
NSData *d =[[NSData alloc] initWithBase64EncodedString:b64 options:nil];
[uiWebview loadData:data MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];
Or try:
[[PDFDocument alloc] initWithData: d];
to get a Pdf from your base64 string.
I am trying to use Apple App Thinning feature (available from iOS 9) that let you differentiate resources based on device architecture and features. In my case what I would like to do is to have a different video file in the application bundle (in .mp4 format) one for the iPhone and one for the iPad using Xcode .xcassets Data Set.
To retrieve a file from a .xcassets Data Set Apple provides the NSDataAsset class, but: since AVPlayer needs a URL to play a video and NSDataAsset only provides its contents using Data format, I'm unable to play the video.
What I would like to do is to retrive the NSDataAsset .data URL. Is it possible?
You can try:
NSDataAsset *videosDataAsset = [[NSDataAsset alloc] initWithName:#"AssetName"];
NSData *data = videosDataAsset.data;
NSString *filename = #"FileToSaveInto.mp4";
NSURL *URL = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:filename];
if ([data writeToURL:URL atomically:YES]) {
// run player
}
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.
I am having an app in which I want to copy an image and paste it in the sms app.
I am using the below code.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSString *filePath = [[NSBundle mainBundle]pathForResource:#"Heritage" ofType:#"png"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
[pasteboard setData:data forPasteboardType:#"public.png"];
}
I have got this code after searching on google and there are lots of code but somehow none of them seems to work for me.
When I run the app and go to sms app and press, the paste option doesn't show me there.
Heritage.png is an image in my app's bundle.
Where am I doing any mistake?
Please let me know.
Thanks...
Finally, I got a solution of my own.
I didn't enable the keyboard extension to allow full access.
Just forgot to set "RequestOpenAccess" to "YES".
Hope someone else does not forget to check this small things.
Thanks...
How can we set an image behalf of the text?
Following code is to display text in the input view:
[self.textDocumentProxy insertText:[key currentTitle]];
My code of paste board to display the image :
NSData *imgData = UIImagePNGRepresentation(image);
[pasteBoard setData:imgData forPasteboardType:[UIPasteboardTypeListImage objectAtIndex:0]];
NSString* newStr = [[NSString alloc] initWithData:imgData encoding:NSUTF8StringEncoding];
[self.textDocumentProxy insertText:[pasteBoard string]];
I have created a pasteboard with the images just I have to display the images in to input view.
See this picture for the concept :
Note:
I have to select image from keyboard and show in inputview not to copy paste from library.
If anyone know please reply as answer or any suggestion will also acceptable.
Thanks!
I don't think it's possible to add directly an image from a custom keyboard. A workaround would be to copy the image to the pastboard and paste it in the destination area. (It's not very nice but It's the only way I see actually)
I have get solution for this question use clipboard copy and paste
Code :
UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
NSData *data = [self.Videoassets objectAtIndex:btn.tag];
NSLog(#"File size is : %.2f MB",(float)data.length/1024.0f/1024.0f);
[pasteBoard setData:data forPasteboardType:#"public.mpeg-4"];