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

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.

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.

detecting URL and format it in UITextField

I would like to be able to format a URL in my UITextField nicely. So say the text of the UITextField is
textField.text = #"Hi this is a page for my website http://www.mywebsite.com";
I would like it to automatically underline this mark it as a hyperlink and when a user taps on the link it opens up the site. This is similar to any twitter client that you have out there (tweetdeck, tweetbot, etc). How do I do this the easy way?
You can use a UITextView instead and set the dataDetectorTypes property to the types of links you want to be able to handle.
hey this is really simple, you can set the UITextFiled to detect Links, like so:
myTextView.dataDetectorTypes = UIDataDetectorTypeAll
check out my tutorial on this at my site for more info
http://www.sdkboy.com

Extract image URL from HTML text

In Objective-C what is the best method to extract an image url from HTML text?
I've got a chunk of HTML text which may contain one or more images. I want to be able to get the src URL of each image.
If you don't mind limiting yourself to iOS 4, you can use the new text checking APIs. The one you want is NSTextCheckingTypeLink and the NSRegularExpression class.
This seems to be answered over here: parsing HTML on the iPhone
The second answer down has a detailed explanation, however you will just have to change the xpath expression to match your image tags.

How to get rid from question mark in a blue square in UIWebView?

In my iPhone application I'm parsing RSS feed to get html and keep it. Afterwards I display this html in UIWebView. The problem is that html contains urls of images, if there is network connection everything is ok, UIWebView loads and displays these images, but if there is no connection it shows text but in place of images shows frame with blue square inside
How can I get rid of it?
Thank you
Check programmatically to make sure that you have an internet connection.
See this question and its accepted answer on how to do that.
If you don't have an internet connection, then you have a couple of options. You can either:
Parse through your html, replace the <img> tags with blanks (this will completely get rid of the images and their associated blue question mark boxes.
Parse through your html, replace the src part of <img src="somewebsite"> with a reference to an image placeholder in the project bundle.
XPath is your friend when it comes to parsing html. Although if you wanted to, you could do all of this with NSSTring.

Best approach for simple link within UITextView

I am creating a dictionary lookup application. The user selects a word from a UITableView and the app displays the definition. In some cases the word will be similar to another word, so I want to display "See Also:" followed by a list of similar words that when touched, bring up another definition.
In searching here on links within UITextViews, most of the answers involve linking out to the web, which is not really what I need. I simply want to get control when the user touches a word so that I can change the view.
Is UIWebView the only way to do this, or did I miss something obvious in the SDK? Also, I'd prefer to stay within the native SDK and not go the three20 route.
Thanks!
I would use another UITableView to make this work. Your list of similar words will probably be in NSArray format already, so it would be pretty easy to set up another UITableView instead of a UITextView to display the list, and given that you already have this code working for the main UITableView, you already know how to make them clickable!
A UIWebView will be the only thing that suits here I'm afraid. Data detectors are the only way to link inside of a UITextView, and they will only respond to the appropriate data types (Phone number, web page, address)...
Links can be done the normal way:
<a href='http://someotherword'>someotherword</a>
Setup the webviewdelegate to snag any link requests (and prevent them from being opened in the browser) so that you can open them in your own handler:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
{
if(navigationType == UIWebViewNavigationTypeOther) return YES; // Allow direct loading
NSString *myWord = [[request URL] host];
// do something with myWord... say open another word
return NO; // Don't let the browser actually perform this navigation
}