How to add hyperlink in iPhone app? - iphone

In my iPhone app, I require to show the hyperlink to a website
How can I add hyperlink for a website in iPhone programming?.
Actually i want to pass the link from my app onto facebook using facebook API.
So how should I format my text such that it works as hyperlink on facebook?
I am still stuck with this issue.

It depends on where you want to put this link.
If it is in a UITextView, you just have to enable it.
textView.text = #"Some text with link in it : http://http://stackoverflow.com";
textView.dataDetectorTypes = UIDataDetectorTypeLink;
More info on iOS reference library.
Or, if you want to open Safari programmatically:
NSURL *url = [ [ NSURL alloc ] initWithString: #"http://stackoverflow.com" ];
[[UIApplication sharedApplication] openURL:url];
[url release];
If you want to share on Facebook, you need to tell Safari to open a URL which will display a Facebook page that allows the user to share. Here is an example:
NSString *urlString = #"http://stackoverflow.com";
//The url you want to share
NSString *title = "The Title of the Page";
//The title you want to be displayed on Facebook
NSString *shareUrlString = [NSString stringWithFormat:#"http://www.facebook.com/sharer.php?u=%#&t=%#", urlString , title];
//Create the URL string which will tell Facebook you want to share that specific page
NSURL *url = [ [ NSURL alloc ] initWithString:shareUrlString ];
//Create the URL object
[[UIApplication sharedApplication] openURL:url];
//Launch Safari with the URL you created
[url release];
//Release the object if you don't need it

Drag a textView in your View Controller and select it's attribute inspector:
Set your text as "My website: www.mywebsitelink.com" and make sure
that text type should be plain.
Tick your behavior as Selectable and not Editable.
Tick on detection as Links.
Run your project. You can see link in View Controller. Click on link, that will take you to safari and open a new tab on it.

Parth
Can you mention where you need to insert hyperlink ,this is my way to insert link in iPhone app:
NSMutableString *err=[[NSMutableString alloc] init];
[err appendString:#"<html><body style=\"background-color:black\" ><font size=3 face=\"helvetica\" color=\"white\" align=\"center\"><H3>Login Error</H3><p> "];
[err appendString:#"Please try again. If you forgot your password, you can retrieve it at<font color=\"red\"> ENTER LINK TEXT</font>.</font></p></body></html>"];
}

My favorite library for sharing would be ShareKit. It's really easy to setup in your app, and will allow people to share via Facebook, and other services too. It's customizable too, so you can skin it, change colors, and control which services you want users to share with.
Check out, http://www.getsharekit.com/
Once you add it to your app you can integrate it very easily by creating a button
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:#selector(share)];
//add the button to a navigation bar or tool bar.
and then creating the share method in your controller.
- (void)share
{
// Create the item to share (in this example, a url)
NSURL *url = [NSURL URLWithString:#"http://getsharekit.com"];
SHKItem *item = [SHKItem URL:url title:#"ShareKit is Awesome!"];
// Get the ShareKit action sheet
SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
// Display the action sheet
[actionSheet showFromToolbar:navigationController.toolbar];
}

extension NSAttributedString{
add a new swift file for adding link in ios
static func makeHyperlink(for path: String, in string: String, as substring: String) -> NSAttributedString {
let nsString = NSString(string: string)
let substringRange = nsString.range(of: substring)
let attributedString = NSMutableAttributedString(string: string)
attributedString.addAttribute(.link, value: path, range: substringRange)
return attributedString
}
}
And then use this makehyperlink function in your text view function.
let attributedString = NSAttributedString.makeHyperlink(for: link, in: paragraph, as: "text to use for link")

Related

iOS 7 UIActivityViewController Email Attachments

I have reviewed a bunch of posts here, numerous online tutorials/sample code and I'm stumped. In my app I have no problem displaying the UIActivityController natively provided by iOS7 with the sharing options appropriate to my app (AirDrop and mail).
The specific problem I'm having is getting my saved document attached to the email message when the user selects the option to share via mail. The message body is being set to the text, but the attachment is MIA. The relevant code is:
// Generate the XML file to be shared for the currently displayed record...
NSURL *url = [self createShareFile];
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:#[#"Data shared from my app.", url] applicationActivities:nil];
// Filter out the sharing methods we're not interested in....
controller.excludedActivityTypes = #[UIActivityTypePostToTwitter, UIActivityTypePostToFacebook,
UIActivityTypePostToWeibo,
UIActivityTypeMessage,
UIActivityTypePrint, UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo];
// Now display the sharing view controller.
[self presentViewController:controller animated:YES completion:nil];
What am I missing? My file is being properly created, the contents is correct and the NSURL object contains the proper path to the file.
Thanks!
Problem solved.....
The code posted in my original post is 100% accurate. The issue ended up being in the way I was constructing the NSURL being returned in my createShareFile method:
Incorrect (original way):
return [NSURL URLWithString:[docFile stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Correct way:
return [NSURL fileURLWithPath:docFile];
As soon as I fixed that, it started working, even with my custom file type.
I had a similar problem where the mail app was the only one I couldn't add a pdf to. Here is my code in Swift as well as handling iPad popup.
var filesToShare = [Any]()
filesToShare.append(self.myUrl)
let activityViewController = UIActivityViewController(activityItems: filesToShare as [Any], applicationActivities: nil)
present(activityViewController, animated: true)
// for iPad -> where to present on screen
if let popOver = activityViewController.popoverPresentationController {
//action button being my top left icon
popOver.barButtonItem = self.actionButton
}
My issue was as well handling the URL differently

How to make a text in a label as url, to move to webpage / make a call by click on a text in a label

In my application I have displayed some content in a tableview with custom cell.
Custom cell having an imageView, button, 4 labels
Each label for
1. Name of the candidate
2. Web page address,
3. Email address
4. Phone number.
In this I need to highlight the web address label , phone number label and email address label like url's and to do respective actions when user touch them.
like
1. open web page when touch on web address label,
2. open email composer sheet when touch on email address label,
3. make a call when touch on phone number label
It's pretty much all the same idea, all applications in iOS can be opened with using the format appShortName://whatyouwantToHandle, for websites its just http://myurl
To make a call, it's tel:
NSURL *phoneURL = [NSURL URLWithString:#"tel:+123456789"];
[[UIApplication sharedApplication] openURL:phoneURL];
To start composing an email, it's mailto:, and it supports GET parameters to fill-out the email.
const NSString* mailFormat = #"mailto:%#?subject=%#&body=%#";
const NSString* mailEmails = #"person1#gmail.com,person2#gmail.com";
const NSString* mailBody = #"This is the mail body\n Hi!");
const NSString* mailSubject = #"This is the mail Subject!";
NSString *urlString = [NSString stringWithFormat:(NSString*)mailFormat,[mailEmails stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[mailSubject stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[mailBody stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURL *mailURL = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:mailURL];
And to open the web page, as you'd expect, it's just http:
NSURL *websiteURL = [NSURL URLWithString:#"http://mywebsite.com"];
[[UIApplication sharedApplication] openURL:websiteURL];
Check the documentation on -openURL: if you want to know more.
Make all UILabel's user interaction enabled. Apply tap gesture on each label and implement function call on tap of each label. Implement functions for each label suggested as above.

How to get selection text from UIWebView?

, Hello, everyone.
I'm developing my app which has a feature to post article from website.
This is a picture of UIWebview with UIMenuController.
It was possible to get event when user tap the button. but I can't find the way to get the text the user selected.
In UITextView case, it has 'selectedRange' property, so it is easy to get selection text.
UIMenuItem *facebookMenuItem = [[UIMenuItem alloc] initWithTitle:#"Facebook" action:#selector(facebookMenuItemTapped:)];
UIMenuItem *twitterMenuItem = [[UIMenuItem alloc] initWithTitle:#"Twitter" action:#selector(twitterMenuItemTapped:)];
[UIMenuController sharedMenuController].menuItems = [NSArray arrayWithObjects: facebookMenuItem, twitterMenuItem, nil];
[twitterMenuItem release];
[facebookMenuItem release];
I would like to get the selection text on UIWebView.
Does anybody have an idea or hint?
Thanks.
Or if you donʾt want to mess with Javascript you can just copy the selection to the pasteboard and then retrieve it by running:
[[UIApplication sharedApplication] sendAction:#selector(copy:) to:nil from:self forEvent:nil];
NSString *text = [UIPasteboard generalPasteboard].string;

TTStyledTextLabel

I'm trying to get this control to work with a hypertext link with not much success. I have looked at TTCalaog and tried to replecate but does not work.
I have this working as far as displaying the hypertext link but it does not fire.
TTStyledTextLabel* label = [[[TTStyledTextLabel alloc] initWithFrame:CGRectMake(5, 0, 315, 175)] autorelease];
NSString* labelText = #"This should work";
label.text = [TTStyledText textFromXHTML:labelText lineBreaks:NO URLs:YES];
[self.view addSubview:label];
I thing I am missing the point here perhaps with the placement of the google url? I have seen a post on this forum that makes use of custom-uri://some/url that is then set up in TTURLMap and TTNavigator, but I need to open a url from the hypertext in a webview so I need the url to run a method in my class that creates my webview controller etc.
I have tried to cusomise TTURLMap to work without a TTNavigator but completely pickled?
Any help gratefullt appreciated ;-)
Thanks
I've just found myself a solution to catch the clicked URL on a TTStyledTextLabel. I hope this could help in your case too.
This's what I have done.
1. Create TTNavigator
TTNavigator *navigator = [TTNavigator navigator];
navigator.persistenceMode = TTNavigatorPersistenceModeNone;
navigator.delegate = self;
2. Create TTNavigatorDelegate
As you assigned self as delegate of the navigator object. Therefore, please remember to add protocol in the header file .h before you continue.
In the implementation, create this method
- (BOOL) navigator:(TTBaseNavigator *)navigator shouldOpenURL:(NSURL *)URL {
// Now you can catch the clicked URL, and can do whatever with it
// For Example: In my case, I take the query of the URL
// If no query is available, let the app open the URL in Safari
// If there's query, get its value and process within the app
NSString *query = URL.query;
if (query == nil) {
return YES;
} else {
// process the query
}
}
I hope this helps! Please vote for me if this helps to solve your issue!
Best Regards,
Thang

If statement in Xcode to determine which HTML page is being loaded by a UIWebView

I have a UIWebView on my interface and I have added a UIToolbar with a next button and previous button. When I press the next button, I want the WebView to load a different HTML page. BUT I also want to run an if statement to determine which HTML page the WebView currently has in it.
So For example.
If the UIWebView has index.html loaded in it,
when the user presses the next button I want it to load about.html.
If the UIWebView has about.html loaded in it,
when the user presses the next button I want it to load contact.html. etc.
Here is the way I am setting the WebView:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"PrepareEquipment" ofType:#"html"]isDirectory:NO]]];
Thanks guys!
Stefan.
How about a simple array or other collection where you store all your pages in the order they should be shown?
NSArray *pagesArray = [NSArray arrayWithObjects:#"index.html", #"about.html", #"contact.html", nil];
NSString *currentPage = [pagesArray objectAtIndex:0];
...
- (NSString *)getNextPage {
int pageIndex = [pagesArray indexOfObject:currentPage];
++pageIndex;
pageIndex%= [pagesArray count];
NSString *nextPage = (NSString *)[pagesArray objectAtIndex:pageIndex];
currentPage = nextPage;
return nextPage;
}
... and a similar method for going back?
Completely untested...