2010-08-31 10 views

Respuesta

26

Es fácil. Usted configura el target y selector para el botón, luego dentro del selector, llama a safari para abrir su enlace.

código para llamar a Safari:

Objective-C

- (void)buttonPressed { 
    [[UIApplication sharedApplication] 
     openURL:[NSURL URLWithString: @"https://www.google.co.uk"]]; 
} 

2.x Swift

UIApplication.sharedApplication().openURL(NSURL(string: "https://www.google.co.uk")!) 

3.x Swift

UIApplication.shared.openURL(URL(string: "https://www.google.co.uk")!) 
+0

En Swift:. 'UIApplication.sharedApplication() openURL (NSURL (cadena: "https://www.google.com"))' –

2

Cree un botón y dele un objetivo a un selector que abra Safari con el enlace.

Ejemplo básico:

Hacer un UIButton

UIButton *button = [[UIButton alloc] initWithFrame:...]; 
[button addTarget:self action:@selector(someMethod) forControlEvents:UIControlEventTouchUpInside]; 

A continuación, el método para abrir la URL

-(void)someMethod { 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://www.google.ca"]]; 
} 

No se olvide de dar a su botón de un marco adecuado y título, y para agregarlo a su vista.

0
- (IBAction)buyNowButtonPressed { 
    NSString *s = [ NSString stringWithFormat:@"http://www.sample.com/buynowpage"]; 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:s]]; 
} 
0

openURL es obsoleto de iOS 10 y el uso siguiente en su lugar.

UIApplication *application = [UIApplication sharedApplication]; 
NSURL *url = [NSURL URLWithString:@"http://www.yourDomain.com"]; 
[application openURL:url options:@{} completionHandler:nil]; 
Cuestiones relacionadas