LoginSignup
7
8

iOS 外部遷移まとめ

Posted at

はじめに

そもそも知らない遷移もあるので引き出しを増やすためにもまとめたいと思います。

そもそも外部遷移とは

他のアプリやWebページ、システム設定などにユーザーを導く操作を指します。
アプリからSafari開いたり、iPhoneの設定画面を開かせるアレです。

外部遷移一覧

さっそく見ていきます! それぞれで必要なimportは記載していません。

UIImagePickerControllerを使用したカメラまたは写真ライブラリの起動

教材でよく見るやつです。

Swift
let imagePicker = UIImagePickerController()
imagePicker.sourceType = .camera // または .photoLibrary
present(imagePicker, animated: true, completion: nil)

UIApplicationを使用した外部ブラウザでのWebページの遷移

Webページへの遷移でよく使うのはこちらです。

Swift
if let url = URL(string: "https://example.com") {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

Safariを使用したWebページへの遷移

SafariServicesははじめて知りました。コンテンツブロックなどSafariServicesのみで提供されているAPIもあるようでSFSafariViewControllerDelegateを使ってユーザーのアクションを検知できます。

Swift
if let url = URL(string: "https://example.com") {
    let safariViewController = SFSafariViewController(url: url)
    present(safariViewController, animated: true, completion: nil)
}

UIDocumentInteractionControllerを使用したドキュメントの共有

ちょっと何言ってるか分からないです。

Swift
let documentInteractionController = UIDocumentInteractionController(url: documentURL)
documentInteractionController.presentOptionsMenu(from: view.bounds, in: view, animated: true)

外部アプリケーションの起動

他のアプリを起動する方法です。連携できるアプリとかで使えそうです。

Swift
if let url = URL(string: "external-app://") {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

ユニバーサルリンク(DeepLink)を使用した外部アプリケーションへの遷移

Webサイト等のリンクをクリックした時にiOSのアプリを起動させる手法です。

Swift
if let url = URL(string: "myapp://specific-screen") {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

UIApplicationを使用した電話の発信

発信を数行で実装できるのは驚きでした

Swift
if let phoneURL = URL(string: "tel://1234567890") {
    UIApplication.shared.open(phoneURL, options: [:], completionHandler: nil)
}

MapKitを使用した地図アプリへの遷移

マップを開くのも思ったより簡単ですね。

Swift
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を使用したメールアプリへの遷移

純正のメールアプリ使う人いるのでしょうか。

Swift
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も使えて結構便利な印象です。

Swift
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が入ります。ストアにリリースしないと割り振られないので注意です。

Swift
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等含めるか悩みましたが外部遷移ではないと思い外しています。漏れがあったらすみません。

最後に

私の働いている会社で経験の有無を問わず採用を行っています。
興味のある方は是非カジュアル面談から応募してみてください!

7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8