2010-12-10 17 views
7

En mi aplicación de iPhone, necesito mostrar el hipervínculo a un sitio web¿Cómo agregar un hipervínculo en la aplicación iPhone?

¿Cómo puedo agregar un hipervínculo para un sitio web en la programación de iPhone ?.

En realidad, quiero pasar el enlace de mi aplicación a Facebook usando facebook API.

Entonces, ¿cómo debo formatear mi texto para que funcione como hipervínculo en Facebook?

Todavía estoy atascado con este problema.

Respuesta

12

Depende de dónde desee colocar este enlace. Si está en una UITextView, solo tiene que habilitarla.

textView.text = @"Some text with link in it : http://http://stackoverflow.com"; 
textView.dataDetectorTypes = UIDataDetectorTypeLink; 

Más información sobre iOS reference library.

O, si desea abrir Safari mediante programación:

NSURL *url = [ [ NSURL alloc ] initWithString: @"http://stackoverflow.com" ]; 
[[UIApplication sharedApplication] openURL:url]; 
[url release]; 

Si desea compartir en Facebook, tiene que decirle a Safari para abrir una URL que se mostrará una página de Facebook que permite al usuario compartir . Aquí está un ejemplo:

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 
+0

En realidad, quiero pasar el enlace de mi aplicación a Facebook usando facebook API. Entonces, ¿cómo debo formatear mi texto para que funcione como hipervínculo en Facebook? –

+0

Ok, eso es diferente. Entonces, ¿tiene un enlace que desea que los usuarios puedan compartir en su perfil de Facebook? – Julien

+0

Ok, he actualizado mi pregunta – Julien

1

Parth

Puede mencionar en donde es necesario insertar hyperlink, esta es mi manera de insertar link de aplicación para el iPhone:

  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>"]; 
    } 
+0

funcionó para mí ... gracias –

0

Mi biblioteca favorita para compartir sería ShareKit. Es realmente fácil de configurar en su aplicación, y permitirá a las personas compartir a través de Facebook y otros servicios también. También es personalizable, por lo que puedes personalizarlo, cambiar los colores y controlar qué servicios deseas compartir con los usuarios.

Salida, http://www.getsharekit.com/

Una vez que lo agregue a su aplicación se puede integrar muy fácilmente mediante la creación de un botón

[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction 
target:self 
action:@selector(share)]; 

//add the button to a navigation bar or tool bar. 

y luego crear el método de participación en su controlador.

- (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]; 
} 
1

Arrastre un TextView en su Vista Controlador y seleccione es atribuir inspector:

  1. Establecer el texto como "Mi sitio web: www.mywebsitelink.com" y asegúrese de que tipo de texto debe ser claro .
  2. Marque su comportamiento como seleccionable y no editable.
  3. Marque la detección como Enlaces.

Ejecute su proyecto. Puede ver el enlace en View Controller. Haga clic en el enlace, que lo llevará a Safari y abrirá una nueva pestaña en él.

+0

esta es la respuesta correcta – NSurajit

+0

por favor favor de votar si le parece útil. – Sanket

Cuestiones relacionadas