Paste from the clipboard in iOS - iphone

So I have 2 apps. One is a sensors app (built with XCode) that records data (text) with hardware wireless sensors. The other is a checklist/reference manual (built with Titaniam Appcelerator). Using custom URL schemes, they can instantiate each other.
What I am trying to do is paste any text data the sensors app copies to the clipboard into a text field in the reference manual app. I have a UIWebview showing html pages (the checklist) with a text box displayed now. To demo the capability, I have to touch the field and select paste. I was thinking that javascript might work, but all my research poo poo's that idea. Any thoughts about how to grab the text that is on the clipboard and display it programmatically in the reference manual app without having to touch the field and select paste?
Should I even be looking at the clipboard or should I be looking into modifying the custom URL scheme to pass data that way instead?

To get the text from the clipboard:
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSString *string = pasteboard.string;
if (string) {
// Do something
}
For more funcional communication between apps, take a look at URL Schemes.

So, I figured out a way to pass the data in the url with this tutorial. At the bottom it describes how to pass data after you set up the URL id for each app. Hope this helps someone.

Pasting in Swift
Get the pasteboard string with UIPasteboard.generalPasteboard().string.
The string is optional, so it must be unwrapped before being used.
if let pasteboardString = UIPasteboard.generalPasteboard().string {
// use the string, for example:
myTextView.insertText(pasteboardString)
}
Note
The original question is asking for something more complex than this. However, most people come here based on the question title rather than question content, so that I what I am answering here.

Related

Word learning of UITextChecker, Swift -- not Working

I want my code to add custom words like 'nooooober' into my iphone dictionary. I write below code. But Still UItextField auto-fill is not showing me 'nooooober' word in autofill options.
Here is my code, written in DidFinishlaunching of appdelegate
if (UITextChecker.hasLearnedWord("nooooober")){
UITextChecker.learnWord("nooooober")
}
Thanks,
Aaban.
Text display and fonts UITextChecker works in the context of a single piece of text, like a document.
So any words that you ignore/learn aren't added globally for the device, just the current context that it is being used on. You wont be able to 'learn' words in your app and then expect auto-fill to use them in another app, such as Messages

copy paste text and image both to clipboard in iphone app

I am working on an app where I want to copy some text and image and allow user to paste it anywhere. I know it is done using UIPasteboard and I have implemented copying of image but now I want to copy image and text both and then let user paste it. There can be several images and text messages which can come in any order. It is like a paragraph being written with text and images. Is this possible? Can someone suggest me how can I achieve it?
Regards
Pankaj
You can put anything you wish in the pasteboard, including multiple entries like of type UIPasteboardTypeListString and another of type UIPasteboardTypeListImage, and even another of type #"My Made-Up Type". Think of it as a shared mutable dictionary.
It's up to the receiving application to understand what to do with them.

Using iOS 5 rich text editor

