Clickable link in ios? - iphone

In my app i am using UITextView to display three lines. In that i need to add link for certain text. I am searched in google and SO, but i couldn't found the exact solution for my problem.
I Tried this but its not working
NSString *testString = #"This will go to Google";
[webview loadHTMLString:testString baseURL:baseURL];
[lblLinksTitle setText:testString];
I want Google as clickable text which have to open in safari browser. May be its simple. As i am new to iOS i couldn't figure out to do this.
Thanks for your help guys.
Much appreciated.

Just use UIWebView, don't use UiTextView
NSString *testString = #"This will go to Google";
[webview loadHTMLString:testString baseURL:nil];
webview.delegate=self;
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSString * requestString=[[request URL] absoluteString];
//NSLog(#"%# is requestString from clicking",requestString );
if ([[[request URL] absoluteString] hasPrefix:#"http://www.google"]) {
[[UIApplication sharedApplication] openURL:[request URL]];
}
return TRUE;
}

Try using this:
lblLinksTitle.editable = NO;
lblLinksTitle.dataDetectorTypes = UIDataDetectorTypeLink;
lblLinksTitle.text = testString;

Related

Redirecting Url to app in iphone for linkedIn

I am trying to authorize LinkedIn with iPhone..
I am using following code to redirect url
NSString *authUrl = [NSString stringWithFormat:#"https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=%#&scope=%#&state=%#&redirect_uri=%#" ,
API_KEY ,
#"r_fullprofile",
#"ASDKASIIWER23432KKQ",
#"http://www.myappname.com"
];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: authUrl]];
In my Url types, i have added URL Scheme as http:// and url identifier as
www.myappname.com
however after authorizing , i don't get back to my application from browser.
Any ideas where i am wrong?
i have used a diff approach now, i have used webView inside app and is using it. Works perfect so far.
- (void)viewDidLoad
{
[super viewDidLoad];
[self.myWebView setDelegate:self];
self.indicator = [[CustomActivityViewer alloc] initWithView:self.view];
NSString *authUrl = [NSString stringWithFormat:#"https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=%#&scope=%#&state=%#&redirect_uri=%#" ,
API_KEY ,
#"r_fullprofile rw_nus r_emailaddress r_network w_messages",
SCOPE_CODE
REDIRECT_URI
];
authUrl = [authUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:authUrl]]];
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
[self.indicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[self.indicator stopAnimating];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[self.indicator stopAnimating];
}
- (BOOL) webView: (UIWebView *) webView shouldStartLoadWithRequest: (NSURLRequest *) request navigationType: (UIWebViewNavigationType) navigationType
{
NSURL *url = request.URL;
NSLog(#"%#", url.absoluteString);
if ( [url.host isEqualToString:HOST])
{
URLParser *parser = [[URLParser alloc] initWithURLString:url.absoluteString];
NSString *code = [parser valueForVariable:#"code"];
if (code != nil)
{
NSString *authUrl = [NSString stringWithFormat:#"https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code=%#&redirect_uri=%#&client_id=%#&client_secret=%#",
code,
REDIRECT_URI_OAUTH,
API_KEY,
SECRET_KEY];
NSLog(#"%#" , authUrl);
authUrl = [authUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[Utilities responseFromURL:[NSURL URLWithString:authUrl] completionBlock:^(NSString *response, NSError *err)
{
if (err != nil)
{
[Utilities errorDisplay];
}
else
{
NSDictionary *results = [response JSONValue];
[defaults setObject:[results objectForKey:#"access_token"] forKey:#"access_token"];
}
}];
}
}
return YES;
}
You are going to need to register a custom URL scheme, as iOS and OS X don't have a way to redirect a specific host within a URL scheme.
Generally, you can use something like x-myapp: as the url scheme.
Further, the URL Identifier is not a host, but an identifier that describes the URL scheme, much like a UTI identifier for a file type describes a specific file type. For example, you could use com.myCompany.myApp.url or something similar as the identifier.
You should be fine if you create a scheme of form x-myapp: and then use that as the redirect URL.
An example from a proposed Info.plist would be:
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>com.myCompany.myApp.url</string>
<key>CFBundleURLSchemes</key>
<array>
<string>x-myapp</string>
</array>
</dict>
The CFBundleURLName corresponds in the Xcode GUI to URL Identifier.

Best way to have article text with links on iPhone?

I am making an iPhone app that has a bunch of "articles" with certain words that I want to link to other "articles". I need to display the articles in UITableViewCells.
If there is a way to do this without a web view, that's great.
If I need to use a web view, should I just make <a href> style links? How can I have it jump to the article without creating a bunch of actual html pages, just have it let my program know that they clicked on that link and then the program goes and finds the appropriate article from a plist or something?
I've create a test demo using UIWebView and it works fine.
Create two HTML files:home.html and first.html.
// home.html
Home page. first
// first.html
First page. home
//viewDidLoad
self.webView.delegate = self;
NSString *htmlFilePathString = [[NSBundle mainBundle] pathForResource:#"home" ofType:#"html"];
NSURL *htmlFileURL = [[NSURL alloc] initFileURLWithPath:htmlFilePathString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:htmlFileURL];
[self.webView loadRequest:request];
// webView's delegate:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}

loading links in in-app browser in a web view in iphone

I have some local html files in resources.on launch one html,say a.html, is loaded in webview. there are some hyperlinks in that webview(or say the html content) which are links to other html files in my resources.now on click of those links I want to open them in in-app browser.
how to do this.
Any idea????
thnx!!!!
I've not tried this, but it might work!
Create your UIWebView and load the first html file from your bundle
NSString *file = [[NSBundle mainBundle] pathForResource:#"a" ofType:#"html"];
NSString *html = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:[NSString stringWithFormat:#"<html><head></head><body>%#</body></html>", html] baseURL:baseURL];
webView.delegate = self;
Then get when the user taps a link
-(BOOL)webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if (inType == UIWebViewNavigationTypeLinkClicked) {
// Find which link is selected, get the file and load it into the UIWebView
}
return YES;
}

UIWebView links opening in Safari

I want to open an iTunes link in my webView, but when the webView launches the page, it redirects to the Safari browser. There, the url is getting opened, but I want it to open in my webView.
- (void)viewDidLoad {
NSString *urlAddress = #"http://itunes.apple.com/us/app/foodcheck-traffic-light-nutrition/id386368933?mt=8";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
[super viewDidLoad];
}
Please suggest a way to sort out this problem.
You may want to try logging the load requests when you run the app. It may be that apple is automatically changing http:// to itms-apps or http://phobos or something along these lines. If so, then you can block the load when it's call using something like this:
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType;
{
NSURL *loadURL = [[request URL] retain];
NSLog(#"%#",loadURL);
if([[loadURL absoluteString] hasPrefix:#"http://"])
{
[loadURL release];
return TRUE;
}
[loadURL release];
return FALSE;
}
Good luck. I'm curious to know what finally works.
A note from the Apple reference documents- Q: How do I launch the App Store from my iPhone application? Also, how do I link to my application on the store?
Note: If you have iTunes links inside
a UIWebView, you can use this
technique after intercepting the links
with the -[UIWebViewDelegate webView:shouldStartLoadWithRequest:navigationType:]
delegate method.
Not sure if you ever got it working, but this works well for me:
NSString *responseString = [NSString stringWithContentsOfURL:myURL];
[self.webView loadHTMLString:responseString baseURL: nil)];

custom buttons on uiwebview(iphone)

I want to add custom button on webView. they should also should be there when i try anything in url.
how is it possible??
basically i want to put buttons on uiwebView and they are custom buttons
//edited code...
I am doing this...here link is appearing but method is not getting called...and there wasnot any error in your code ..:)
NSString *imagePath = [[NSBundle mainBundle] resourcePath];
imagePath = [imagePath stringByReplacingOccurrencesOfString:#"/" withString:#"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSString *HTMLData = #"<htmlClick me!--></style><br><br>";
[webView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: [NSString stringWithFormat:#"file:/%#//",imagePath]]];
and then
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// only do something if a link has been clicked...
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
// check if the url requests starts with our custom protocol:
if ([[[request URL] absoluteString] hasPrefix:#"button://"]) {
// Do custom code
return NO;
}
}
return YES;
}
You'll just have to use links and style them.
Something like:
Click me!
Have a look at http://www.cssbuttongenerator.com/, very easy to create your own button and let it generate the css code for you. You'll actualy have to click on the button create itself to generate the code.
Execute custom code by clicking on a link(button) in html
First of all you've got to conform to the UIWebViewDelegate protocol, and set the delegate accordingly.
Then implement shouldStartLoadWithRequest.
You button links should look like this:
Click me!
We are using a custom protocol we make up: button://.
Now implement shouldStartLoadWithRequest like this:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// only do something if a link has been clicked...
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
// check if the url requests starts with our custom protocol:
if ([[[request URL] absoluteString] hasPrefix:#"button://"]) {
// Do custom code
return NO;
}
}
return YES;
}
That's it.