Notice
Recent Posts
Recent Comments
Link
관리 메뉴

김종권의 iOS 앱 개발 알아가기

[iOS - swift] 2. UIActivityViewController 인터페이스 제공 방법 (share extension, share sheet) - UI 구현, 데이터 교환(NSExtensionActivationRule) 본문

iOS 응용 (swift)

[iOS - swift] 2. UIActivityViewController 인터페이스 제공 방법 (share extension, share sheet) - UI 구현, 데이터 교환(NSExtensionActivationRule)

jake-kim 2024. 2. 19. 01:29

1. UIActivityViewController 인터페이스 제공 방법 (share extension, share sheet) - Share Extension 생성까지

2. UIActivityViewController 인터페이스 제공 방법 (share extension, share sheet) - UI 구현

3. UIActivityViewController 인터페이스 제공 방법 (share extension, share sheet) - 데이터 교환

지난 포스팅 글까지 한 것) Share Extension 생성

  • 앱에 Target으로 Share Extension을 추가하여 아래처럼 ExInstagramShareExtension 모듈이 추가된 상태

UI 구현

  • 자동으로 생성된 ShareViewController 확인
  • SLComposeServiceViewController부터 메서드들까지 모두 샘플코드이므로 UIViewController로 변경할것
import UIKit
import Social

class ShareViewController: SLComposeServiceViewController {
    override func isContentValid() -> Bool {
        // Do validation of contentText and/or NSExtensionContext attachments here
        return true
    }
    
    override func didSelectPost() {
        // This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
        
        // Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
        self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
    }
    
    override func configurationItems() -> [Any]! {
        // To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
        return []
    }
    
}
  • 수정한코드
class ShareViewController: UIViewController {
    private let label = {
        let label = UILabel()
        label.text = "test"
        label.font = .systemFont(ofSize: 20, weight: .regular)
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .lightGray.withAlphaComponent(0.8)
        
        view.addSubview(label)
        NSLayoutConstraint.activate([
            label.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            label.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        ])
    }
}
  • Xcode에서 scheme을 Share Extension 모듈로 설정

  • cmd + R로 실행하면 Share Extension을 테스트할 수 있는 앱선택 가능
    • Safari 선택

  • 공유하기 > Share Sheet에서 위에서 만들었던 앱 클릭
    • test label하나 구현된것 확인 완료

* 이어서 데이터 교환 방법에 대한 내용은 다음 포스팅 글 참고

 

* 전체 코드: https://github.com/JK0369/ExShareInterface

* 참고

- https://developer.apple.com/documentation/foundation/app_extension_support/supporting_suggestions_in_your_app_s_share_extension

- https://developer.apple.com/documentation/uikit/uiactivityviewcontroller

- https://royhelen.tistory.com/25

Comments