UIActivityViewController
: A view controller that you use to offer standard services from your app.
The system provides several standard services, such as copying items to the pasteboard, posting content to social media sites, sending items via email or SMS, and more. Apps can also define custom services.
이미지, URL, 텍스트 등의 정보를 복사, AirDrop, 메세지로 보내기, 카카오톡 공유, 메모에 저장 등으로 쉽게 공유할 수 있도록 해주는 컨트롤러. 쉽게 말해 앱에서 '공유하기' 기능을 구현해주는 시스템이다.
다음과 같이 사용할 수 있다.
let items = ["이걸 복사할거야"] //text
let ac = UIActivityViewController(activityItems: items, applicationActivities: nil)
ac.popoverPresentationController?.sourceView = self.view
self.present(ac, animated: true)
let items = [URL(string: "https://www.hongssup.tistory.com")!] //url
array를 받아와 공유해주는 것이기 때문에 다음과 같이 Any 타입으로 여러개의 데이터를 공유해줄 수도 있다.
let items: [Any] = ["이걸 복사할거야", URL(string: "https://www.hongssup.tistory.com")!]
이미지 공유도 마찬가지로 해주면 되는데, 사진 라이브러리에 저장하기 위해서는 info.plist에 [Privacy - Photo Library Usage Description]을 추가해주고 코드를 한 줄 더 추가해주어야 한다.
let ac = UIActivityViewController(activityItems: [image], applicationActivities: nil)
ac.excludedActivityTypes = [.saveToCameraRoll] //이 코드가 없으면 공유하기 화면에서 save Image 누르면 crash 발생
present(ac, animated: true)
대리야 코드에서는 다음과 같이 추천 메세지를 카카오톡으로 공유할 수 있도록 구현이 되어있는데,
popoverPresentationController는 왜 사용되는지 모르겠다.
이걸 원래 기본 코드로 같이 넣어주는 것 같기도 하고.
@IBAction func onClickBtn(_ sender: UIButton) {
switch sender.tag {
case kTagButtonRecommeded: //추천하기
let message : String! = Utils.getPreference(key: USERDEFAULTS_KEY_KAKAO_MESSGAE)
let array : Array = [message]
let VC = UIActivityViewController(activityItems: array as [Any], applicationActivities: nil)
VC.popoverPresentationController?.sourceView = self.view
self.present(VC, animated: true, completion: nil)
break
}
}
참고 : devsc.tistory, hackingwithswift
'초보 iOS 개발자의 일상 > 개발 업무' 카테고리의 다른 글
[Swift iOS] JSON parsing : ObjectMapper vs. Codable (0) | 2021.05.31 |
---|---|
[Swift iOS] UIView layout update cycle & methods (0) | 2021.05.28 |
[Swift iOS] UserDefaults 사용 및 문자열 치환 replacingOccurrences (0) | 2021.05.26 |
[Swift iOS] in-app purchase using StoreKit (0) | 2021.05.25 |
[iOS] Firebase Hosting - ipa 파일 다운로드 링크 생성 (AdHoc & Enterprise) (0) | 2021.05.17 |