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 lin
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];
}
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:
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.
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")
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<a href=\"ENTER YOUR LINK URL\"><font color=\"red\"> ENTER LINK TEXT</font></a>.</font></p></body></html>"];
}