Skip to content

πŸ“€ The missing UIActivityViewController for SwiftUI

Notifications You must be signed in to change notification settings

rebeloper/ShareSheetView

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“€ ShareSheetView

swift v5.3 platform iOS deployment target iOS 13

ShareSheetView is a lightweight library that creates a UIActivityViewController in SwiftUI.

πŸ’» Installation

πŸ“¦ Swift Package Manager

Using Swift Package Manager, add it as a Swift Package in Xcode 11.0 or later, select File > Swift Packages > Add Package Dependency... and add the repository URL:

https://github.com/rebeloper/ShareSheetView.git

✊ Manual Installation

Download and include the ShareSheetView folder and files in your codebase.

πŸ“² Requirements

  • iOS 13+
  • Swift 5.3+

πŸ‘‰ Import

Import ShareSheetView into your View

import ShareSheetView

🧳 Features

Here's the list of the awesome features ShareSheetView has:

  • UIActivityViewController in SwiftUI
  • ShareSheetButton

πŸ’» How to Use

Here are the ways that you can create a new ShareSheetView:

  1. Create a @State variable to hold the ShareSheetView presentation
  2. Create a Button that triggers a sheet view-modifier that has a ShareSheetView as its content
import SwiftUI
import ShareSheetView

struct ContentView: View {
    
    // 1.
    @State private var isShareSheetViewPresented = false
    
    var body: some View {
        VStack(spacing: 12) {
            
            // 2.
            Button(action: {
                isShareSheetViewPresented = true
            }, label: {
                Text("Share")
            })
            .sheet(isPresented: $isShareSheetViewPresented, content: {
                ShareSheetView(activityItems: getActivityItems(), applicationActivities: getApplicationActivities(), excludedActivityTypes: getExcludedActivityTypes()) { (activityType, finished, activityItems, error) in
                    if let error = error {
                        print(error.localizedDescription)
                        return
                    }
                    
                    if let activityType = activityType {
                        print(activityType)
                    }
                    
                    print(finished)
                    
                    if let activityItems = activityItems {
                        print(activityItems)
                    }
                }
            })
            
            Spacer()
        }
        .padding()
    }
    
    func getActivityItems() -> [Any] {
        return ["Hello ShareSheetView"]
    }
    
    func getApplicationActivities() -> [UIActivity]? {
        return nil
    }
    
    func getExcludedActivityTypes() -> [UIActivity.ActivityType]? {
        return [.postToFacebook]
    }
}

Or use the more convinient ShareSheetButton:

import SwiftUI
import ShareSheetView

struct ContentView: View {
    
    var body: some View {
        VStack(spacing: 12) {
            
            ShareSheetButton { () -> ShareSheetView in
                ShareSheetView(activityItems: getActivityItems())
            } label: { () -> AnyView in
                AnyView(
                    Image(systemName: "square.and.arrow.up")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 32, height: 32)
                )
            }
            
            Spacer()
        }
        .padding()
    }
    
    func getActivityItems() -> [Any] {
        return ["Hello ShareSheetView"]
    }
    
}

πŸͺ Demo project

For a comprehensive Demo project check out: ShareSheetViewDemo

✍️ Contact

rebeloper.com / YouTube / Shop / Mentoring

πŸ“ƒ License

The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.