copy HTML to UIPasteboard - iphone

I want to transfer HTML from my app to the iPhone mail application.
I already have the HTML text - lest say <span style='color:red'>Test</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.

Related

Paste from the clipboard in iOS

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.

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.

How to register a custom app opening URL scheme with Xcode 4?

Xcode4 is asking for a huge number of arguments just to make this simple thing possible:
NSString *stringURL = #"twitterriffic://";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
What are all these properties for? Why an image? Must I repeat the app identifier here? What role to choose if I want anyone to be able to call this URL to open my app? And what are these Additional url type properties for?
I found no Xcode4-related tutorial how to register such an URL scheme with Xcode 4.
Open "Supporting Files" (folder) on left and click the "YourAppName-Info.plist"
Choose a row like "Bundle creator OS Type Code" and mouse over row and click the (+) symbol
This creates a new row and type "URL types"
Click the arror to left and see Item 0 and you'll rename the value in Item 0 to "URL Schemes" as shown
Then edit the field in Item 0 and type in your prototocol; I typed in "goomzee" as shown
Now if I install this app on my simulator, and open Safari and type "goomzee://" in the address bar it will launch my app.
Yup, this stuff isn't straightforward is it ?
I've outlined the steps required to register a custom URL here:
Custom URLs
But, basically the key to it is setting up a "URL Types" value in your .plist file. Here's what it looks like in XCode 5:
In this example, I've registered the MKB prefix, so now, I can use this new type of URLs in hyperlinks on webpages, and emails (if I read an email in the iPad Mail app):
If a user taps on one of these links, my iPad app will start up, and I can access the full URL string, to extract the other parameters from the URL (eg "DocumentNumber=100")
The only gotcha is that I have yet to work out how to test whether a user has an iPad app installed which can recognise a particular prefix.
If they haven't, and they tap on one of these MKB:// links on their iPad, they'll get an ugly error message:
You can continue to register your custom URL by editing your app's info.plist file (as shown in one of your previous questions). The new editor in Xcode 4 is supposed to be a convenience to make it easier to add the entries - all it is doing is making the same info.plist changes that you would do manually. If you want to use the new editor then you only need to fill in the 'Identifier' and the 'URL Schemes' boxes. You don't need an image, icon or additional URL properties.
The other properties (icon etc) are not well documented but seem to be applicable only on Mac OS X and might be used in the Finder's Get Info dialog to display what types of URL a particular app can open. For example, take a look at the Launch Services Programming Guide.
What you change in that editor is also reflected in your project's info plist file. It takes the form of ProjectName-Info.plist. Hopefully that helps.

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.

iPhone Localization: simple project not working

Im doing my first localized project and I've been fighting with it for several hours with no luck.
I have to create an app that, based on the user selection, shows texts and images in different languages.
I've read most of Apple's documents on the matter but I cant make a simple example work.
This are my steps so far:
1) Create a new project.
2) Manually create a "en.lproj" directory in the projects folder.
3) Using TexEdit create file called "Localizable.strings" and store it in Unicode UTF-16. The file looks like this:
/*
Localizable.strings
Multilanguage02
Created by Gonzalo Floria on 5/6/10.
Copyright 2010 __MyCompanyName__. All rights reserved.
*/
"Hello" = "Hi";
"Goodbye" = "Bye";
4) I drag this file to the Resources Folder on XCode and it appear with the "subdir" "en" underneath it (with the dropdown triangle to the left). If I try to see it on XCode it looks all wrong, whit lots of ? symbols, but Im guessing thats because its a UTF-16 file. Right?
5) Now on my view did load I can access this strings like this:
NSString *translated;
translated = NSLocalizedString(#"Hello", #"User greetings");
NSLog(#"Translated text is %#",translated);
My problem is allowing the user to switch language. I have create an es.lproj with the Localizable.strings file (in Spanish), but I CANT access it.
I've tried this line:
[[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObjects:#"es", nil] forKey:#"AppleLanguages"];
But that only works the NEXT time you load the application. Is there no way to allow the user to switch languages while running the application??
Do I have to implement my own Dictionary files and forget all about NSLocalizableString family?
Thanks for ANY advice or pointers.
Gonso
There is already a discussion about that here.
Their suggestion is to create a sub-bundle and then use a method called NSLocalizedStringFromTableInBundle(...) (as described in the reference manual) to
get a localized string from a specific table in a bundle.
I am just giving you an hunch, I haven't tried but, I guess, this could be a good way to face your problem.