250x250
반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- ToDoRim
- UIAccessibility
- algorithm
- toyproject
- keyWindow
- reetcode
- Widget
- IOS
- flutter
- GIT
- Leetcode
- basic
- swiftlint
- SwiftGen
- Swift
- it
- Equatable
- enumerations
- listview
- tip
- OSLog
- xcode
- pubspec.yaml
- pubspec
- designPattern
- Extentsion
- dart
- protocol
- github
- COMMIT
Archives
- Today
- Total
수니의 개발새발
[iOS/Swift] 외부 브라우저(사파리)로 링크 열기 본문
728x90
반응형
📌 이번 글은
외부 브라우저(사파리)로 링크를 여는 방법입니다.
UIApplication.shared.open()
func openExternalLink(urlStr: String, _ handler:(() -> Void)? = nil) {
guard let url = URL(string: urlStr) else {
return
}
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:]) { (result) in
handler?()
}
} else {
UIApplication.shared.openURL(url)
handler?()
}
}
사파리로 링크를 여는 함수입니다.
👩🏻💻 사용 예제
import UIKit
class Utils {
/**
# openExternalLink
- Parameters:
- urlStr : String 타입 링크
- handler : Completion Handler
- Note: 외부 브라우저로 링크 오픈
*/
static func openExternalLink(urlStr: String, _ handler:(() -> Void)? = nil) {
guard let url = URL(string: urlStr) else {
return
}
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:]) { (result) in
handler?()
}
} else {
UIApplication.shared.openURL(url)
handler?()
}
}
}
공통적으로 사용하는 변수 및 함수들을 Utils 라는 공통 클래스를 생성하여 관리하는 예시입니다.
Utils.openExternalLink(urlStr: "https://www.google.co.kr/")
필요한 곳 어디서든 생성한 함수를 호출하여 사용합니다.
728x90
반응형
'iOS - Swift' 카테고리의 다른 글
[iOS/Swift] 시간을 다른 형태로 변형하기 (DateFormatter) (0) | 2021.12.29 |
---|---|
[iOS/Swift] 현재 시간의 밀리초 구하기 (0) | 2021.12.29 |
[iOS/Swift] 시뮬레이터(Simulator) 구동 여부 확인하기 (0) | 2021.12.29 |
[iOS/Swift] 디바이스 고유넘버(device uuid) 구하기 (0) | 2021.12.29 |
[iOS/Swift] 현재 번들 버전(Bundle Version) 구하기 (0) | 2021.12.29 |
Comments