I have a comma-separated string like this: "Banana,Cake". I want to share this string to the Reminders app through UIActivityViewController, but while opening the controller, I can't see an option for Reminders there.
I have seen it in the YouTube app. If a content is being shared, it shows the Reminders app as an option.
You must pass an array of a string and an URL to activityItems in order to display Reminders as an option.
if let url = URL(string: "http://example.com") {
let activityVc = UIActivityViewController(activityItems: ["string", url], applicationActivities: nil)
}
Related
I am building an app and I want the user to be able to tap on a URL using the URL scheme and be sent to a specific view controller on my app. That is working OK. However, I also need to get specific data onto that view controller. I need it so that User A can send an invite (URL) through Messages to User B. User B can tap on that invite (URL) to open a screen with details about the invitation from Firebase. The user can then accept the invite, or decline the invite.
I have setup my database like so:
users(collection)
"uid"(document)
parties(collection)
"partyName"(document)
respondedYes(field)
respondedNo(field)
This is the code that is in the AppDelegate for someone taps on a URL.
func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
// Open the RSVPViewController.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rsvpViewController = storyboard.instantiateViewController(withIdentifier: "RSVPViewController") as! RSVPViewController
if let window = self.window, let rootViewController = window.rootViewController {
var currentController = rootViewController
while let presentedController = currentController.presentedViewController {
currentController = presentedController
}
currentController.present(rsvpViewController, animated: true, completion: nil)
}
return true
}
I don't know how to get it so I know which invitation is being displayed after User B taps the URL so I cannot display the data. How can I get my app to work like I explained in my concept in the first paragraph? Let me know if you need more info. It is really hard to explain what I need help with.
In this scenario, the invite URL needs to include parameters that will tell your app which invite User B is opening. This can be achieved with simple GET params in your URL. So for instance your URL now might look like this: myapp:Invite?invite_id=2, where the invite_id param will be dealt with when User B opens the app.
Since it's not the best idea to give other user's access to data under the first user's UID, I would create a new "invitations" collection on firestore, where a new invitation document gets created when user A sends it. The UUID of the newly created invitation document would be the parameter you send in the URL above.
To create this URL, see this answer, Basically you would do the following:
let queryItems = [NSURLQueryItem(name: "invite_id", value: "123")]
let urlComps = NSURLComponents(string: "myapp:Invite")!
urlComps.queryItems = queryItems
let URL = urlComps.URL!
Now, in the application(_:,handleOpen:) (for the receiver's app) you can parse URL params by extending the URL class as explained in this answer.
Assuming you use the extension described in the linked answer(which I tested on playground before recommending here), you can rewrite your function as follows:
func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
let invite_id = url.valueOf("invite_id")
// get data from firestore for this invite id and store it in a struct called invite_data
// (i'm going to assume that the variable invite_data is defined now).
// Open the RSVPViewController.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rsvpViewController = storyboard.instantiateViewController(withIdentifier: "RSVPViewController") as! RSVPViewController
// tell the rsvpViewController about the data:
// this assumes you have a variable in rsvpVC that takes this info to display it.
rsvpViewController.invite_data = invite_data
if let window = self.window, let rootViewController = window.rootViewController {
var currentController = rootViewController
while let presentedController = currentController.presentedViewController {
currentController = presentedController
}
currentController.present(rsvpViewController, animated: true, completion: nil)
}
return true
}
Note that invite_data above is a struct and can contain whatever info you need to both display relevant details in the RSVP VC plus handle things like accepting or declining invites.
An alternate approach would be to simply send all the data you want to display in the rsvpViewController and not bother with an invitation document. Might be easier if the number of fields is small. But I suspect in this scenario you will want to pass things like party_id etc, so depends on your use case.
I'd like to be able to AirDrop a text file and an image at the same time using the UIActivityViewController. The code below works fine to send both file types via iMessage or eMail, but it fails when I try to use AirDrop. The code works fine for AirDropping 2 images or 2 text files, but not for one of each.
#IBAction func shareImage(_ sender: UIButton)
{
// can't seem to AirDrop a mixture of file types. ie. can send 2 images, or 2 data files, but not an image and a data file
let fileToSend: NSURL = NSURL(fileURLWithPath: dataFile!)
let image = imageView.image!
let objectsToShare = [fileToSend, image] as [Any]
let controller = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
controller.excludedActivityTypes = [UIActivityType.postToFacebook, UIActivityType.postToTwitter, UIActivityType.postToWeibo, UIActivityType.print, UIActivityType.copyToPasteboard, UIActivityType.assignToContact, UIActivityType.saveToCameraRoll, UIActivityType.postToFlickr, UIActivityType.postToTencentWeibo]
self.present(controller, animated: true, completion: nil)
}
AirDrop to an iOS device does not support sending multiple different types, but sending to a mac does. Until Apple changes that there is no code change that you can do to "fix" this.
I would like to know if there is a simplified method of setting the mail recipients when using UIActivityViewController with iOS 8? I want to add the mail recipients. I was trying to do so using the following code but the recipient is just added to the main body of text.
let url = NSURL.fileURLWithPath(fileName)
let url2 = NSURL(string: "mailto:somebody#gmail.com") as! AnyObject
let activityItems:Array = [url, url2]
let activityViewController = UIActivityViewController(activityItems: activityItems , applicationActivities: nil)
I have seen some workaround methods for iOS 6 but just wondered if there was now a simplified method to add the recipient?
When I run through the Xcode simulator, I can post both a picture and a url. One a device, however, I get an entirely different dialog.
I've used another example on here to illustrate the code
let myWebsite = NSURL(string:"http://www.google.com/")
let img: UIImage = imageView.image!
guard let url = myWebsite else {
print("nothing found")
return
}
let shareItems:Array = [img, url]
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityTypePrint, UIActivityTypePostToWeibo, UIActivityTypeCopyToPasteboard, UIActivityTypeAddToReadingList, UIActivityTypePostToVimeo]
self.presentViewController(activityViewController, animated: true, completion: nil)
On the simulator, the dialog comes up perfectly, and it posts both the picture and url to Facebook.
When I run the exact same example on my phone, a totally different dialog comes up, where I'm asked to add a line ("say something about this") and then it posts this headline, along with the google url
Interestingly enough, on the simulator, the url is posted as a url in Facebook, on the device, it's posted as a hyperlink.
Ultimately, I would like the the image and the hyperlink to appear on Facebook. However, if I was able to get the image and the url to post, I would be thrilled.
Has anyone had similar experiences?
Hey I am developing an app for the version iOS7 and above, where I have to share a content via mail or message. I use the following code
let textToShare = "Just click the following link to view "
let urlToShare = NSURL(string: "croding.com://crodid/crodname")
let objectsToShare = [textToShare, urlToShare]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
I have added the croding.com in plist. Its working fine (open our app from mail exactly what I want) only if the app is installed already otherwise this thing is just a text instead of link. I need to show it as link even app is not installed.
croding.com://crodid/crodname