Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share image in iMessage using Custom Keyboard in iOS 8

I am working on iOS 8 custom keyboard, where i have designed keyboard using some images like smiley. i want this keyboard to be work with iMessage. when i am trying to send text its working properly but can't able to share image there. I have tried following code :

To share text : (its working properly)

-(void)shouldAddCharector:(NSString*)Charector{
    if ([Charector isEqualToString:@"Clear"]) {
        [self.textDocumentProxy deleteBackward];
    } else if([Charector isEqualToString:@"Dismiss"]){
        [self dismissKeyboard];
    } else {
        [self.textDocumentProxy insertText:Charector];
    }
}

To add image : ( Not working)

-(void)shouldAddImage:(UIImage*)oneImage
{
        UIImage* onions = [UIImage imageNamed:@"0.png"];

        NSMutableAttributedString *mas;
        NSTextAttachment* onionatt = [NSTextAttachment new];
        onionatt.image = onions;
        onionatt.bounds = CGRectMake(0,-5,onions.size.width,onions.size.height);
        NSAttributedString* onionattchar = [NSAttributedString attributedStringWithAttachment:onionatt];

        NSRange r = [[mas string] rangeOfString:@"Onions"];
        [mas insertAttributedString:onionattchar atIndex:(r.location + r.length)];
        NSString *string =[NSString stringWithFormat:@"%@",mas];
        [self.textDocumentProxy insertText:string];
}

Is there any possibility to pass image to [self.textDocumentProxy insertText:string];

following attached image shows how exactly i want to use this image keyboard. i am surprised how emoji keyboard will work? enter image description here

like image 949
Gaurav Avatar asked Jul 24 '14 12:07

Gaurav


1 Answers

As far as I know, the behavior you are looking for is not possible as of iOS 8 beta 4.

Currently, the only way for iOS custom keyboards to interact with text is through <UITextDocumentProxy> and the only way to insert anything is via the insertText: method.

Here is the header for the insertText: method in <UITextDocumentProxy>:

- (void)insertText:(NSString *)text;

As you can see, it takes a plain NSString... not an NSAttributedString. This is why your attempt to insert an image doesn't work.

However, despite the fact that you can't add pictures, it is still very possible to insert emojis, since an emoticon is really just a Unicode character.

To add an emoji, just insert the proper Unicode character:

[self.textDocumentProxy insertText:@"\U0001F603"];

Useful links:

List of Unicode Emoji: http://apps.timwhitlock.info/emoji/tables/unicode

Unicode Characters as NSStrings: Writing a unicode character with NSString

like image 139
Matt Avatar answered Sep 19 '22 00:09

Matt