はじめに
そもそも知らない遷移もあるので引き出しを増やすためにもまとめたいと思います。
そもそも外部遷移とは
他のアプリやWebページ、システム設定などにユーザーを導く操作を指します。
アプリからSafari開いたり、iPhoneの設定画面を開かせるアレです。
外部遷移一覧
さっそく見ていきます! それぞれで必要なimportは記載していません。
UIImagePickerControllerを使用したカメラまたは写真ライブラリの起動
教材でよく見るやつです。
let imagePicker = UIImagePickerController()
imagePicker.sourceType = .camera // または .photoLibrary
present(imagePicker, animated: true, completion: nil)
UIApplicationを使用した外部ブラウザでのWebページの遷移
Webページへの遷移でよく使うのはこちらです。
if let url = URL(string: "https://example.com") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
Safariを使用したWebページへの遷移
SafariServicesははじめて知りました。コンテンツブロックなどSafariServicesのみで提供されているAPIもあるようでSFSafariViewControllerDelegateを使ってユーザーのアクションを検知できます。
if let url = URL(string: "https://example.com") {
let safariViewController = SFSafariViewController(url: url)
present(safariViewController, animated: true, completion: nil)
}
UIDocumentInteractionControllerを使用したドキュメントの共有
ちょっと何言ってるか分からないです。
let documentInteractionController = UIDocumentInteractionController(url: documentURL)
documentInteractionController.presentOptionsMenu(from: view.bounds, in: view, animated: true)
外部アプリケーションの起動
他のアプリを起動する方法です。連携できるアプリとかで使えそうです。
if let url = URL(string: "external-app://") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
ユニバーサルリンク(DeepLink)を使用した外部アプリケーションへの遷移
Webサイト等のリンクをクリックした時にiOSのアプリを起動させる手法です。
if let url = URL(string: "myapp://specific-screen") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
UIApplicationを使用した電話の発信
発信を数行で実装できるのは驚きでした
if let phoneURL = URL(string: "tel://1234567890") {
UIApplication.shared.open(phoneURL, options: [:], completionHandler: nil)
}
MapKitを使用した地図アプリへの遷移
マップを開くのも思ったより簡単ですね。
if let coordinates = CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194) {
let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinates, addressDictionary: nil))
mapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving])
}
MessageUIを使用したメールアプリへの遷移
純正のメールアプリ使う人いるのでしょうか。
if MFMailComposeViewController.canSendMail() {
let mailComposer = MFMailComposeViewController()
mailComposer.setToRecipients(["recipient@example.com"])
mailComposer.setSubject("Subject")
mailComposer.setMessageBody("Body", isHTML: false)
mailComposer.mailComposeDelegate = self
present(mailComposer, animated: true, completion: nil)
}
AVPlayerViewControllerを使用した動画の再生
純正のPlayerはPicture in Pictureも使えて結構便利な印象です。
if let videoURL = URL(string: "https://example.com/video.mp4") {
let player = AVPlayer(url: videoURL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
present(playerViewController, animated: true) {
playerViewController.player?.play()
}
}
App Storeへのリンク
idのあとにアプリ固有のIDが入ります。ストアにリリースしないと割り振られないので注意です。
if let url = URL(string: "itms-apps://itunes.apple.com/app/id123456789") {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
終わりに
思っている以上に外部遷移の手段がありました。Webに遷移するにも2種類あったり電話を発信する方法は発見でした。
UIActivityVC等含めるか悩みましたが外部遷移ではないと思い外しています。漏れがあったらすみません。
最後に
私の働いている会社で経験の有無を問わず採用を行っています。
興味のある方は是非カジュアル面談から応募してみてください!