Reputation: 1368
Trying to re-integrate the sharing button in my application. Apple again changed things in Swift 3
for iOS 10
. I just updated to the Xcode 8
release version and it's been giving me some issues with UIActivities
.
Found that many of them now begin with UIActivityType.
in order for the compiler to not fail. However many of them still are using com.apple
it also made me cast the activityType
array to type [Any]
for some reason.
So with that... Does anyone know why they did that? and what do you put in the for argument in the activityTypesToExclude.contains(where: (Any) throws -> Bool)
method?
internal func _shouldExcludeActivityType(_ activity: UIActivity) -> Bool {
let activityTypesToExclude = [
"com.apple.reminders.RemindersEditorExtension",
UIActivityType.openInIBooks,
UIActivityType.print,
UIActivityType.assignToContact,
UIActivityType.postToWeibo,
"com.google.Drive.ShareExtension",
"com.apple.mobileslideshow.StreamShareService"
] as [Any]
if let actType = activity.activityType {
if activityTypesToExclude.contains(where: (Any) throws -> Bool) {
return true
}
else if super.excludedActivityTypes != nil {
return super.excludedActivityTypes!.contains(actType)
}
}
return false
}
Upvotes: 2
Views: 1650
Reputation: 47886
UIActivityType
is a type-safe thin wrapper for String representation of activity type. If you have found some activity types which has no predefined constants, you can define your own with extension.
extension UIActivityType {
static let remindersEditorExtension = UIActivityType(rawValue: "com.apple.reminders.RemindersEditorExtension")
static let googleDriveShareExtension = UIActivityType(rawValue: "com.google.Drive.ShareExtension")
static let streamShareService = UIActivityType(rawValue: "com.apple.mobileslideshow.StreamShareService")
}
With the extension above, you can write something like this:
internal func _shouldExcludeActivityType(_ activity: UIActivity) -> Bool {
let activityTypesToExclude: [UIActivityType] = [
.remindersEditorExtension,
.openInIBooks,
.print,
.assignToContact,
.postToWeibo,
.googleDriveShareExtension,
.streamShareService
]
if let actType = activity.activityType {
if activityTypesToExclude.contains(actType) {
return true
} else if let superExcludes = super.excludedActivityTypes {
return superExcludes.contains(actType)
}
}
return false
}
You should not use raw String representations directly in Swift 3 code.
So, about why they did that?: with this change, programmers get far less chance to accidentally include non-activityType Strings where activityType identifiers needed.
Similar changes have been made for some String constants, for example Notification.Name
.
Upvotes: 7