Text not showing up in UIActivityViewcontroller - ios

I have a share function in my app that calls a uiactivityviewcontroller with text,url and an image to share.
The url and image work fine, but the text doesn't show up..
Here is the code I am using:
var highscore:Int = NSUserDefaults.standardUserDefaults().integerForKey("highscore")
var text = "My favourite game: 'Zig Zag!' My highscore: \(highscore)"
let link = NSURL(string: "https://itunes.apple.com/us/app/go-down!/id101725307?l=nl&ls=1&mt=8")
let actviewcon = UIActivityViewController(activityItems: [text,link!,shareima!], applicationActivities: nil)
actviewcon.excludedActivityTypes = [
UIActivityTypePrint,
UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList,
]
actviewcon.completionWithItemsHandler = {
(activity, success, items, error) in
//println("Activity: \(activity) Success: \(success) Items: \(items) Error: \(error)")
}
self.presentViewController(actviewcon, animated: true, completion: nil)
}

Related

Share Image Via URL

I am creating Images app from my WordPress website with json and i am using swift, i want to share image on Social Networks from my app , currently i tried this code it works but only with image name i want to share image from image url, is that possible ?
this is my code
let myWebsite = NSURL(string: "nice")
let img: UIImage = UIImage(named:"splash")!
guard let url = myWebsite else {
print("nothing found")
return
}
let shareItems:Array = [img,url]
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivity.ActivityType.print, UIActivity.ActivityType.postToWeibo, UIActivity.ActivityType.copyToPasteboard, UIActivity.ActivityType.addToReadingList, UIActivity.ActivityType.postToVimeo]
self!.present(activityViewController, animated: true, completion: nil)
If you need to download an image and then share you should do that separately, there is no single method that does that for you. Here is how:
func shareImageFromUrl(_ string: String) {
guard let myUrl = URL(string: string) else {
print("Invalid url!")
return
}
URLSession.shared.dataTask(with: myUrl) { (data, _, _) in
guard let data = data,
let image = UIImage(data: data) else
{ return }
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: [image], applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivity.ActivityType.print, UIActivity.ActivityType.postToWeibo, UIActivity.ActivityType.copyToPasteboard, UIActivity.ActivityType.addToReadingList, UIActivity.ActivityType.postToVimeo]
DispatchQueue.main.async {
self.present(activityViewController, animated: true, completion: nil)
}
}.resume()
}

How can I share location from my app to WhatsApp in Swift 3

I'm trying to share a location from my iOS app to WhatsApp and I want it to look like this:
What I'm doing is sending vCard with this code :
func vCardURL(from coordinate: CLLocationCoordinate2D, with name: String?) -> URL {
let vCardFileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("Shared Location.loc.vcf")
let vCardString = [
"BEGIN:VCARD",
"VERSION:3.0",
//"PRODID:-//Apple Inc.//iPhone OS 10.3.2//EN",
"N:;My Location;;;",
"FN:My Location",
"item1.URL;type=pref:https://maps.apple.com/?ll=50.359890\\,12.934560&q=My%20Location&t=m",
"item1.X-ABLabel:map url",
"END:VCARD"
].joined(separator: "\n")
do {
try vCardString.write(toFile: vCardFileURL.path, atomically: true, encoding: .utf8)
} catch let error {
print("Error, \(error.localizedDescription), saving vCard: \(vCardString) to file path: \(vCardFileURL.path).")
}
print(vCardString)
return vCardFileURL
} // end of function
// calling the methood above
let vURL = LocationVCard.vCardURL(from: self.newLocation.coordinate, with: "Berlin")
let activityViewController = UIActivityViewController(activityItems: [vURL], applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
But I always end up with this style instead of what I want:
No need to save the vCard string to a file, just convert it to Data and wrap it with NSItemProvider with the correct type identifier public.vcard, and it'll work just fine:
let data = vCardString.data(using: .utf8)! as NSData
let item = NSItemProvider(item: data, typeIdentifier: "public.vcard")
let activityViewController = UIActivityViewController(activityItems: [item], applicationActivities: nil)

how to share video like iPhone default share

I want to share a video in my app like default share of iPhone video share.
I have written code but its not like iPhone default share
here are difference between my share function and iPhone default share
func shareVideo() {
let paths = NSSearchPathForDirectoriesInDomains(
NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let documentsDirectory: AnyObject = paths[0]
let dataPath = documentsDirectory.stringByAppendingPathComponent("saveFileName")
let URL: String = documentsDirectory.stringByAppendingPathComponent(dataPath)
let someText: String = "A nice song"
let urlToShare: NSURL = NSURL.fileURLWithPath(URL, isDirectory: false)
let dataToShare: [AnyObject] = [someText, urlToShare]
let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: dataToShare, applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact]
self.presentViewController(activityViewController, animated: true, completion: { _ in })
}

UIActivityViewController is always sharing image before text

In my app, I want to share the current song and artist followed by the album artwork. Here is my code:
var sharingItems = [AnyObject]()
let currentSong = musicPlayer.nowPlayingItem?.title
let currentArtist = musicPlayer.nowPlayingItem?.artist
let artwork = musicPlayer.nowPlayingItem?.artwork
let artworkImage = artwork?.imageWithSize(CGSizeMake(100, 100))
let shareText = "Now playing \(currentSong!) by \(currentArtist!) via Ryan's awesome app"
sharingItems.append(shareText) //text first, then image
sharingItems.append(artworkImage!)
let activityViewController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypeOpenInIBooks, UIActivityTypePrint, UIActivityTypeSaveToCameraRoll]
self.presentViewController(activityViewController, animated: true, completion: nil)
When I choose a share option, like Messges, the image is before the text in the TextField. Why is the image first if it's last in the array?

Ios Swift Sharing on WhatsApp and FB

I am experiencing a few issue with the Share option in iOS8.
With the below code I am able to correctly share by Email and Text(SMS) including my text strings and my image.
But when I try to share to Facebook, the generated post only includes my image (skipping the text strings) while if I try to share on WhatsApp, the image is skipped and only the text strings are passed.
Does anybody have any suggestion?
My code:
#IBAction func shareButtonClicked(sender: UIBarButtonItem) {
var kidToShare1 : String? = noKids
if selectedKid == nil {
kidToShare1 = noKids
println("kid is nil")
} else {
kidToShare1 = selectedKid!.kidName
}
let kidToShare = NSLocalizedString("Kid: ", comment:"Kid to share") + kidToShare1!
let imageToShare = photo! ?? noPhoto!
let textToShare = NSLocalizedString("Event Description: ", comment:"Text to share") + descriptionText as NSString
let dateToShare = NSLocalizedString("Event Date: ", comment: "Date to share") + formatDate(date) as NSString
let message = NSLocalizedString("My App", comment: "Main sharing message")
let objectsToShare = [message, dateToShare, kidToShare, textToShare, imageToShare]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAirDrop, UIActivityTypeAssignToContact, UIActivityTypeAddToReadingList,UIActivityTypeCopyToPasteboard,UIActivityTypeSaveToCameraRoll,UIActivityTypePrint]
self.presentViewController(activityVC, animated: true, completion: nil)
}
}

Resources