so I've got the following code to insert text:
func insert(text: String) {
(textDocumentProxy as UIKeyInput).insertText(text)
}
But how do I actually get it to send to the chat window?
If you are looking to access all the text entered in the UIInputViewController, the following code may help:
var totalText = "" // complete text entered
if let before: String = self.textDocumentProxy.documentContextBeforeInput {
totalText += before
}
if let after: String = self.textDocumentProxy.documentContextAfterInput {
totalText += after
}
Now that you have complete text entered, you can send it to chat window. There are some limitations here which I can share if this is what you're looking for. Please let me know
Edit
To send text to whatsapp, you need to press send button which is outside the custom keyboard extension. If you want to enter text into the UITextView of whatsapp, it is pretty straightforward to do so. But, the custom keyboard extension can't control the send button of whatsapp
Related
I'm presenting custom action buttons in my iOS 11 notification window via a Notification Content Extension target. One of them is a 'comment' button. If I press it the keyboard shows up properly, but I am not able to figure out how to have the keyboard go away and get back to the other buttons on the notification. There's not really anything I can see to call resignFirstResponder on. Am I just missing something really obvious?
There is more than one way to do this.
Without A Content Extension
The first does not even require a notification content extension! The UNTextInputNotificationAction does all of the work for you. When initializing the action you specify parameters for the text field that will be presented when the action is triggered. That action is attached to your notification category during registration (i.e. inside willFinishLaunchingWithOptions):
userNotificationCenter.getNotificationCategories { (categories) in
var categories: Set<UNNotificationCategory> = categories
let inputAction: UNTextInputNotificationAction = UNTextInputNotificationAction(identifier: "org.quellish.textInput", title: "Comment", options: [], textInputButtonTitle: "Done", textInputPlaceholder: "This is awesome!")
let category: UNNotificationCategory = UNNotificationCategory(identifier: notificationCategory, actions: [inputAction], intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: "Placeholder", options: [])
categories.insert(category)
userNotificationCenter.setNotificationCategories(categories)
}
This will produce an experience like this:
Note that by default, the "Done" button dismisses the keyboard and notification.
With more than one action you get this:
There is no going back to the action buttons that were presented with the notification - notifications can't do that. To see those actions choices again would require showing another notification.
With a Content Extension
First, the above section works with a content extension as well. When the user finishes entering text and hits the "textInputButton" the didReceive(_:completionHandler:) method of the content extension is called. This is an opportunity to use the input or dismiss the extension. The WWDC 2016 session Advanced Notifications describes this same use case and details ways it can be customized further.
This may not meet your needs. You may want to have a customized text entry user interface, etc. In that case it is up to your extension to handle showing and hiding the keyboard. The responder that handles text input - a UITextField, for example - should become first responder when the notification is received. Doing so will show the keyboard. Resigning first responder will hide it. This can be done inside a UITextField delegate method.
For example, this:
override var canBecomeFirstResponder: Bool {
get {
return true
}
}
func didReceive(_ notification: UNNotification) {
self.label?.text = notification.request.content.body
self.textField?.delegate = self
self.becomeFirstResponder()
self.textField?.becomeFirstResponder()
return
}
// UITextFieldDelegate
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.textField?.resignFirstResponder()
self.resignFirstResponder()
return true
}
Produces a result like this:
Keep in mind that on iOS 10 and 11 any taps on the notification itself - like on your text field - may result in it being dismissed! For this and many other reasons going this route is probably not desirable.
I am working on chat application, Where I need to show Emoji view on button selection. So I can select and send emoji within the text message. This functionality is already inside Messenger application. I know there is button already in keyboard but I want to add button outside keyboard. Please let me know if there is any resource available to get it done quickly.
This way you can array of Emoji.
let emojiRanges = [
0x1F601...0x1F64F, // emoticons
0x1F30D...0x1F567, // Other additional symbols
0x1F680...0x1F6C0, // Transport and map symbols
0x1F681...0x1F6C5 //Additional transport and map symbols
]
var emojis: [String] = []
for range in emojiRanges {
for i in range {
let c = String(describing: UnicodeScalar(i)!)
emojis.append(c)
}
}
I would like to make my app appear in the UIActivityViewController for text sharing, like in Mail, iMessage, Notes, Gmail etc etc.
For example, when user tapback on selected text and hit the 'Share' button from any app like in the attachment:
I would like my app to appear in the UIActivityViewController and when the user selects my app, to launch it with the ability to handle that selected text.
So what I have tried:
Search in Apple documentation.
Searched for relevant UTI but I understood that the UTIs are used only for files and not for a simple NSString (Correct me if I'm wrong).
I have tried to implement Share Extension, but this is not the solution that i want, I don't need the post popup and moreover than that I need to launch my app after sharing like in Mail, Notes, iMessage does (Regarding to Apple documentation we can't launch the contain app through Share extension only through Today extension).
Of course, I have searched a lot in StackOverFlow.
So any solutions?
Thanks!
Assume that you already have some app.
Add Target File -> New -> Target. In
the left pane, select Application Extension from the iOS section,
choose Action extension, and click Next.
Set the Product Name. Make sure the Action Type is Presents user interface. Click Finish.
In the Project Navigator, expand the extension group and click on MainInterface.storyboard. Select the image view and replace it with UITextView. Create and bind #IBOutlet weak var to it.
Select Info.plist of the extension, navigate to NSExtension -> NSExtensionAttributes -> NSExtensionActivationRule. Change the NSExtensionActivationRule's type from String to Dictionary. With the dictionary expanded, click the + button next to it. This will add a child key. Set its name to NSExtensionActivationSupportsText, its type to Boolean, and the value to YES. This ensures that the action extension is only visible when at least one input item contains text.
Put this code to ActionViewController.swift:
_
import UIKit
import MobileCoreServices
class ActionViewController: UIViewController {
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Get the item[s] we're handling from the extension context.
var textFound = false
for item in self.extensionContext!.inputItems as! [NSExtensionItem] {
for provider in item.attachments! {
if provider.hasItemConformingToTypeIdentifier(kUTTypePlainText as String) {
// This is an plain Text.
weak var weakTextView = self.textView
provider.loadItem(forTypeIdentifier: kUTTypePlainText as String, options: nil, completionHandler: { (textItem, error) in
OperationQueue.main.addOperation {
if let strongTextView = weakTextView {
if let gotText = textItem as? String {
strongTextView.text = gotText
// do what you need with the text
}
}
}
})
textFound = true
break
}
}
if (textFound) {
// We only handle one text, so stop looking for more. You can do as you need.
break
}
}
}
#IBAction func done() {
// Return any edited content to the host app.
// This template doesn't do anything, so we just echo the passed in items.
self.extensionContext!.completeRequest(returningItems: self.extensionContext!.inputItems, completionHandler: nil)
}
}
I'm implementing a barcode scanner for my app using the google books api. The data gets fetched successfully and stored in the proper textfields:
func readInJSON(controller: UIViewController, title: String, author: String, imageLink: String) {
self.titleTextField.text = title
self.authorTextField.text = author
}
However the text does not actually appear in the textFields until I tap on the them, then the text populates. I'm wondering if there is some way to have the text appear without having a user tap on the text fields themselves.
Maybe you are not on the main thread. UI operation should be done on the main thread.
Try this
dispatch_async(dispatch_get_main_queue()) {
self.titleTextField.text = title
self.authorTextField.text = author
}
I am working with IOS by using xamarin ,I am new to technology I have one requirement ,That is I don't want to allow any special character in my text box .How can I do this any one help me
thanks
What you can do is use ShouldChangeCharacters delegate.
Using it you can decide weather or not to update the text of the UITextField.
For example lets say you have a UITextField named textField:
textField.ShouldChangeCharacters = (textField, range, replacementString) => {
if (isSpecialCharacter) {
return false;
} else {
return true;
}
};