as you know the Mail app in iOS 5 have a rich text editor is there any possible way to use this feature for a regular UITextView ?
I know of two fundamental approaches to creating a rich text editor in iOS 5:
Use Core Text and a custom view. I don't have any experience with this approach.
Use a UIWebView (instead of a UITextView) and the contentEditable HTML attribute. The basic idea is to load a custom HTML document from your app resources directory. The bare minimum that it needs is this:
<div contentEditable>TEXT_PLACEHOLDER</div>
To initialize the rich text editor view:
1. Load the contents of this file into an NSMutableString and replace the TEXT_PLACEHOLDER string with the text you want to edit.
2. Send the loadHTMLString:baseURL: message to the UIWebView with that HTML string.
Now you have a UIWebView displaying your text inside a div with contentEditable. At this point, you should be able to run your app tap on the text, and be presented with a cursor and be able to add/remove text. The next step is to add rich text formatting functionality. This is done with a set of simple javascript function calls. See the Mozilla documentation on contentEditable for a great reference. You will also want to add a Javascript function to your HTML template file like this:
function getHtmlContent() { return document.getElementById('myDiv').innerHTML; }
So you can easily retrieve the edited text as HTML using [myWebView stringByEvaluatingJavaScriptFromString:#"getHtmlContent()"]. You can also add custom context menu items like you show in the screen shot in your question.
If you have access to the Apple iOS dev center, the WWDC session Rich Text Editing in Safari on iOS talks all about this approach.
A variation of this approach is to use a third-party rich text editor like TinyMCE. I've heard of some success with integrating this into a UIWebView in iOS 5. Again this relies on the contentEditable attribute.
Here is my implementation. Still haven't added UIMenuController functionality, but it's planned to be added soon.
https://github.com/aryaxt/iOS-Rich-Text-Editor
The iOS 5 rich text edit control is also present in the notes app in iOS 4 (make a rich text note on the computer and sync it to see).
This is a custom Apple-made control which they use in their own apps, but it is not published in any official developer API. It's probably in the SDK somewhere, but because it is undocumented, even if you find it and use it, Apple will reject your app.
Basically, if you want a rich text control you will have to make your own.
Edit: Try using this: https://github.com/omnigroup/OmniGroup/tree/master/Frameworks/OmniUI/iPad/Examples/TextEditor/. I haven't used it, so I don't know how well it will work. (Link from this question)
look at https://github.com/gfthr/FastTextView which is more good open source editor than Omni editor

copy HTML to UIPasteboard

I want to transfer HTML from my app to the iPhone mail application.
I already have the HTML text - lest say &LT;span style='color:red'>Test&LT;/span>
I can place this to UIPasteBoard - but when I paste it to mail I get the html source.
When I place the same string in a HTMLView - select it there and copy it it pastes as red text in mail.
What do I have to do to place the string in the UIPasteBoard so that it pastes as red text to the mail application?
I've been searching for "format types" - and found that UIPasteBoard returns "Aplle Web Archive pasteboard type" when I have the element (copied from UIWebView) in the clipboard.
But setting this as type when adding the content to UIPasteBoard pastes nothing in the mail app.
Manfred
That is not true. you can paste ANYTHING to the pasteboard, go read the docs.
I've finally put together a tutorial that shows how to copy HTML into the Mail app.
http://mcmurrym.wordpress.com/2010/08/13/pasting-simplehtml-into-the-mail-app-ios/
I've got HTML copy working so it pastes into the built-in Mail and Notes apps properly. It looks like this:
NSString *htmlContent = #"This is <span style='font-weight:bold'>HTML</span>";
NSString *content = #"This is HTML!";
NSDictionary *dict = #{(NSString *)kUTTypeText: content, (NSString *)kUTTypeHTML: htmlContent};
[[UIPasteboard generalPasteboard] setItems:#[dict]];
To get access to those type constants, you need to import this:
#import <MobileCoreServices/UTCoreTypes.h>
on that same link you gave in your comment, you'll find this paragraph at the top.
A Uniform Type Identifier (UTI) is frequently used for a representation type (sometimes called a pasteboard type). For example, you could use kUTTypeJPEG (a constant for public.jpeg) as a representation type for JPEG data. However, applications are free to use any string they want for a representation type; however, for application-specific data types, it is recommended that you use reverse-DNS notation to ensure the uniqueness of the type (for example, com.myCompany.myApp.myType).
Right below it is a link to here. http://developer.apple.com/iphone/library/documentation/FileManagement/Conceptual/understanding_utis/understand_utis_intro/understand_utis_intro.html#//apple_ref/doc/uid/TP40001319
Which explains UTIs.
Finally this link gives you SEVERAL types http://developer.apple.com/iphone/library/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref/doc/uid/TP40009259-SW1
Of course that list isn't ALL types as you can create your own types.
I have successfully pasted html into the mail app. I'll give you a good place to start...
Create an app that will show the data types in the pasteboard. Goto safari on the device, copy a web page. Run your app, You'll notice the pasteboard type is "Apple Web Archive pasteboard type." Note that this is really a pasteboard type (a custom one). If you try to duplicate the safari mobile copy and paste feature yourself by creating a web archive and attempt to paste it as text into the mail app, it will show the web archive file as raw xml. If you define the type as "Apple Web Archive pasteboard type" the mail app will actually format the paste as html.
If you want to know what a web archive looks like. On desktop safari, just save a web page as an archive and look at the file in a text reader (text edit will try to parse it, so you might use a different program to look at the archive xml).
Please read all of the documentation as you can discover that you can do custom types in the link that you sent me.
It is absolutely possible. UIPasteboard usually contains multiple values, indexed by their UTI.
Here is a working example of how to get public.html as a primary type.
-(NSString * _Nullable) getHtmlFromPB {
static NSArray * prefs = #[#"public.html",#"public.utf8-plain-text",#"public.plain-text"];
if (!UIPasteboard.generalPasteboard.hasStrings) return nil;
for (NSString * theone in prefs) {
if ([[UIPasteboard generalPasteboard].pasteboardTypes indexOfObject:theone] == NSNotFound) continue;
return [UIPasteboard.generalPasteboard.items.firstObject objectForKey:theone];
}
return nil;
}
If you need explaination: prefs array contains preferred UTI for Pasteboard content in desired order. So, it will take one of them or nothing.
No, it cannot. UIPasterBoard only accepts strings, images, URLs and colors.

How do I know if text contains URL links (UIWebView)

I have some text which might or might not contain web URLs, phone numbers, email links etc. which UIWebView automatically detects as hotspots.
Question: I want to show this text in UIWebView ONLY when there are one or more hotspots, but as plain text if it doesn't. So how can I detect this in code?
Additional Info: JavaScript code below tells how many ... links there are. This does NOT count how many other "link" items there are. For example "Link to www.yle.fi" contains one link according to UIWebView, but zero according to JavaScript:
NSString *s = [webView stringByEvaluatingJavaScriptFromString:
#"document.links.length"];
Still no answer to the question how to ask UIWebView how many links it has found...
You can use several regular expressions and check if the text matches those of URL/phone number/email addresses.
However, if your intention is simply let the user open a link, the UITextView is suffice.
Check the dataDetectorTypes property.