├── .gitignore ├── CHANGELOG.md ├── Communicado.podspec ├── Example ├── Communicado.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── Communicado │ ├── AppDelegate.swift │ ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json │ ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard │ ├── Info.plist │ └── ViewController.swift ├── Images ├── cartman.gif ├── homer.gif ├── styled_messages.png └── yay.gif ├── LICENSE ├── Package.swift ├── README.md └── Sources └── Communicado ├── Attachment.swift ├── AttachmentType.swift ├── ShareDestination.swift ├── ShareParameters.swift └── UIViewController+Sharing.swift /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata 19 | 20 | ## Other 21 | *.xccheckout 22 | *.moved-aside 23 | *.xcuserstate 24 | *.xcscmblueprint 25 | 26 | ## Obj-C/Swift specific 27 | *.hmap 28 | *.ipa 29 | 30 | ## Playgrounds 31 | timeline.xctimeline 32 | playground.xcworkspace 33 | 34 | # Swift Package Manager 35 | # 36 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 37 | # Packages/ 38 | .build/ 39 | 40 | # CocoaPods 41 | # 42 | # We recommend against adding the Pods directory to your .gitignore. However 43 | # you should judge for yourself, the pros and cons are mentioned at: 44 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 45 | # 46 | # Pods/ 47 | 48 | # Carthage 49 | # 50 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 51 | # Carthage/Checkouts 52 | 53 | Carthage/Build 54 | 55 | # fastlane 56 | # 57 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 58 | # screenshots whenever they are needed. 59 | # For more information about the recommended setup visit: 60 | # https://github.com/fastlane/fastlane/blob/master/docs/Gitignore.md 61 | 62 | fastlane/report.xml 63 | fastlane/screenshots 64 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | # 3.2.0 (2020-01-19) 4 | 5 | - Adding Swift 5.1 support. 6 | - Adding SPM support. 7 | 8 | # 3.1 (2018-12-26) 9 | 10 | - Adding Swift 4.2 support. 11 | 12 | # 3.0.1 (2017-10-06) 13 | 14 | - Adding public initializers for `ShareDestination`s. 15 | 16 | # 3.0 (2017-09-11) 17 | 18 | - The underlying framework is completely rewritten. If you find any bugs, please report them. 🐛 19 | 20 | - This version is written in and supports Swift 4. If you need to use Swift 3, use version 2.0.2 of Communicado instead. 21 | 22 | --- 23 | 24 | #### New Features 25 | 26 | - You can now style the background color, `UINavigationBar`'s `titleTextAttributes` (on `MFMailComposeViewController` only), and `UIBarButtonItem`'s `titleTextAttributes` on `MFMailComposeViewController` and `MFMailComposeViewController` and `MFMessageComposeViewController`. 27 | 28 | - You can now style multiple parts of the `MFMailComposeViewController` and `MFMessageComposeViewController` experience. 29 | 30 | ##### MFMailComposeViewController 31 | - `UINavigationBar.titleTextAttributes` 32 | - `UINavigationBar.backgroundColor` 33 | - `UIBarButtonItem.titleTextAttributes` 34 | 35 | ##### MFMessageComposeViewController 36 | - `UINavigationBar.backgroundColor` 37 | - `UIBarButtonItem.titleTextAttributes` 38 | 39 | --- 40 | #### ⚠️ Breaking changes ⚠️ 41 | 42 | - You must now implement `SharingCapableViewController` on any `UIViewController` you wish to share from. The API will be unavailable otherwise. 43 | 44 | - `TwitterShareDestination`, `FacebookShareDestination`, `TencentWeiboShareDestination`, and `SinaWeiboShareDestination` are deprecated in iOS 11 because Apple deprecated the `Social` framework. They are still supported in iOS 9 and 10. 45 | 46 | - `TextShareParameters` has been renamed `MessagesShareParameters`. 47 | 48 | - `PasteboardShareParameters` now accepts a `PasteboardShareParameters.Value` with a string, image, or url, rather than potentially all three. 49 | 50 | - `SharingType` has been renamed `ShareResult`. 51 | 52 | # 2.0.2 (2017-03-05) 53 | 54 | - Most of the checks preventing sharing if it wasn't *possible* were based on whether `UIActivityViewController` was around, which is always true. 55 | 56 | Since this was due to bad copy/pasting, next time I will write things out by hand, on a blackboard, as proper punishment. 57 | 58 | # 2.0.1 (2016-12-03) 59 | 60 | - `ActivityShareParameters` allow for `Any` `applicationActivities`, rather than `AnyObject` to support Swift 3 Foundation converted types (like `URL` instead of `NSURL`). 61 | 62 | # 2.0 (2016-09-14) 63 | 64 | ### This release is a completely breaking change to the API. 65 | 66 | - The library is now compatible with Swift 3 only. The previous release will continue to work with Swift 2.2. 67 | 68 | - This version removes FacebookSDK support, until better Swift compatibility is provided. 69 | 70 | - There are ShareDestination providers now, which you configure with lightweight `ShareParameter` configuration structs. 71 | 72 | - There is only one method, `.share`, which you call with the proper `ShareParameter` struct. 73 | 74 | A simple example, for text messages looks like this: 75 | 76 | ```swift 77 | let heartImageData = UIImagePNGRepresentation(myHeartImage) 78 | let attachment = Attachment(attachmentType: AttachmentType.png, filename: "heart.png", data: heartImageData) 79 | let textParameters = TextShareParameters(message: "I love my users.", attachments: [ attachment ]) 80 | self.share(textParameters) 81 | ``` 82 | 83 | Steve Holt! \o/ 84 | 85 | # 1.4 (2016-08-13) 86 | 87 | - After thinking about it, Instagram is not a good candidate to be in Communicado, since it is non-standard behavior, and relies on a workaround Instagram may drop support for. 88 | 89 | 90 | # 1.3.3 (2016-05-13) 91 | 92 | - Allowing sharingCompleted event to be nil. 93 | 94 | # 1.3.2 (2016-04-12) 95 | 96 | - Removing Facebook subspec. 97 | 98 | # 1.3.1 (2016-04-11) 99 | 100 | - Adding method to save an image to the clipboard. 101 | 102 | # 1.3 (2016-02-21) 103 | 104 | - Adding Facebook SDK sharing for those that don't want to use iOS's limited Facebook SDK integration. 105 | 106 | 107 | # 1.2 (2016-02-18) 108 | 109 | - Adding public modifiers for more sharing. 110 | 111 | # 1.1.1 (2016-02-17) 112 | 113 | - MessageAttachment's initializer is now public so it can be used in a framework. 114 | 115 | # 1.1.0 (2016-02-17) 116 | 117 | - Adding Instagram and Save to camera roll. 118 | 119 | # 1.0 (2016-02-17) 120 | 121 | - Initial release. 122 | -------------------------------------------------------------------------------- /Communicado.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |spec| 2 | spec.name = 'Communicado' 3 | spec.version = '3.2.0' 4 | spec.license = { :type => 'MIT', :file => 'LICENSE' } 5 | spec.homepage = 'https://github.com/mergesort/Communicado' 6 | spec.authors = { 'Joe Fabisevich' => 'github@fabisevi.ch' } 7 | spec.summary = 'The easiest way to share from your app to anywhere, because you\'ve got so much to say!' 8 | spec.source = { :git => 'https://github.com/mergesort/Communicado.git', :tag => "#{spec.version}" } 9 | spec.source_files = 'Sources/Communicado/*.swift' 10 | spec.framework = 'Foundation', 'MessageUI', 'Photos', 'Social' 11 | spec.requires_arc = true 12 | spec.social_media_url = 'https://twitter.com/mergesort' 13 | spec.ios.deployment_target = '9.0' 14 | spec.swift_version = '5.1' 15 | end 16 | -------------------------------------------------------------------------------- /Example/Communicado.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 48; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | B76E5F5F2380E47000736662 /* AttachmentType.swift in Sources */ = {isa = PBXBuildFile; fileRef = B76E5F5A2380E47000736662 /* AttachmentType.swift */; }; 11 | B76E5F602380E47000736662 /* Attachment.swift in Sources */ = {isa = PBXBuildFile; fileRef = B76E5F5B2380E47000736662 /* Attachment.swift */; }; 12 | B76E5F612380E47000736662 /* UIViewController+Sharing.swift in Sources */ = {isa = PBXBuildFile; fileRef = B76E5F5C2380E47000736662 /* UIViewController+Sharing.swift */; }; 13 | B76E5F622380E47000736662 /* ShareParameters.swift in Sources */ = {isa = PBXBuildFile; fileRef = B76E5F5D2380E47000736662 /* ShareParameters.swift */; }; 14 | B76E5F632380E47000736662 /* ShareDestination.swift in Sources */ = {isa = PBXBuildFile; fileRef = B76E5F5E2380E47000736662 /* ShareDestination.swift */; }; 15 | B7FDCA351F649D4600E82AAE /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7FDCA341F649D4600E82AAE /* AppDelegate.swift */; }; 16 | B7FDCA371F649D4600E82AAE /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7FDCA361F649D4600E82AAE /* ViewController.swift */; }; 17 | B7FDCA3A1F649D4600E82AAE /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B7FDCA381F649D4600E82AAE /* Main.storyboard */; }; 18 | B7FDCA3C1F649D4600E82AAE /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B7FDCA3B1F649D4600E82AAE /* Assets.xcassets */; }; 19 | B7FDCA3F1F649D4600E82AAE /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B7FDCA3D1F649D4600E82AAE /* LaunchScreen.storyboard */; }; 20 | /* End PBXBuildFile section */ 21 | 22 | /* Begin PBXFileReference section */ 23 | B76E5F5A2380E47000736662 /* AttachmentType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AttachmentType.swift; sourceTree = ""; }; 24 | B76E5F5B2380E47000736662 /* Attachment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Attachment.swift; sourceTree = ""; }; 25 | B76E5F5C2380E47000736662 /* UIViewController+Sharing.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIViewController+Sharing.swift"; sourceTree = ""; }; 26 | B76E5F5D2380E47000736662 /* ShareParameters.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShareParameters.swift; sourceTree = ""; }; 27 | B76E5F5E2380E47000736662 /* ShareDestination.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShareDestination.swift; sourceTree = ""; }; 28 | B7FDCA311F649D4600E82AAE /* Communicado.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Communicado.app; sourceTree = BUILT_PRODUCTS_DIR; }; 29 | B7FDCA341F649D4600E82AAE /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 30 | B7FDCA361F649D4600E82AAE /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 31 | B7FDCA391F649D4600E82AAE /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 32 | B7FDCA3B1F649D4600E82AAE /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 33 | B7FDCA3E1F649D4600E82AAE /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 34 | B7FDCA401F649D4600E82AAE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 35 | /* End PBXFileReference section */ 36 | 37 | /* Begin PBXFrameworksBuildPhase section */ 38 | B7FDCA2E1F649D4600E82AAE /* Frameworks */ = { 39 | isa = PBXFrameworksBuildPhase; 40 | buildActionMask = 2147483647; 41 | files = ( 42 | ); 43 | runOnlyForDeploymentPostprocessing = 0; 44 | }; 45 | /* End PBXFrameworksBuildPhase section */ 46 | 47 | /* Begin PBXGroup section */ 48 | B76E5F582380E47000736662 /* Sources */ = { 49 | isa = PBXGroup; 50 | children = ( 51 | B76E5F592380E47000736662 /* Communicado */, 52 | ); 53 | name = Sources; 54 | path = ../../Sources; 55 | sourceTree = ""; 56 | }; 57 | B76E5F592380E47000736662 /* Communicado */ = { 58 | isa = PBXGroup; 59 | children = ( 60 | B76E5F5A2380E47000736662 /* AttachmentType.swift */, 61 | B76E5F5B2380E47000736662 /* Attachment.swift */, 62 | B76E5F5C2380E47000736662 /* UIViewController+Sharing.swift */, 63 | B76E5F5D2380E47000736662 /* ShareParameters.swift */, 64 | B76E5F5E2380E47000736662 /* ShareDestination.swift */, 65 | ); 66 | path = Communicado; 67 | sourceTree = ""; 68 | }; 69 | B7FDCA281F649D4600E82AAE = { 70 | isa = PBXGroup; 71 | children = ( 72 | B7FDCA331F649D4600E82AAE /* Communicado */, 73 | B7FDCA321F649D4600E82AAE /* Products */, 74 | ); 75 | sourceTree = ""; 76 | }; 77 | B7FDCA321F649D4600E82AAE /* Products */ = { 78 | isa = PBXGroup; 79 | children = ( 80 | B7FDCA311F649D4600E82AAE /* Communicado.app */, 81 | ); 82 | name = Products; 83 | sourceTree = ""; 84 | }; 85 | B7FDCA331F649D4600E82AAE /* Communicado */ = { 86 | isa = PBXGroup; 87 | children = ( 88 | B76E5F582380E47000736662 /* Sources */, 89 | B7FDCA341F649D4600E82AAE /* AppDelegate.swift */, 90 | B7FDCA361F649D4600E82AAE /* ViewController.swift */, 91 | B7FDCA381F649D4600E82AAE /* Main.storyboard */, 92 | B7FDCA3B1F649D4600E82AAE /* Assets.xcassets */, 93 | B7FDCA3D1F649D4600E82AAE /* LaunchScreen.storyboard */, 94 | B7FDCA401F649D4600E82AAE /* Info.plist */, 95 | ); 96 | path = Communicado; 97 | sourceTree = ""; 98 | }; 99 | /* End PBXGroup section */ 100 | 101 | /* Begin PBXNativeTarget section */ 102 | B7FDCA301F649D4600E82AAE /* Communicado */ = { 103 | isa = PBXNativeTarget; 104 | buildConfigurationList = B7FDCA431F649D4600E82AAE /* Build configuration list for PBXNativeTarget "Communicado" */; 105 | buildPhases = ( 106 | B7FDCA2D1F649D4600E82AAE /* Sources */, 107 | B7FDCA2E1F649D4600E82AAE /* Frameworks */, 108 | B7FDCA2F1F649D4600E82AAE /* Resources */, 109 | ); 110 | buildRules = ( 111 | ); 112 | dependencies = ( 113 | ); 114 | name = Communicado; 115 | productName = Communicado; 116 | productReference = B7FDCA311F649D4600E82AAE /* Communicado.app */; 117 | productType = "com.apple.product-type.application"; 118 | }; 119 | /* End PBXNativeTarget section */ 120 | 121 | /* Begin PBXProject section */ 122 | B7FDCA291F649D4600E82AAE /* Project object */ = { 123 | isa = PBXProject; 124 | attributes = { 125 | LastSwiftUpdateCheck = 0900; 126 | LastUpgradeCheck = 1010; 127 | ORGANIZATIONNAME = Mergesort; 128 | TargetAttributes = { 129 | B7FDCA301F649D4600E82AAE = { 130 | CreatedOnToolsVersion = 9.0; 131 | LastSwiftMigration = 1120; 132 | ProvisioningStyle = Automatic; 133 | }; 134 | }; 135 | }; 136 | buildConfigurationList = B7FDCA2C1F649D4600E82AAE /* Build configuration list for PBXProject "Communicado" */; 137 | compatibilityVersion = "Xcode 8.0"; 138 | developmentRegion = en; 139 | hasScannedForEncodings = 0; 140 | knownRegions = ( 141 | en, 142 | Base, 143 | ); 144 | mainGroup = B7FDCA281F649D4600E82AAE; 145 | productRefGroup = B7FDCA321F649D4600E82AAE /* Products */; 146 | projectDirPath = ""; 147 | projectRoot = ""; 148 | targets = ( 149 | B7FDCA301F649D4600E82AAE /* Communicado */, 150 | ); 151 | }; 152 | /* End PBXProject section */ 153 | 154 | /* Begin PBXResourcesBuildPhase section */ 155 | B7FDCA2F1F649D4600E82AAE /* Resources */ = { 156 | isa = PBXResourcesBuildPhase; 157 | buildActionMask = 2147483647; 158 | files = ( 159 | B7FDCA3F1F649D4600E82AAE /* LaunchScreen.storyboard in Resources */, 160 | B7FDCA3C1F649D4600E82AAE /* Assets.xcassets in Resources */, 161 | B7FDCA3A1F649D4600E82AAE /* Main.storyboard in Resources */, 162 | ); 163 | runOnlyForDeploymentPostprocessing = 0; 164 | }; 165 | /* End PBXResourcesBuildPhase section */ 166 | 167 | /* Begin PBXSourcesBuildPhase section */ 168 | B7FDCA2D1F649D4600E82AAE /* Sources */ = { 169 | isa = PBXSourcesBuildPhase; 170 | buildActionMask = 2147483647; 171 | files = ( 172 | B76E5F602380E47000736662 /* Attachment.swift in Sources */, 173 | B76E5F622380E47000736662 /* ShareParameters.swift in Sources */, 174 | B76E5F5F2380E47000736662 /* AttachmentType.swift in Sources */, 175 | B76E5F632380E47000736662 /* ShareDestination.swift in Sources */, 176 | B7FDCA371F649D4600E82AAE /* ViewController.swift in Sources */, 177 | B76E5F612380E47000736662 /* UIViewController+Sharing.swift in Sources */, 178 | B7FDCA351F649D4600E82AAE /* AppDelegate.swift in Sources */, 179 | ); 180 | runOnlyForDeploymentPostprocessing = 0; 181 | }; 182 | /* End PBXSourcesBuildPhase section */ 183 | 184 | /* Begin PBXVariantGroup section */ 185 | B7FDCA381F649D4600E82AAE /* Main.storyboard */ = { 186 | isa = PBXVariantGroup; 187 | children = ( 188 | B7FDCA391F649D4600E82AAE /* Base */, 189 | ); 190 | name = Main.storyboard; 191 | sourceTree = ""; 192 | }; 193 | B7FDCA3D1F649D4600E82AAE /* LaunchScreen.storyboard */ = { 194 | isa = PBXVariantGroup; 195 | children = ( 196 | B7FDCA3E1F649D4600E82AAE /* Base */, 197 | ); 198 | name = LaunchScreen.storyboard; 199 | sourceTree = ""; 200 | }; 201 | /* End PBXVariantGroup section */ 202 | 203 | /* Begin XCBuildConfiguration section */ 204 | B7FDCA411F649D4600E82AAE /* Debug */ = { 205 | isa = XCBuildConfiguration; 206 | buildSettings = { 207 | ALWAYS_SEARCH_USER_PATHS = NO; 208 | CLANG_ANALYZER_NONNULL = YES; 209 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 210 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 211 | CLANG_CXX_LIBRARY = "libc++"; 212 | CLANG_ENABLE_MODULES = YES; 213 | CLANG_ENABLE_OBJC_ARC = YES; 214 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 215 | CLANG_WARN_BOOL_CONVERSION = YES; 216 | CLANG_WARN_COMMA = YES; 217 | CLANG_WARN_CONSTANT_CONVERSION = YES; 218 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 219 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 220 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 221 | CLANG_WARN_EMPTY_BODY = YES; 222 | CLANG_WARN_ENUM_CONVERSION = YES; 223 | CLANG_WARN_INFINITE_RECURSION = YES; 224 | CLANG_WARN_INT_CONVERSION = YES; 225 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 226 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 227 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 228 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 229 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 230 | CLANG_WARN_STRICT_PROTOTYPES = YES; 231 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 232 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 233 | CLANG_WARN_UNREACHABLE_CODE = YES; 234 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 235 | CODE_SIGN_IDENTITY = "iPhone Developer"; 236 | COPY_PHASE_STRIP = NO; 237 | DEBUG_INFORMATION_FORMAT = dwarf; 238 | ENABLE_STRICT_OBJC_MSGSEND = YES; 239 | ENABLE_TESTABILITY = YES; 240 | GCC_C_LANGUAGE_STANDARD = gnu11; 241 | GCC_DYNAMIC_NO_PIC = NO; 242 | GCC_NO_COMMON_BLOCKS = YES; 243 | GCC_OPTIMIZATION_LEVEL = 0; 244 | GCC_PREPROCESSOR_DEFINITIONS = ( 245 | "DEBUG=1", 246 | "$(inherited)", 247 | ); 248 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 249 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 250 | GCC_WARN_UNDECLARED_SELECTOR = YES; 251 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 252 | GCC_WARN_UNUSED_FUNCTION = YES; 253 | GCC_WARN_UNUSED_VARIABLE = YES; 254 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 255 | MTL_ENABLE_DEBUG_INFO = YES; 256 | ONLY_ACTIVE_ARCH = YES; 257 | SDKROOT = iphoneos; 258 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 259 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 260 | }; 261 | name = Debug; 262 | }; 263 | B7FDCA421F649D4600E82AAE /* Release */ = { 264 | isa = XCBuildConfiguration; 265 | buildSettings = { 266 | ALWAYS_SEARCH_USER_PATHS = NO; 267 | CLANG_ANALYZER_NONNULL = YES; 268 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 269 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 270 | CLANG_CXX_LIBRARY = "libc++"; 271 | CLANG_ENABLE_MODULES = YES; 272 | CLANG_ENABLE_OBJC_ARC = YES; 273 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 274 | CLANG_WARN_BOOL_CONVERSION = YES; 275 | CLANG_WARN_COMMA = YES; 276 | CLANG_WARN_CONSTANT_CONVERSION = YES; 277 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 278 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 279 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 280 | CLANG_WARN_EMPTY_BODY = YES; 281 | CLANG_WARN_ENUM_CONVERSION = YES; 282 | CLANG_WARN_INFINITE_RECURSION = YES; 283 | CLANG_WARN_INT_CONVERSION = YES; 284 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 285 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 286 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 287 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 288 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 289 | CLANG_WARN_STRICT_PROTOTYPES = YES; 290 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 291 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 292 | CLANG_WARN_UNREACHABLE_CODE = YES; 293 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 294 | CODE_SIGN_IDENTITY = "iPhone Developer"; 295 | COPY_PHASE_STRIP = NO; 296 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 297 | ENABLE_NS_ASSERTIONS = NO; 298 | ENABLE_STRICT_OBJC_MSGSEND = YES; 299 | GCC_C_LANGUAGE_STANDARD = gnu11; 300 | GCC_NO_COMMON_BLOCKS = YES; 301 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 302 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 303 | GCC_WARN_UNDECLARED_SELECTOR = YES; 304 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 305 | GCC_WARN_UNUSED_FUNCTION = YES; 306 | GCC_WARN_UNUSED_VARIABLE = YES; 307 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 308 | MTL_ENABLE_DEBUG_INFO = NO; 309 | SDKROOT = iphoneos; 310 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 311 | VALIDATE_PRODUCT = YES; 312 | }; 313 | name = Release; 314 | }; 315 | B7FDCA441F649D4600E82AAE /* Debug */ = { 316 | isa = XCBuildConfiguration; 317 | buildSettings = { 318 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 319 | CODE_SIGN_STYLE = Automatic; 320 | DEVELOPMENT_TEAM = ""; 321 | INFOPLIST_FILE = Communicado/Info.plist; 322 | IPHONEOS_DEPLOYMENT_TARGET = 10.3; 323 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 324 | PRODUCT_BUNDLE_IDENTIFIER = com.Mergesort.Communicado; 325 | PRODUCT_NAME = "$(TARGET_NAME)"; 326 | SWIFT_VERSION = 5.0; 327 | TARGETED_DEVICE_FAMILY = "1,2"; 328 | }; 329 | name = Debug; 330 | }; 331 | B7FDCA451F649D4600E82AAE /* Release */ = { 332 | isa = XCBuildConfiguration; 333 | buildSettings = { 334 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 335 | CODE_SIGN_STYLE = Automatic; 336 | DEVELOPMENT_TEAM = ""; 337 | INFOPLIST_FILE = Communicado/Info.plist; 338 | IPHONEOS_DEPLOYMENT_TARGET = 10.3; 339 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 340 | PRODUCT_BUNDLE_IDENTIFIER = com.Mergesort.Communicado; 341 | PRODUCT_NAME = "$(TARGET_NAME)"; 342 | SWIFT_VERSION = 5.0; 343 | TARGETED_DEVICE_FAMILY = "1,2"; 344 | }; 345 | name = Release; 346 | }; 347 | /* End XCBuildConfiguration section */ 348 | 349 | /* Begin XCConfigurationList section */ 350 | B7FDCA2C1F649D4600E82AAE /* Build configuration list for PBXProject "Communicado" */ = { 351 | isa = XCConfigurationList; 352 | buildConfigurations = ( 353 | B7FDCA411F649D4600E82AAE /* Debug */, 354 | B7FDCA421F649D4600E82AAE /* Release */, 355 | ); 356 | defaultConfigurationIsVisible = 0; 357 | defaultConfigurationName = Release; 358 | }; 359 | B7FDCA431F649D4600E82AAE /* Build configuration list for PBXNativeTarget "Communicado" */ = { 360 | isa = XCConfigurationList; 361 | buildConfigurations = ( 362 | B7FDCA441F649D4600E82AAE /* Debug */, 363 | B7FDCA451F649D4600E82AAE /* Release */, 364 | ); 365 | defaultConfigurationIsVisible = 0; 366 | defaultConfigurationName = Release; 367 | }; 368 | /* End XCConfigurationList section */ 369 | }; 370 | rootObject = B7FDCA291F649D4600E82AAE /* Project object */; 371 | } 372 | -------------------------------------------------------------------------------- /Example/Communicado.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Example/Communicado.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Example/Communicado/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // Communicado 4 | // 5 | // Created by Joe Fabisevich on 9/9/17. 6 | // Copyright © 2017 Mergesort. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(_ application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(_ application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(_ application: UIApplication) { 33 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(_ application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(_ application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Example/Communicado/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | } 88 | ], 89 | "info" : { 90 | "version" : 1, 91 | "author" : "xcode" 92 | } 93 | } -------------------------------------------------------------------------------- /Example/Communicado/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Example/Communicado/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 34 | 44 | 54 | 64 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /Example/Communicado/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Example/Communicado/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // Communicado 4 | // 5 | // Created by Joe Fabisevich on 9/9/17. 6 | // Copyright © 2017 Mergesort. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | final class ViewController: UIViewController, SharingCapableViewController { 12 | 13 | override func viewDidLoad() { 14 | super.viewDidLoad() 15 | 16 | self.setup() 17 | } 18 | 19 | } 20 | 21 | private extension ViewController { 22 | 23 | func setup() { 24 | self.title = "Sharing Example" 25 | 26 | self.sharingTitleTextAttributes = [ 27 | .foregroundColor : #colorLiteral(red: 0, green: 0.2156862745, blue: 0.5019607843, alpha: 1), 28 | .font : UIFont.systemFont(ofSize: 21.0) 29 | ] 30 | 31 | self.sharingBarButtonItemAttributes = [ 32 | .foregroundColor : #colorLiteral(red: 0.9254901961, green: 0.9411764706, blue: 0.9450980392, alpha: 1), 33 | .font : UIFont.systemFont(ofSize: 16.0) 34 | ] 35 | 36 | self.sharingBackgroundColor = #colorLiteral(red: 0.09803921569, green: 0.7098039216, blue: 0.9960784314, alpha: 1) 37 | 38 | self.sharingCompleted = { shareResult in 39 | print("Was successful: \(shareResult.success)") 40 | print("Sharing service: \(shareResult.sharingService)") 41 | } 42 | } 43 | 44 | @IBAction func messagesButtonTapped() { 45 | self.shareViaMessages() 46 | } 47 | 48 | @IBAction func mailButtonTapped() { 49 | self.shareViaMail() 50 | } 51 | 52 | @IBAction func twitterButtonTapped() { 53 | self.shareViaTwitter() 54 | } 55 | 56 | @IBAction func pasteboardButtonTapped() { 57 | self.shareViaPasteboard() 58 | } 59 | 60 | @IBAction func activityControllerButtonTapped() { 61 | self.shareViaActivityViewController() 62 | } 63 | 64 | func shareViaMessages() { 65 | let messageShareParameters = MessagesShareParameters(message: "I ❤️ Communicado", attachments: nil) 66 | self.share(messageShareParameters) 67 | } 68 | 69 | func shareViaMail() { 70 | let attachments: [Attachment]? 71 | 72 | let blankImage = UIImage() 73 | if let imageData = blankImage.pngData() { 74 | attachments = [Attachment(attachmentType: .png, filename: "blankImage.png", data: imageData)] 75 | } else { 76 | attachments = nil 77 | } 78 | 79 | let mailShareParameters = MailShareParameters(subject: "I ❤️ Communicado", message: "I ❤️ Communicado.", isHTML: true, toRecepients: ["github@fabisevi.ch"], ccRecepients: nil, bccRecepients: nil, attachments: attachments) 80 | self.share(mailShareParameters) 81 | } 82 | 83 | func shareViaActivityViewController() { 84 | // All the parameters are optional except for the activity. 85 | // You can call a simplified version like this if you'd prefer. 86 | // let activityShareParameters = ActivityShareParameters(activityItems: ["I ❤️ Communicado"]]) 87 | 88 | let activityShareParameters = ActivityShareParameters(activityItems: ["I ❤️ Communicado"], excludedActivityTypes: [.airDrop, .print], applicationActivites: nil, completionItemsHandler: nil, sourceView: nil) 89 | self.share(activityShareParameters) 90 | } 91 | 92 | func shareViaPasteboard() { 93 | let pasteBoardShareParmeters = PasteboardShareParameters(value: PasteboardShareParameters.Value.string("I ❤️ Communicado")) 94 | self.share(pasteBoardShareParmeters) 95 | } 96 | 97 | func shareViaTwitter() { 98 | // You can share to any of the built in Social networks (Twitter, Facebook, Sina Weibo, and Tencent Weibo). 99 | // This functionality has been deprecated in iOS 11 100 | // as Apple removed the Social framework which this was based on. 101 | 102 | if #available(iOS 11.0, *) { 103 | let alertController = UIAlertController(title: "This functionality has been deprecated in iOS 11", message: nil, preferredStyle: .alert) 104 | alertController.addAction(UIAlertAction(title: "Okay", style: .default, handler: nil)) 105 | self.present(alertController, animated: true, completion: nil) 106 | } else { 107 | let twitterShareDestination = TwitterShareDestination() 108 | let twitterShareParameters = SocialShareParameters(network: twitterShareDestination, message: "I ❤️ Communicado", images: nil, urls: nil) 109 | self.share(twitterShareParameters) 110 | } 111 | } 112 | 113 | 114 | } 115 | -------------------------------------------------------------------------------- /Images/cartman.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mergesort/Communicado/dc10510f2005bd8e4f1107813046a57060f326a5/Images/cartman.gif -------------------------------------------------------------------------------- /Images/homer.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mergesort/Communicado/dc10510f2005bd8e4f1107813046a57060f326a5/Images/homer.gif -------------------------------------------------------------------------------- /Images/styled_messages.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mergesort/Communicado/dc10510f2005bd8e4f1107813046a57060f326a5/Images/styled_messages.png -------------------------------------------------------------------------------- /Images/yay.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mergesort/Communicado/dc10510f2005bd8e4f1107813046a57060f326a5/Images/yay.gif -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Joe Fabisevich 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.0 2 | // The swift-tools-version declares the minimum version of Swift required to build this package. 3 | import PackageDescription 4 | 5 | let package = Package( 6 | name: "Communicado", 7 | platforms: [ 8 | .iOS(.v9) 9 | ], 10 | products: [ 11 | .library( 12 | name: "Communicado", 13 | targets: ["Communicado"]), 14 | ], 15 | dependencies: [], 16 | targets: [ 17 | .target( 18 | name: "Communicado", 19 | dependencies: []) 20 | ] 21 | ) 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Communicado 2 | 3 | ### Sharing on iOS made easy. 4 | 5 | [![BuddyBuild](https://dashboard.buddybuild.com/api/statusImage?appID=59b695f696d4600001f5144a&branch=master&build=latest)](https://dashboard.buddybuild.com/apps/59b695f696d4600001f5144a/build/latest?branch=master) 6 | [![Pod Version](https://img.shields.io/badge/Pod-3.2.0-6193DF.svg)](https://cocoapods.org/) 7 | ![Swift Version](https://img.shields.io/badge/Swift-5.1-brightgreen.svg) 8 | ![License MIT](https://img.shields.io/badge/License-MIT-lightgrey.svg) 9 | ![Plaform](https://img.shields.io/badge/Platform-iOS-lightgrey.svg) 10 | 11 | --- 12 | 13 | #### Are you tired of rewriting the same sharing code over and over again? 14 | 15 | ![](Images/cartman.gif) 16 | #### Me too! 17 | 18 | ![](Images/homer.gif) 19 | #### That's why I wrote Communicado! Let's show you how it's done. 20 | 21 | Sharing 22 | --- 23 | 24 | The first thing to know is can share from any `UIViewController` that conforms to `SharingCapableViewController`. Once you add this, you will get a `share` function on `UIViewController` which supports many built in sharing types. 25 | 26 | You can share to: 27 | 28 | - Messages 29 | - Mail 30 | - Pasteboard 31 | - Photos 32 | - UIActivityController 33 | - Twitter (iOS 10.3 and lower) 34 | - Facebook (iOS 10.3 and lower) 35 | - Sina Weibo (iOS 10.3 and lower) 36 | - Tencent Weibo (iOS 10.3 and lower) 37 | 38 | Each sharing destination takes in parameters. Let's try a simple example. 39 | 40 | ```swift 41 | let heartImageData = UIImagePNGRepresentation(myHeartImage) 42 | let attachment = Attachment(attachmentType: AttachmentType.png, filename: "heart.png", data: heartImageData) 43 | let messageParameters = MessageShareParameters(message: "I ❤️ Communicado", attachments: [ attachment ]) 44 | ``` 45 | 46 | Now let's call the **ONLY** method that's even available to you. 47 | 48 | ```swift 49 | self.share(messageParameters) 50 | ``` 51 | 52 | And when you're done, you'll get one unified callback with the information of how the share attempt went. 53 | 54 | ```swift 55 | self.sharingCompleted = { shareResult in 56 | print("Was successful? \(shareResult.success)") 57 | print("Sharing service: \(shareResult.sharingService)") 58 | } 59 | ``` 60 | 61 | You can do the same for the other sharing destinations as well. 62 | 63 | ```swift 64 | self.share(MailShareParameters) 65 | self.share(SocialShareParameters) 66 | self.share(ActivityShareParameters) 67 | self.share(PhotosShareParameters) 68 | self.share(PasteboardShareParameters) 69 | ``` 70 | 71 | Styling 72 | --- 73 | 74 | You can also use Communicado to style the `MFMailComposeViewController` and `MFMessageComposeViewController` with just a few lines of code. This works around all of the hoops Apple makes you jump through to style the built in sharing controllers. 75 | 76 | All you have to do is: 77 | 78 | ```swift 79 | self.sharingTitleTextAttributes = [ 80 | NSAttributedString.Key.foregroundColor : UIColor.white, 81 | NSAttributedString.Key.font : UIFont.systemFont(ofSize: 21.0) 82 | ] 83 | 84 | self.sharingBarButtonItemAttributes = [ 85 | NSAttributedString.Key.foregroundColor : UIColor.purple, 86 | NSAttributedString.Key.font : UIFont.systemFont(ofSize: 16.0) 87 | ] 88 | 89 | self.sharingBackgroundColor = UIColor.blue 90 | ``` 91 | 92 | And get something that looks like this: 93 | 94 | ![](Images/styled_messages.png) 95 | 96 | Beautiful, isn't it? 97 | 98 | ![](Images/yay.gif) 99 | 100 | ## Requirements 101 | 102 | - iOS 9.0+ 103 | - Xcode 8.0+ 104 | 105 | ## Installation 106 | For **Swift 3** support, use version **2.0.2**.
107 | For **Swift 4** support, you can use version **3.0**.
108 | For **Swift 4.2** support, you can use version **3.1** or above.
109 | For **Swift 5.1** support, you can use version **3.2.0** or above. 110 | 111 | You can use [SPM](https://swift.org/package-manager/) to install `Communicado`. 112 | 113 | You can also use [CocoaPods](http://cocoapods.org/) to install `Communicado` by adding it to your `Podfile`: 114 | 115 | ```ruby 116 | platform :ios, '9.0' 117 | use_frameworks! 118 | 119 | pod 'Communicado' 120 | ``` 121 | 122 | Or install it manually by downloading all the files in the `Source` folder and dropping them into your project. 123 | 124 | ## About me 125 | 126 | Hi, I'm [Joe](http://fabisevi.ch) everywhere on the web, but especially on [Twitter](https://twitter.com/mergesort). 127 | 128 | ## License 129 | 130 | See the [license](LICENSE) for more information about how you can use Communicado. I promise it's not GPL, because I am not "that guy". 131 | 132 | ## The end? 133 | 134 | Yes, this is the end. Hopefully Communicado makes your life easier. It probably won't help you pay your rent, but it might make it easier to share in your app. 135 | -------------------------------------------------------------------------------- /Sources/Communicado/Attachment.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | /// An Attachment which can be used 4 | public struct Attachment { 5 | 6 | /// The attachment type for this attachment, passed as a MIME type. 7 | public let attachmentType: AttachmentType 8 | 9 | /// The filename as it will be transmitted in the attachment. 10 | public let filename: String 11 | 12 | /// The data which will be transmitted in the attachment. 13 | public let data: Data 14 | 15 | public init(attachmentType: AttachmentType, filename: String, data: Data) { 16 | self.attachmentType = attachmentType 17 | self.filename = filename 18 | self.data = data 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Sources/Communicado/AttachmentType.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | /// Commonly supported attachment types 4 | public enum AttachmentType { 5 | 6 | case aiff 7 | case avi 8 | case gif 9 | case html 10 | case jpg 11 | case mov 12 | case mp3 13 | case mp4 14 | case pdf 15 | case plainText 16 | case png 17 | case psd 18 | case rtf 19 | case tiff 20 | case zip 21 | 22 | /// Any identifier which is not one of the common ones encapsulated in this `AttachmentType`s. 23 | case custom(value: String) 24 | 25 | var identifier: String { 26 | switch self { 27 | 28 | case .aiff: 29 | return "audio/aiff" 30 | 31 | case .avi: 32 | return "video/avi" 33 | 34 | case .gif: 35 | return "image/gif" 36 | 37 | case .html: 38 | return "text/html" 39 | 40 | case .jpg: 41 | return "image/jpeg" 42 | 43 | case .mov: 44 | return "video/quicktime" 45 | 46 | case .mp3: 47 | return "audio/mp3" 48 | 49 | case .mp4: 50 | return "video/mp4" 51 | 52 | case .pdf: 53 | return "application/pdf" 54 | 55 | case .plainText: 56 | return "text/plain" 57 | 58 | case .png: 59 | return "image/png" 60 | 61 | case .psd: 62 | return "image/psd" 63 | 64 | case .rtf: 65 | return "text/rtf" 66 | 67 | case .tiff: 68 | return "image/tiff" 69 | 70 | case .zip: 71 | return "application/zip" 72 | 73 | case .custom(let value): 74 | return value 75 | 76 | } 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /Sources/Communicado/ShareDestination.swift: -------------------------------------------------------------------------------- 1 | import MessageUI 2 | import Photos 3 | import Social 4 | import UIKit 5 | 6 | /// A type that defines the values needed for a destination to share to. 7 | public protocol ShareDestination { 8 | 9 | /// The name of the destination we're sharing to. 10 | static var name: String { get } 11 | 12 | /// A computed var telling us whether or not we are currently capable of sharing to this destination. 13 | var canShare: Bool { get } 14 | 15 | /// A `UIActivityType` of the destination that we are sharing to. 16 | var activityType: UIActivity.ActivityType { get } 17 | } 18 | 19 | /// A type that defines the values needed for a social network backed destination to share to. 20 | public protocol SocialShareDestination: ShareDestination { 21 | 22 | /// The name of the destination we're sharing to. 23 | var name: String { get } 24 | } 25 | 26 | /// A ShareDestination for sharing to Messages. 27 | public struct MessagesShareDestination: ShareDestination { 28 | 29 | public static let name = UIActivity.ActivityType.message.rawValue 30 | 31 | public var canShare: Bool { 32 | return MFMessageComposeViewController.canSendText() 33 | } 34 | 35 | public var activityType: UIActivity.ActivityType { 36 | return UIActivity.ActivityType(MessagesShareDestination.name) 37 | } 38 | 39 | public init() {} 40 | } 41 | 42 | /// A ShareDestination for sharing to Mail. 43 | public struct MailShareDestination: ShareDestination { 44 | 45 | public static let name = UIActivity.ActivityType.mail.rawValue 46 | 47 | public var canShare: Bool { 48 | return MFMailComposeViewController.canSendMail() 49 | } 50 | 51 | public var activityType: UIActivity.ActivityType { 52 | return UIActivity.ActivityType(MailShareDestination.name) 53 | } 54 | 55 | public init() {} 56 | } 57 | 58 | /// A ShareDestination for sharing to the camera roll. 59 | public struct PhotosShareDestination: ShareDestination { 60 | 61 | public static let name = UIActivity.ActivityType.saveToCameraRoll.rawValue 62 | 63 | public var canShare: Bool { 64 | return PHPhotoLibrary.authorizationStatus() == .authorized 65 | } 66 | 67 | public var activityType: UIActivity.ActivityType { 68 | return UIActivity.ActivityType(PhotosShareDestination.name) 69 | } 70 | 71 | public init() {} 72 | } 73 | 74 | /// A ShareDestination for sharing to the pasteboard. 75 | public struct PasteboardShareDestination: ShareDestination { 76 | 77 | public static let name = UIActivity.ActivityType.copyToPasteboard.rawValue 78 | 79 | public var canShare: Bool { 80 | return true 81 | } 82 | 83 | public var activityType: UIActivity.ActivityType { 84 | return UIActivity.ActivityType(PasteboardShareDestination.name) 85 | } 86 | 87 | public init() {} 88 | } 89 | 90 | /// A ShareDestination for sharing to the `UIActivityViewController`. 91 | public struct ActivityControllerShareDestination: ShareDestination { 92 | 93 | public static let name = "com.apple.activityController" 94 | 95 | public var canShare: Bool { 96 | return true 97 | } 98 | 99 | public var activityType: UIActivity.ActivityType { 100 | return UIActivity.ActivityType(ActivityControllerShareDestination.name) 101 | } 102 | 103 | public init() {} 104 | } 105 | 106 | /// A ShareDestination for sharing to Twitter. 107 | /// Deprecated in iOS 11, as Apple removed the Social framework, which this was based on. 108 | @available(iOS, deprecated: 11.0) 109 | public struct TwitterShareDestination: SocialShareDestination { 110 | 111 | public static let name = SLServiceTypeTwitter 112 | public let name = SLServiceTypeTwitter 113 | 114 | public var canShare: Bool { 115 | return SLComposeViewController.isAvailable(forServiceType: SLServiceTypeTwitter) 116 | } 117 | 118 | public var activityType: UIActivity.ActivityType { 119 | return UIActivity.ActivityType(TwitterShareDestination.name) 120 | } 121 | 122 | public init() {} 123 | } 124 | 125 | /// A ShareDestination for sharing to Facebook. 126 | /// Deprecated in iOS 11, as Apple removed the Social framework, which this was based on. 127 | @available(iOS, deprecated: 11.0) 128 | public struct FacebookShareDestination: SocialShareDestination { 129 | 130 | public static let name = SLServiceTypeFacebook 131 | public let name = SLServiceTypeFacebook 132 | 133 | public var canShare: Bool { 134 | return SLComposeViewController.isAvailable(forServiceType: SLServiceTypeFacebook) 135 | } 136 | 137 | public var activityType: UIActivity.ActivityType { 138 | return UIActivity.ActivityType(FacebookShareDestination.name) 139 | } 140 | 141 | public init() {} 142 | } 143 | 144 | /// A ShareDestination for sharing to Tencent Weibo. 145 | /// Deprecated in iOS 11, as Apple removed the Social framework, which this was based on. 146 | @available(iOS, deprecated: 11.0) 147 | public struct TencentWeiboShareDestination: SocialShareDestination { 148 | 149 | public static let name = SLServiceTypeTencentWeibo 150 | public let name = SLServiceTypeTencentWeibo 151 | 152 | public var canShare: Bool { 153 | return SLComposeViewController.isAvailable(forServiceType: SLServiceTypeTencentWeibo) 154 | } 155 | 156 | public var activityType: UIActivity.ActivityType { 157 | return UIActivity.ActivityType(TencentWeiboShareDestination.name) 158 | } 159 | 160 | public init() {} 161 | } 162 | 163 | /// A ShareDestination for sharing to Sina Weibo. 164 | /// Deprecated in iOS 11, as Apple removed the Social framework, which this was based on. 165 | @available(iOS, deprecated: 11.0) 166 | public struct SinaWeiboShareDestination: SocialShareDestination { 167 | 168 | public static let name = SLServiceTypeSinaWeibo 169 | public let name = SLServiceTypeSinaWeibo 170 | 171 | public var canShare: Bool { 172 | return SLComposeViewController.isAvailable(forServiceType: SLServiceTypeSinaWeibo) 173 | } 174 | 175 | public var activityType: UIActivity.ActivityType { 176 | return UIActivity.ActivityType(SinaWeiboShareDestination.name) 177 | } 178 | 179 | public init() {} 180 | } 181 | 182 | public extension UIActivity.ActivityType { 183 | 184 | /// A `UIActivityType` which indicates that a share activity was cancelled by the user. 185 | static let cancelled = UIActivity.ActivityType("com.plugin.cancelled") 186 | 187 | } 188 | -------------------------------------------------------------------------------- /Sources/Communicado/ShareParameters.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | /// A protocol for types which share values to a `ShareDestination`. 4 | protocol ShareParameters { 5 | var shareDestination: ShareDestination { get } 6 | } 7 | 8 | /// Parameters which are used when sharing via the built in `UIActivityViewController`. 9 | public struct ActivityShareParameters: ShareParameters { 10 | 11 | let shareDestination: ShareDestination = ActivityControllerShareDestination() 12 | 13 | public let activityItems: [Any] 14 | public let excludedActivityTypes: [UIActivity.ActivityType]? 15 | public let applicationActivites: [UIActivity]? 16 | public let completionItemsHandler: UIActivityViewController.CompletionWithItemsHandler? 17 | public let sourceView: UIView? 18 | 19 | public init(activityItems: [Any], excludedActivityTypes: [UIActivity.ActivityType]? = nil, applicationActivites: [UIActivity]? = nil, completionItemsHandler: UIActivityViewController.CompletionWithItemsHandler? = nil, sourceView: UIView? = nil) { 20 | self.activityItems = activityItems 21 | self.excludedActivityTypes = excludedActivityTypes 22 | self.applicationActivites = applicationActivites 23 | self.completionItemsHandler = completionItemsHandler 24 | self.sourceView = sourceView 25 | } 26 | 27 | } 28 | 29 | /// Parameters which are used when sharing via the built in `MFMessageComposeViewController`. 30 | public struct MessagesShareParameters: ShareParameters { 31 | 32 | let shareDestination: ShareDestination = MessagesShareDestination() 33 | 34 | public let message: String? 35 | public let attachments: [Attachment]? 36 | 37 | public init(message: String? = nil, attachments: [Attachment]? = nil) { 38 | self.message = message 39 | self.attachments = attachments 40 | } 41 | 42 | } 43 | 44 | /// Parameters which are used when sharing via the built in `MFMailComposeViewController`. 45 | public struct MailShareParameters: ShareParameters { 46 | 47 | let shareDestination: ShareDestination = MailShareDestination() 48 | 49 | public let subject: String? 50 | public let message: String? 51 | public let isHTML: Bool 52 | public let toRecepients: [String]? 53 | public let ccRecepients: [String]? 54 | public let bccRecepients: [String]? 55 | public let attachments: [Attachment]? 56 | 57 | public init(subject: String? = nil, message: String? = nil, isHTML: Bool = false, toRecepients: [String]? = nil, ccRecepients: [String]? = nil, bccRecepients: [String]? = nil, attachments: [Attachment]? = nil) { 58 | self.subject = subject 59 | self.message = message 60 | self.isHTML = isHTML 61 | self.toRecepients = toRecepients 62 | self.ccRecepients = ccRecepients 63 | self.bccRecepients = bccRecepients 64 | self.attachments = attachments 65 | } 66 | 67 | } 68 | 69 | /// Parameters which are used when saving a `PasteboardShareParameters.Value` to the user's pasteboard. 70 | public struct PasteboardShareParameters: ShareParameters { 71 | 72 | public enum Value { 73 | case string(String?) 74 | case image(UIImage?) 75 | case url(URL?) 76 | } 77 | 78 | let shareDestination: ShareDestination = PasteboardShareDestination() 79 | 80 | public let string: String? 81 | public let image: UIImage? 82 | public let url: URL? 83 | 84 | public init(value: PasteboardShareParameters.Value) { 85 | switch value { 86 | 87 | case .string(let string): 88 | self.string = string 89 | self.image = nil 90 | self.url = nil 91 | 92 | case .image(let image): 93 | self.image = image 94 | self.string = nil 95 | self.url = nil 96 | 97 | case .url(let url): 98 | self.url = url 99 | self.string = nil 100 | self.image = nil 101 | } 102 | } 103 | 104 | } 105 | 106 | /// Parameters which are used when saving an image to the user's camera roll. 107 | public struct PhotosShareParameters: ShareParameters { 108 | 109 | let shareDestination: ShareDestination = PhotosShareDestination() 110 | 111 | public let image: UIImage 112 | 113 | public init(image: UIImage) { 114 | self.image = image 115 | } 116 | 117 | } 118 | 119 | /// Parameters which are used when posting to a social network. 120 | /// Deprecated in iOS 11, as Apple removed the Social framework, which this was based on. 121 | @available(iOS, deprecated: 11.0) 122 | public struct SocialShareParameters { 123 | 124 | public let network: SocialShareDestination 125 | public let message: String? 126 | public let images: [UIImage]? 127 | public let urls: [URL]? 128 | 129 | public init(network: SocialShareDestination, message: String? = nil, images: [UIImage]? = nil, urls: [URL]? = nil) { 130 | self.network = network 131 | self.message = message 132 | self.images = images 133 | self.urls = urls 134 | } 135 | 136 | } 137 | -------------------------------------------------------------------------------- /Sources/Communicado/UIViewController+Sharing.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | import MessageUI 4 | import Photos 5 | import Social 6 | import ObjectiveC.runtime 7 | 8 | /// A value returned for when sharing events occur. 9 | /// - success: Whether or not the share was successful or failed. 10 | /// - sharingService: A `UIActivityType` for which specific service was attempting to be shared. 11 | public typealias ShareResult = (success: Bool, sharingService: UIActivity.ActivityType) 12 | 13 | /// A unified completion handler after a share event occurs. 14 | public typealias SharingCompletedEvent = (ShareResult) -> Void 15 | 16 | /// A protocol for defining where the share functionality for `UIViewController`s exists. 17 | public protocol SharingCapableViewController: UIViewController {} 18 | 19 | public extension SharingCapableViewController { 20 | 21 | /// Share using UIActivityViewController. 22 | /// 23 | /// - Parameter parameters: Parameters that are applicable for sharing when using UIActivityViewController. 24 | func share(_ parameters: ActivityShareParameters) { 25 | self.shareIfPossible(destination: parameters.shareDestination) { 26 | let activityController = UIActivityViewController(activityItems: parameters.activityItems, applicationActivities: parameters.applicationActivites) 27 | activityController.excludedActivityTypes = parameters.excludedActivityTypes 28 | 29 | activityController.completionWithItemsHandler = { activityType, completed, returnedItems, activityError in 30 | parameters.completionItemsHandler?(activityType, completed, returnedItems, activityError) 31 | 32 | let sharingService = activityType ?? UIActivity.ActivityType.cancelled 33 | self.sharingCompleted?((success: (completed && activityError == nil), sharingService: sharingService)) 34 | } 35 | 36 | if UIDevice.current.userInterfaceIdiom == .pad { 37 | activityController.modalPresentationStyle = .popover 38 | self.present(activityController, animated: true, completion: nil) 39 | if let controller = activityController.popoverPresentationController { 40 | controller.permittedArrowDirections = .any 41 | controller.sourceView = parameters.sourceView 42 | } 43 | } else { 44 | self.present(activityController, animated: true, completion: nil) 45 | } 46 | } 47 | } 48 | 49 | /// Share to the Messages app. 50 | /// 51 | /// - Parameter parameters: Parameters that are applicable for sharing to Messages. 52 | func share(_ parameters: MessagesShareParameters) { 53 | self.shareIfPossible(destination: parameters.shareDestination) { 54 | self.temporarySharingBarButtonItemAttributes = UIBarButtonItem.appearance().titleTextAttributes(for: .normal) 55 | UIBarButtonItem.appearance().setTitleTextAttributes(self.sharingBarButtonItemAttributes, for: .normal) 56 | 57 | if let backgroundColor = self.sharingBackgroundColor { 58 | self.temporarySharingBackgroundImage = UINavigationBar.appearance().backgroundImage(for: UIBarMetrics.default) 59 | UINavigationBar.appearance().setBackgroundImage(UIImage(color: backgroundColor), for: UIBarMetrics.default) 60 | } 61 | 62 | let messageController = MFMessageComposeViewController() 63 | messageController.messageComposeDelegate = self 64 | messageController.body = parameters.message 65 | parameters.attachments?.forEach { attachment in 66 | messageController.addAttachmentData(attachment.data, typeIdentifier: attachment.attachmentType.identifier, filename: attachment.filename) 67 | } 68 | 69 | self.present(messageController, animated: true, completion: nil) 70 | } 71 | } 72 | 73 | /// Share to the Mail app. 74 | /// 75 | /// - Parameter parameters: Parameters that are applicable for sharing to Mail. 76 | func share(_ parameters: MailShareParameters) { 77 | self.shareIfPossible(destination: parameters.shareDestination) { 78 | self.temporarySharingBarButtonItemAttributes = UIBarButtonItem.appearance().titleTextAttributes(for: .normal) 79 | UIBarButtonItem.appearance().setTitleTextAttributes(self.sharingBarButtonItemAttributes, for: .normal) 80 | 81 | if let backgroundColor = self.sharingBackgroundColor { 82 | self.temporarySharingBackgroundImage = UINavigationBar.appearance().backgroundImage(for: UIBarMetrics.default) 83 | UINavigationBar.appearance().setBackgroundImage(UIImage(color: backgroundColor), for: UIBarMetrics.default) 84 | } 85 | 86 | let mailController = MFMailComposeViewController() 87 | 88 | mailController.navigationBar.titleTextAttributes = self.sharingTitleTextAttributes 89 | 90 | mailController.mailComposeDelegate = self 91 | mailController.setSubject(parameters.subject ?? "") 92 | mailController.setMessageBody(parameters.message ?? "", isHTML: parameters.isHTML) 93 | mailController.setToRecipients(parameters.toRecepients) 94 | mailController.setCcRecipients(parameters.ccRecepients) 95 | mailController.setBccRecipients(parameters.bccRecepients) 96 | 97 | parameters.attachments?.forEach { attachment in 98 | mailController.addAttachmentData(attachment.data, mimeType: attachment.attachmentType.identifier, fileName: attachment.filename) 99 | } 100 | 101 | self.present(mailController, animated: true, completion: nil) 102 | } 103 | } 104 | 105 | /// Share to a social network. 106 | /// This includes SocialNetwork.twitter, .facebook, .sinaWeibo, and .tencentWeibo. 107 | /// 108 | /// - Parameter parameters: Parameters that are applicable for sharing to a social network. 109 | @available(iOS, deprecated: 11.0) 110 | func share(_ parameters: SocialShareParameters) { 111 | self.shareIfPossible(destination: parameters.network) { 112 | let destination = parameters.network 113 | if let composeController = SLComposeViewController(forServiceType: destination.name) { 114 | composeController.setInitialText(parameters.message) 115 | 116 | parameters.urls.flatMap { $0 }?.lazy.forEach({ composeController.add($0) }) 117 | parameters.images.flatMap { $0 }?.lazy.forEach({ composeController.add($0) }) 118 | 119 | composeController.completionHandler = { result in 120 | let succeeded = (result == SLComposeViewControllerResult.done) 121 | self.sharingCompleted?((success: succeeded, sharingService: destination.activityType)) 122 | self.dismiss(animated: true, completion: nil) 123 | } 124 | 125 | self.present(composeController, animated: true, completion: nil) 126 | } 127 | } 128 | } 129 | 130 | /// Share to the user's pasteboard. 131 | /// 132 | /// - Parameter parameters: Parameters that are applicable for sharing to the pasteboard. 133 | func share(_ parameters: PasteboardShareParameters) { 134 | self.shareIfPossible(destination: parameters.shareDestination) { 135 | if let string = parameters.string { 136 | UIPasteboard.general.string = string 137 | } 138 | 139 | if let url = parameters.url { 140 | UIPasteboard.general.url = url 141 | } 142 | 143 | if let image = parameters.image { 144 | UIPasteboard.general.image = image 145 | } 146 | } 147 | } 148 | 149 | /// Share to the user's photo library. 150 | /// 151 | /// - Parameter parameters: Parameters that are applicable for sharing to the photo library. 152 | func share(_ parameters: PhotosShareParameters) { 153 | PHPhotoLibrary.shared().performChanges({ 154 | let changeRequest = PHAssetChangeRequest.creationRequestForAsset(from: parameters.image) 155 | changeRequest.creationDate = Date() 156 | }) { success, error in 157 | let saved = (error == nil && success) 158 | let activity = parameters.shareDestination.activityType 159 | self.sharingCompleted?((success: saved, sharingService: activity)) 160 | } 161 | } 162 | 163 | } 164 | 165 | extension UIViewController: MFMailComposeViewControllerDelegate { 166 | 167 | public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { 168 | // Reset the UIAppearance styles to what they were before we started 169 | UINavigationBar.appearance().setBackgroundImage(self.temporarySharingBackgroundImage, for: UIBarMetrics.default) 170 | if let temporarySharingBarButtonItemAttributes = self.temporarySharingBarButtonItemAttributes as? [NSAttributedString.Key : Any] { 171 | UIBarButtonItem.appearance().setTitleTextAttributes(temporarySharingBarButtonItemAttributes, for: .normal) 172 | } 173 | 174 | self.temporarySharingBackgroundImage = nil 175 | self.temporarySharingBarButtonItemAttributes = nil 176 | 177 | controller.dismiss(animated: true, completion: nil) 178 | 179 | let success = (result == MFMailComposeResult.sent || result == MFMailComposeResult.saved) 180 | let mailDestination = MailShareDestination() 181 | self.sharingCompleted?((success: success, sharingService: mailDestination.activityType)) 182 | } 183 | 184 | } 185 | 186 | extension UIViewController: MFMessageComposeViewControllerDelegate { 187 | 188 | public func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) { 189 | // Reset the UIAppearance styles to what they were before we started 190 | UINavigationBar.appearance().setBackgroundImage(self.temporarySharingBackgroundImage, for: UIBarMetrics.default) 191 | if let temporarySharingBarButtonItemAttributes = self.temporarySharingBarButtonItemAttributes as? [NSAttributedString.Key : Any] { 192 | UIBarButtonItem.appearance().setTitleTextAttributes(temporarySharingBarButtonItemAttributes, for: .normal) 193 | } 194 | 195 | self.temporarySharingBackgroundImage = nil 196 | self.temporarySharingBarButtonItemAttributes = nil 197 | 198 | controller.dismiss(animated: true, completion: nil) 199 | 200 | let success = (result == MessageComposeResult.sent) 201 | let messagesDestination = MessagesShareDestination() 202 | self.sharingCompleted?((success: success, sharingService: messagesDestination.activityType)) 203 | } 204 | 205 | } 206 | 207 | private extension UIViewController { 208 | 209 | /// A method that determines whether we can currently share to a specified ShareDestination. 210 | /// 211 | /// - Parameter destination: The ShareDestination whose availability we should check. 212 | /// - Parameter canShareAction: The action to take if you can indeed share to a destination. 213 | func shareIfPossible(destination: ShareDestination, canShareAction: () -> Void) { 214 | if destination.canShare { 215 | canShareAction() 216 | } else { 217 | self.sharingCompleted?((success: false, sharingService: destination.activityType)) 218 | } 219 | } 220 | 221 | } 222 | 223 | 224 | // MARK: Associated objects 225 | 226 | public extension UIViewController { 227 | 228 | private enum AssociatedObjectKeys { 229 | static var sharingBarButtonItemAttributes = "UIViewController.sharingBarButtonItemAttributes" 230 | static var sharingTitleTextAttributes = "UIViewController.sharingTitleTextAttributes" 231 | static var sharingBackgroundColor = "UIViewController.sharingBackgroundColor" 232 | static var sharingCompleted = "UIViewController.sharingCompleted" 233 | 234 | static var temporarySharingBarButtonItemAttributes = "UIViewController.temporarySharingBarButtonItemAttributes" 235 | static var temporarySharingBackgroundImage = "UIViewController.temporarySharingBackgroundImage" 236 | } 237 | 238 | /// A property for configuring the `backgroundColor` on `MFMailComposeViewController` or `MFMessageComposeViewController`. 239 | var sharingBackgroundColor: UIColor? { 240 | get { 241 | return objc_getAssociatedObject(self, &AssociatedObjectKeys.sharingBackgroundColor) as? UIColor 242 | } set { 243 | objc_setAssociatedObject(self, &AssociatedObjectKeys.sharingBackgroundColor, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) 244 | } 245 | } 246 | 247 | /// A property for configuring the `titleTextAttributes` on `MFMailComposeViewController`. 248 | /// Unfortunately this does not work on `MFMessageComposeViewController`. 249 | var sharingTitleTextAttributes: [ NSAttributedString.Key : Any ]? { 250 | get { 251 | return objc_getAssociatedObject(self, &AssociatedObjectKeys.sharingTitleTextAttributes) as? [ NSAttributedString.Key : Any ] 252 | } set { 253 | objc_setAssociatedObject(self, &AssociatedObjectKeys.sharingTitleTextAttributes, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) 254 | } 255 | } 256 | 257 | /// A property for configuring the `barButtonItemAttributes` on `MFMailComposeViewController` or `MFMessageComposeViewController`. 258 | var sharingBarButtonItemAttributes: [ NSAttributedString.Key : Any ]? { 259 | get { 260 | return objc_getAssociatedObject(self, &AssociatedObjectKeys.sharingBarButtonItemAttributes) as? [ NSAttributedString.Key : Any ] 261 | } set { 262 | objc_setAssociatedObject(self, &AssociatedObjectKeys.sharingBarButtonItemAttributes, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) 263 | } 264 | } 265 | 266 | /// A closure that fires when a sharing event completes, whether it is succeeds or fails. 267 | var sharingCompleted: SharingCompletedEvent? { 268 | get { 269 | guard let box = objc_getAssociatedObject(self, &AssociatedObjectKeys.sharingCompleted) as? SharingCompletedEventBox else { 270 | return nil 271 | } 272 | 273 | return box.event 274 | } set { 275 | objc_setAssociatedObject(self, &AssociatedObjectKeys.sharingCompleted, SharingCompletedEventBox(event: newValue), .OBJC_ASSOCIATION_RETAIN_NONATOMIC) 276 | } 277 | } 278 | 279 | // MARK: Temporary properties which are used to store in between presenting 280 | // `MFMailComposeViewController` and `MFMessageComposeViewController` since they require delegate 281 | // callbacks. 282 | 283 | /// A temporary store for the original `UINavigationBar.backgroundImage` while we are presenting a 284 | /// `MFMailComposeViewController` or `MFMessageComposeViewController`, to be restored after use. 285 | fileprivate var temporarySharingBackgroundImage: UIImage? { 286 | get { 287 | return objc_getAssociatedObject(self, &AssociatedObjectKeys.temporarySharingBackgroundImage) as? UIImage 288 | } set { 289 | objc_setAssociatedObject(self, &AssociatedObjectKeys.temporarySharingBackgroundImage, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) 290 | } 291 | } 292 | 293 | /// A temporary store for the original `titleTextAttributes` while we are presenting a 294 | /// `MFMailComposeViewController` or `MFMessageComposeViewController`, to be restored after use. 295 | var temporarySharingBarButtonItemAttributes: [ AnyHashable : Any ]? { 296 | get { 297 | return objc_getAssociatedObject(self, &AssociatedObjectKeys.temporarySharingBarButtonItemAttributes) as? [ AnyHashable : Any ] 298 | } set { 299 | objc_setAssociatedObject(self, &AssociatedObjectKeys.temporarySharingBarButtonItemAttributes, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) 300 | } 301 | } 302 | } 303 | 304 | /// UIImage extension to create an image from specified color 305 | private extension UIImage 306 | { 307 | convenience init?(color: UIColor, size: CGSize = CGSize(width: 1.0, height: 1.0)) { 308 | let rect = CGRect(origin: .zero, size: size) 309 | 310 | UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0) 311 | defer { UIGraphicsEndImageContext() } 312 | 313 | color.setFill() 314 | UIRectFill(rect) 315 | let image = UIGraphicsGetImageFromCurrentImageContext() 316 | 317 | guard let cgImage = image?.cgImage else { return nil } 318 | self.init(cgImage: cgImage) 319 | } 320 | } 321 | 322 | // Boxing so we can store the sharingCompleted closure on UIViewController 323 | private class SharingCompletedEventBox { 324 | 325 | var event: SharingCompletedEvent? 326 | 327 | init(event: SharingCompletedEvent?) { 328 | self.event = event 329 | } 330 | 331 | } 332 | --------------------------------------------------------------------------------