-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocialShareHelper.swift
205 lines (167 loc) · 7.62 KB
/
SocialShareHelper.swift
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//
// SocialShareHelper.swift
//
// Created by Harit Kothari on 01/12/16.
// Copyright © 2016 Simform. All rights reserved.
//
import UIKit
import FBSDKShareKit
import TwitterKit
import MessageUI
import Photos
enum ShareType {
case image
case video
case link
case any
}
// Custom Activity/action
// https://bjartes.wordpress.com/2015/02/19/creating-custom-share-actions-in-ios-with-swift/
class SocialShareHelper: NSObject {
override private init() { }
static let sharedInstance : SocialShareHelper = {
let instance = SocialShareHelper()
return instance
}()
// https://developers.facebook.com/docs/sharing/ios
func shareViaFacebook(parentVC:UIViewController, shareType:ShareType, shareContent:[AnyObject], optionalText:String = "") {
if shareContent.count < 1 {
return
}
var content:FBSDKSharingContent!
switch shareType {
case .image:
content = FBSDKShareMediaContent()
let photo = FBSDKSharePhoto(image: shareContent.first as! UIImage, userGenerated: true)
(content as? FBSDKShareMediaContent)?.media = [photo!]
case .video:
content = FBSDKShareMediaContent()
let video = FBSDKShareVideo(videoURL: URL(string: "http://www.halal.place/video")!)
(content as? FBSDKShareMediaContent)?.media = [video!]
case .link:
content = FBSDKShareLinkContent()
for shareObj in shareContent {
if shareObj is URL {
(content as? FBSDKShareLinkContent)?.contentURL = shareObj as? URL
} else if shareObj is String {
(content as? FBSDKShareLinkContent)?.contentTitle = shareObj as? String // "Content Title"
}
}
// (content as? FBSDKShareLinkContent)?.imageURL = URL(string: "http://www.ezdrone.com/wp-content/uploads/2015/11/Google-logo-2-2014.png")
(content as? FBSDKShareLinkContent)?.contentDescription = optionalText // "This is the description"
default:
fatalError("undefined media")
}
let shareDialog = FBSDKShareDialog()
if UIApplication.shared.canOpenURL(URL(string: "fbauth2://")!) == true {
shareDialog.mode = .native
} else {
shareDialog.mode = .browser
}
shareDialog.fromViewController = parentVC
shareDialog.shareContent = content
shareDialog.delegate = self
shareDialog.show()
}
// https://docs.fabric.io/apple/twitter/compose-tweets.html
func shareViaTwitter(parentVC:UIViewController, shareType:ShareType, shareContent:[AnyObject], optionalText:String = "") {
if shareContent.count < 1 {
return
}
let composer = TWTRComposer()
composer.setText("just setting up my Fabric")
switch shareType {
case .image:
composer.setImage(shareContent.first as? UIImage)
composer.setText(optionalText)
case .video:
fatalError("unsupported media")
case .link:
let shareText = optionalText.isBlank() ? shareContent.first?.absoluteString : (optionalText + " " + (shareContent.first?.absoluteString)!)
composer.setText(shareText!)
default:
fatalError("undefined media")
}
// Called from a UIViewController
composer.show(from: parentVC) { result in
if (result == TWTRComposerResult.cancelled) {
DLog(message: "Tweet composition cancelled")
}
else {
DLog(message: "Sending tweet!")
}
}
}
var documentController:UIDocumentInteractionController?
func shareViaInstagram(parentVC:UIViewController, shareType:ShareType, shareContent:[AnyObject], optionalText:String = "") {
let instagramURL = URL(string: "instagram://app")
if UIApplication.shared.canOpenURL(instagramURL!) == false {
// cant support
DLog(message: "no instagram app")
Extension.showToast(type: .error, message: "You do not have instagram app.")
return
}
if shareType != .image {
fatalError("You cannot share anything except image to instagram")
}
let yourImage = shareContent.first as? UIImage
let imageData = UIImageJPEGRepresentation(yourImage!, 100)
// let captionString = "caption"
let writePath = Extension.getDocumentsDirectory().appendingPathComponent("instagram.igo")
do {
try imageData?.write(to: writePath, options: [.atomic])
} catch let error {
DLog(message: "\(error)")
}
let fileURL = writePath
documentController = UIDocumentInteractionController(url: fileURL)
documentController?.delegate = self
// documentController?.uti = "com.instagram.exlusivegram"
documentController?.uti = "com.instagram.exclusivegram"
documentController?.annotation = [optionalText]
let rect = CGRect(x: 0, y: 0, width: 300, height: 300)
documentController?.presentOpenInMenu(from: rect, in: parentVC.view, animated: true)
// documentController.presentOpenInMenuFromRect(self.view.frame, inView: self.view, animated: true)
}
func shareViaEMail(parentVC:UIViewController, shareType:ShareType, shareContent:[AnyObject]) -> Bool {
if MFMailComposeViewController.canSendMail() {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setToRecipients(["support@halal.place"])
mailComposerVC.setSubject("Feedback / support")
mailComposerVC.setMessageBody("", isHTML: false)
parentVC.present(mailComposerVC, animated: true, completion: nil)
return true
} else {
Extension.showToast(type: .error, message: "Your device could not send e-mail. Please check e-mail configuration and try again.")
return false
}
}
func shareViaDialog(parentVC:UIViewController, shareType:ShareType, shareContent:[AnyObject]) {
let activityViewController = UIActivityViewController(activityItems: shareContent, applicationActivities: nil)
activityViewController.excludedActivityTypes = [.airDrop, .openInIBooks, .postToVimeo, .postToWeibo, .postToTencentWeibo]
parentVC.present(activityViewController, animated: true, completion: {
DLog(message: "shared")
})
}
}
extension SocialShareHelper: FBSDKSharingDelegate {
func sharer(_ sharer: FBSDKSharing!, didCompleteWithResults results: [AnyHashable : Any]!) {
DLog(message: "\(results)")
}
func sharer(_ sharer: FBSDKSharing!, didFailWithError error: Error!) {
DLog(message: "\(error)")
Extension.showToast(type: .error, message: "You do not have facebook app or something went wrong while sharing.")
}
func sharerDidCancel(_ sharer: FBSDKSharing!) {
DLog(message: "sharerDidCancel")
}
}
extension SocialShareHelper: MFMailComposeViewControllerDelegate {
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
DLog(message: "result \(result) error \(error)")
}
}
extension SocialShareHelper: UIDocumentInteractionControllerDelegate {
}