I'm developing a custom UTI like "com.xyz".
All the apps supporting this UTI may be shown in the UIActivityViewController.
Code snippet:
let extensionItem = NSExtensionItem()
let data = NSItemProvider.init(item: nil, typeIdentifier: "com.xyz")
extensionItem.attachments = [data]
let activity = UIActivityViewController(
activityItems: [extensionItem],
applicationActivities: nil
)
present(activity, animated: true, completion: nil)
In order to have a better user experience, I would like to check if the iOS device has any App supporting "com.xyz" before presenting the UIActivityViewController.
Just like check URL:
UIApplication.shared.canOpenURL(URL(string: urlStr)!)
Otherwise the user may see an empty UIActivityViewController without any app.
As I know, there is an ActivityNotFoundException could be try and catch to handle the app not found situation in Android.
Does iOS support something like ActivityNotFoundException?
THANKS! 🙏
Related
I was using a Share extension in my app in order to import audio files and it was working on iOS12. Now in iOS 13 is not working anymore, when I press the share button my app doesn't appear in the share sheet.
I think that maybe something has changed in the plist or similar but I coudn't find any information.
Does anyone have the same problem?
NB: I don't wanna use the copy - paste strategies, only the share extension.
Try this for iOS 13
DispatchQueue.main.async {
let activityItem = URL.init(fileURLWithPath: Bundle.main.path(forResource: "audio", ofType: "mp3")!)
let activityVC = UIActivityViewController(activityItems: [activityItem],applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = self.view
self.present(activityVC, animated: true, completion: nil)
}
For IOS 13 you Should add below code inside your open url method.
UISceneOpenExternalURLOptions * options = [[UISceneOpenExternalURLOptions alloc] init];
options.universalLinksOnly = false;
I am working on a sharing functionality where I have achieved the basic sharing part and is working perfectly. Below is the code for same
let message = "Download the app from the link below"
//Set the link to share.
if let link = NSURL(string: "http://yoururl.com") {
let objectsToShare = [message,link] as [Any]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.excludedActivityTypes = [UIActivityType.airDrop, UIActivityType.addToReadingList]
self.present(activityVC, animated: true, completion: nil)
}
The above code is working great and gives the output as seen in the screenshot
What I want is to have an extra icon/option to share the content for the frequently used app.
for e.g. I use Whatsapp frequently so the share window should show WhatsApp icon somewhere besides "More".
any help will be highly appreciated. TIA
This can't be done because the share sheet is a library provided by apple and only the user can rearrange icons on their own!
I am trying to implement the Facebook Share in my quiz app. The content is app store link and quiz score.Which is working fine in simulator but in device shows in different and not showing my description. Here is my Code
func ShareFB() {
let fbVC = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
fbVC?.setInitialText("Hey! I scored \(String(describing: UserDefaults.standard.string(forKey: "TOTAL_SCORE")!)) in Test. This is really interesting! You can also try.")
fbVC?.add(URL(string: "https://itunes.apple.com/us/app/test-app/i?ls=1&mt=8"))
fbVC?.add(UIImage(named: "AppIcon"))
present(fbVC!, animated: true) { _ in
}
}
also attaching screen Shot of simulator and device.
now you cannot share image, text and url simultaneously in facebook. also Facebook does not allow pre filled text now
Why don't you use UIActivityController for such functionality?
Example:
let shareItems = [
"Hey! I scored \(String(describing: UserDefaults.standard.string(forKey: "TOTAL_SCORE")!)) in Test. This is really interesting! You can also try.",
URL(string: "https://itunes.apple.com/us/app/test-app/i?ls=1&mt=8"),
UIImage(named: "AppIcon")] //Add the items you want to share in this array
let activityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
Edit:
Check this out:
https://stackoverflow.com/a/30020929/5716829
According to Facebook's 2.3 policy rules, you can't share pre-filled content because of policy violation. Facebook does not allow you to share the pre-filled user message parameters with any content the user didn't entered himself.
If you want to share content on Facebook then user has to write/enter himself in Facebook prompt message dialog.
For more info, please visit: https://developers.facebook.com/docs/apps/review/prefill
I am trying to share video PHAsset via UIActivityController using requestAVAsset. This works with Messaging, but not with AirDrop, indicating as 'Failed'.
PHCachingImageManager.default().requestAVAsset(forVideo: asset, options: nil, resultHandler:
{ (givenAsset, audioMix, info) in
let videoAsset = givenAsset as! AVURLAsset
let videoURL = videoAsset.url
DispatchQueue.main.async {
let activityViewController = UIActivityViewController(
activityItems: [videoURL],
applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityType.saveToCameraRoll]
if let popoverPresentationController = activityViewController.popoverPresentationController {
popoverPresentationController.barButtonItem = (sender)
}
self.present(activityViewController, animated: true, completion: nil)
}
})
This seems to properly put up UIActivityController and only work with certain activities:
Messaging - ✔️Works, properly exports video.
AirDrop - ✖️Shows "Failed"
Dropbox - ✖️Puts up the proper Dropbox View, yet says "Unknown error occurred"
I've run into similarly odd behavior when working with PHAssets. My guess is this is a (purposely) undocumented security/sandboxing restriction.
I was able to work around this problem by copying the underlying file to a user directory, and then performing the operation on the copied file.
I did this in a loop. Occasionally, the copy fails with a vague file permissions error. When it does, I retry it after a few seconds (using DispatchQueue.main.asyncAfter). Eventually, it works!
I am trying to share videos from my app using a UIActivityViewController. Below is the code I use:
var url = NSURL(string: path!)!
var activityViewController = UIActivityViewController(activityItems: [url], applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: { () -> Void in })
The path points to a valid .mov file so no problems there. When I present the activity view controller I get the error: Unknown activity items supplied with the path to the .mov file and the sharing options only show AirDrop.
The app is running on iOS 8.
Any ideas?
EDIT I found out that when I save the video to camera roll. the user can share it with Photos app to any app. So there's nothing wrong with the video format I guess.
You should use init?(fileURLWithPath path: String, isDirectory isDir: Bool) if the movie is in resource bundle.
If you use imagePicker controller to select the video, you can get the url from info dictionary using the key UIImagePickerControllerMediaURL, the corresponding delegate method is didFinishPickingMediaWithInfo.
Try this :
var activityViewController = UIActivityViewController(activityItems: [path], applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: { () -> Void in })
I had a similar issue for a zip file and realised the url path I was passing was relative and not an absolute path. In other words, UIActivityViewController urls require the prefix file:///
I was using a relative path because the third party library Objective-Zip needs a relative path to create zip files.
Works:
file:///private/var/mobile/Containers/Data/Application/63284C22-6E22-4865-965C-3B67F58D0659/tmp/myfile.zip
Doesn't work:
/private/var/mobile/Containers/Data/Application/63284C22-6E22-4865-965C-3B67F58D0659/tmp/myfile.zip