Monday, June 3, 2013

UIWebView dataDetectorTypes example in Objective C (iOS).


UIWebView dataDetectorTypes

The types of data converted to clickable URLs in the web view’s content.

@property(nonatomic) UIDataDetectorTypes dataDetectorTypes

Discussion of [UIWebView dataDetectorTypes]
You can use this property to specify the types of data (phone numbers, http links, email address, and so on) that should be automatically converted to clickable URLs in the web view. When clicked, the web view opens the application responsible for handling the URL type and passes it the URL.

UIWebView dataDetectorTypes example.
webview = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,460)];
webview.opaque = NO;
webview.backgroundColor = [UIColor clearColor];
webview.userInteractionEnabled = YES;
webview.dataDetectorTypes = UIDataDetectorTypeAll;

Example of [UIWebView dataDetectorTypes].
// ...

- (void)webViewDidStartLoad:(UIWebView *)theWebView
{
    theWebView.dataDetectorTypes = UIDataDetectorTypeAll ^ UIDataDetectorTypePhoneNumber;   
    return [ super webViewDidStartLoad:theWebView ];
}

// ...

UIWebView dataDetectorTypes example.
NSString *webViewBody = [[body stringByReplacingOccurrencesOfString:@"\t"       withString:@" "] stringByReplacingOccurrencesOfString:@"\n" withString:@"
"];

NSString *str = [NSString stringWithFormat:@"

 %@ 
",body];
        [webView loadHTMLString:webViewBody baseURL:nil];

        webView.dataDetectorTypes = UIDataDetectorTypeAll;

End of UIWebView dataDetectorTypes example article.