Here is my code
let myURL = URL(string: "http://www.dwz.cn/BabySmarter")
let shareActivity = UIActivityViewController(activityItems: [myURL!, "我的宝宝比\(String(describing: Int(finalScore!)))% 的宝宝更发展!你的呢?"], applicationActivities: nil)
shareActivity.popoverPresentationController?.sourceView = self.view
self.present(shareActivity, animated: true, completion: nil)
Here is the result:
This has to be a glitch. Or am I completely wrong here?
May be problem with URL , you can pass url as string.
let shareActivity = UIActivityViewController(activityItems: ["http://www.dwz.cn/BabySmarter 我的宝宝比\(String(describing: Int(finalScore!)))% 的宝宝更发展!你的呢?"], applicationActivities: nil)
shareActivity.popoverPresentationController?.sourceView = self.view
self.present(shareActivity, animated: true, completion: nil)
Related
I have a situation. I launch an activity controller to share files but I want my application not to appear in the view.
if let url = FileManager.filePath(forKey: documentModel.localUrl ?? "", directory: "file") {
let documento = NSData(contentsOf: url)
let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: [url], applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivity.ActivityType(rawValue: "mx.com.myapp.txt")]
activityViewController.popoverPresentationController?.sourceView=self.view
present(activityViewController, animated: true, completion: nil)
I share an invite through UIActivityViewController by passing the text. I am able to achieve it. But I have display issue as shown in the image below. A greyed box displayed over the text at the top. Below is my code
let objectToShare = inviteReferrals.inviteData
let vc = UIActivityViewController(activityItems: [objectToShare], applicationActivities: [])
present(vc, animated: true)
Can any one help me, why this box comes and how to avoid it.
ActivityViewControllerDisplay
let activityViewController = UIActivityViewController(activityItems: [objectToShare], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)
I want to share an URL file that has been created in the app and a text. But it seems it only can share text and not any data like URL or UIImage.
The code I am using:
let sharedVideo = Video(Title: _data[1], VideoID: _data[0], Duration: _data[3], ViewCount: _data[2])
let sharedURL = VideoManager.exportData(video: sharedVideo)
let shareItems:Array = [sharedURL,"check this out baby!"] as [Any]
let activityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
Also I use an UIImage object instead of sharedURL to see If it has problem with URL or not.
It doesn't work even with an image file. When I click on share button inside UIActivityViewController, It works just for text, no URL nor image.
I am using Swift 3 in Xcode 8.
Thanks.
PS: I am sure about sharedURL object that it isn't nil nor undefined.
Try this:
#IBAction func btnExport(sender: AnyObject)
{
let someText:String = "Hello want to share text also"
let objectsToShare:URL = URL(string: "http://www.google.com")!
let sharedObjects:[AnyObject] = [objectsToShare as AnyObject,someText as AnyObject]
let activityViewController = UIActivityViewController(activityItems : sharedObjects, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
activityViewController.excludedActivityTypes = [ UIActivityType.airDrop, UIActivityType.postToFacebook,UIActivityType.postToTwitter,UIActivityType.mail]
self.present(activityViewController, animated: true, completion: nil)
}
Simplified solution working with Swift 5+
let url = URL(string: shareUrlString)!
let text = "Some text that you want shared"
let activity = UIActivityViewController(activityItems: [url, text], applicationActivities: nil)
present(activity, animated: true)
I am making a share function in my game and I have the code and it works fine on iPhone but when I test it on a iPad, when I tap the share button the app crashes. I am using the following code for the share button
let textToShare = "Check out this website!"
if let myWebsite = NSURL(string: "http://www.apple.com/") {
let objectsToShare = [textToShare, myWebsite]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
self.view?.window?.rootViewController?.presentViewController(activityVC, animated: true, completion: nil)
}
The UIActivityViewController's has non-null popoverPresentationController property when running on iPad. So, try below.
if let wPPC = activityVC.popoverPresentationController {
wPPC.sourceView = some view
// or
wPPC.barButtonItem = some bar button item
}
presentViewController( activityVC, animated: true, completion: nil )
Building on #Satachito's answer: As the sourceView you can create an (invisible) CGRect at the place the popup should point to, and set the arrow in that direction:
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
if UIDevice.current.userInterfaceIdiom == .pad {
activityVC.popoverPresentationController?.sourceView = UIApplication.shared.windows.first
activityVC.popoverPresentationController?.sourceRect = CGRect(x: 0, y: 0, width: 300, height: 350)
activityVC.popoverPresentationController?.permittedArrowDirections = [.left]
}
UIApplication.shared.windows.first?.rootViewController?.present(activityVC, animated: true, completion: nil)
The popoverPresentationController sourceView needs to be set to current view.
let activityVC = UIActivityViewController(activityItems: [quoteController.attributedString, view.screenShot()], applicationActivities: [])
present(activityVC, animated: true)
activityVC.popoverPresentationController?.sourceView = view
I have a UIActivityViewController for a share button. For iPhone I have it as a regular UIActivityViewController and for iPad its in a PopOverViewController. This is the code I have for it
let textToShare = "Check out this website!"
if let myWebsite = NSURL(string: "http://www.apple.com/") {
let objectsToShare = [textToShare, myWebsite]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
if let popUpVC = activityVC.popoverPresentationController {
popUpVC.permittedArrowDirections = .Any
popUpVC.sourceRect = share.frame
}
self.view?.window?.rootViewController?.presentViewController(activityVC, animated: true, completion: nil)
}
When I press the share button on a iPad it just crashes with a (lldb). But when I have it present from a view it works but isn't in the right position. This is the code I am using for the present from a view.
popUpVC.sourceView = self.view
Try this, you have to check if the device you are currently running on responds to popoverPresentationController because popoverPresentationController is new to iOS 8 and will crash on iOS 7. It'll also be nil on iPhone because it's only in a UIPopover on iPad.
let activityViewController = UIActivityViewController(activityItems: [myText, myUrl], applicationActivities: nil)
if activityViewController.respondsToSelector("popoverPresentationController") {
// iOS8+
view.presentViewController(activityViewController, animated: true, completion: nil)
activityViewController.popoverPresentationController?.sourceView = view
} else {
view.presentViewController(activityViewController, animated: true, completion: nil)
}