[iOS] Share Sheet를 통해 다른 앱에서 파일 받기

leeyoung·2022년 6월 15일
0

목표

iOS에서 파일 공유하기시 Share에 아이콘이 나타나고 그 share 클릭시 앱을 통해 열 수 있도록 하기

앱이 지원하는 File Type 정의

  • Info.plist에 CFBundleDocumentTypes를 넣어 앱이 지원하는 파일 타입을 정의한다.
	<key>CFBundleDocumentTypes</key>
	<array>
		<dict>
	        <key>CFBundleTypeName</key>
			<string>All Files</string>
			<key>CFBundleTypeIconFiles</key>
			<array/>		
			<key>LSHandlerRank</key>
			<string>Alternate</string>
			<key>LSItemContentTypes</key>
			<array>
				<string>public.data</string>
				<string>public.content</string>
			</array>
		</dict>
	</array>
  • CFBundleDocumentTypes dictionary key값들
    • CFBundleTypeName: 정의한 Document Type 이름
    • LSHandlerRank: 특정 파일 형식을 지원하겠다고 선언된 Editor나 View들 중 앱의 순위. (Owner > Default > Alternate > None)
    • LSItemContentTypes: 지원하는 파일 UTI types
    • CFBundleTypeIconFiles: 파일 아이콘 이미지 이름들
  • 이렇게 하면 지원하는 파일 타입의 파일을 공유하기 했을 때 Share sheet에 아이콘이 표시되고 그 아이콘을 클릭하면 앱이 open

앱 열린 이후 후처리

  • AppDelegate의 application:willFinishLaunchingWithOptions: 또는application:didFinishLaunchingWithOptions:로 파일 정보가 들어온다
  • 파일 정보(dictionary)
    • UIApplicationLaunchOptionsURLKey: 파일 URL
    • UIApplicationLaunchOptionsSourceApplicationKey: open request를 한 앱의 bundle id string
    • UIApplicationLaunchOptionsAnnotationKey: 파일 관련 property들

?? 남은 궁금점

참고

1개의 댓글

comment-user-thumbnail
2024년 2월 14일

사진앱에서는 동작안하는거죠..? 파일앱에서만 내 앱이 뜨네요..

답글 달기