2010-08-31 24 views

Trả lời

26

Thật dễ dàng. Bạn đặt targetselector cho nút, sau đó bên trong selector, bạn gọi safari để mở liên kết của mình.

Mã gọi Safari:

Objective-C

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

Swift 2.x

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

Swift 3.x

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

Trong Swift: 'UIApplication.sharedApplication(). OpenURL (NSURL (string:" https://www.google.com ")!)' –

2

Tạo nút và đặt mục tiêu cho công cụ chọn mở Safari bằng liên kết.

dụ cơ bản:

Thực hiện một UIButton

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

Sau đó, phương pháp này để mở URL

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

Đừng quên để cung cấp cho nút của bạn một khung thích hợp và tiêu đề và thêm nó vào chế độ xem của bạn.

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

openURL bị phản đối từ iOS 10 và sử dụng sau đây để thay thế.

UIApplication *application = [UIApplication sharedApplication]; 
NSURL *url = [NSURL URLWithString:@"http://www.yourDomain.com"]; 
[application openURL:url options:@{} completionHandler:nil]; 
Các vấn đề liên quan