Open any email attachment inside the ios app - swift

I want to open any email attachments with my iOS app. Means from my iOS app I want to open email and if there is any attachments(like pdf), I want to open this attachment with my app.I am able to open email app from my app with the following code
if let mailURL = URL(string: "message:") {
if UIApplication.shared.canOpenURL(mailURL) {
UIApplication.shared.open(mailURL, options: [:], completionHandler: nil)
}
}
After that how to open any attachments inside the iOS app. can any one help me. Can I save the attachments to my documents Directory?

Actually my requirement is from my app I will go to the mail box. If any attachment present there, I have to open it with my app. Not only I want to open it with my app, but also I want to save the file in my documents directory.
You can't tell the Mail app what to do so what you want is not exactly possible.
What you can do is add your app in the "Open In" menu by supporting some file types. So that when the user taps the attachment your app will appear in the list of apps.
You can do that this way:
Add the file types your app can handle. Follow the instructions here: https://developer.apple.com/library/archive/qa/qa1587/_index.html
Once 1. is done add the following function in your AppDelegate:
func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool {
// do what you want with file
}
To save the file in your app's document directory you can see how it's done here:
https://programmingwithswift.com/save-file-to-documents-directory-with-swift/

Related

How to solve Swift FileManager couldn’t open file because of permission and implement file access

I made a music app for macOS that need the access of the file on disk. I can get the proper url, but I can't access the file. To figure out that problem, here I create a concise code snippet to concentrate on the question itself.
.onAppear {
var url = FileManager.default.homeDirectoryForCurrentUser
url.appendPathComponent("Downloads/Test.txt")
var text: String = ""
do {
text = try String(contentsOf: url)
} catch {
print("ERROR: \(error.localizedDescription)")
}
}
The output would be like this
ERROR: The file “Test.txt” couldn’t be opened because you don’t have permission to view it.. So I noticed App Sandbox in Targets > Signing & Capabilities. But at the list of file access (as you can see in the following picture), there's only User Selected File, Downloads Folder, Pictures Folder, Music Folder and Movie Folder. So I come up with 3 questions:
What that User Selected File means? Is that means that the user can select some specific file for my app to access? If it is, how can user selects them?
How can I get access to other folders like Desktop Folder if I remain the App Sandbox capability here?
I also noticed the trash icon on right-upper corner. If I delete App Sandbox, I can access every folder I want with the user's agreement by a default system pop-up. But what the deletion of App Sandbox means? Does it make my app "untrusted" somehow when user install it or others? Does it cause any security problems?
And It couldn't be better if you can tell me the conventions major developers follow when meet this problem. I would highly appreciate your help.

Executing an iMessage Extension app by clicking on a button in the main application

Is it possible to launch my iMessage app extension from the main application( developed using Swift4), by clicking a button? I have already published the app in the store, but I don't know how to do it.
You should be able to do this by using the same iTunes link for your app, with an added query parameter of ?app=messages
Using Starbucks as an example, their App Store URL is
https://itunes.apple.com/us/app/starbucks/id331177714
But the URL to launch iMessage, and pull up your iMessage app would be
https://itunes.apple.com/us/app/starbucks/id331177714?app=messages
Not as far as I can tell, up as late as iOS 12.1
This is long-standing as a request - you can go only go in the one direction, from extension to App.
There's a forum discussion from 2016 where the answer was that it's not possible. Another 2018 forum question got no response.
From extension to app, see the technique described in this blog, using a URL:
guard let url: URL = URL(string: "AppName://?myParam=myValue") else { return }
self.extensionContext?.open(url, completionHandler: { (success: Bool) in
})
Calling the above code will open the phone app.
You can access the URL params in your phone app’s App Delegate:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return true
}

open Skype in a Swift webview

I want to open Skype in a Swift webview by just using a URL. Similar to something like the URL Scheme's provided by Apple for opening Apple Mail mailTo:// or telephone tel://.
1.) must i use the skype-API or just check if skype is installed with
#IBAction func skypeMe(sender: AnyObject) {
let installed = UIApplication.sharedApplication().canOpenURL(NSURL(string: "skype:")!)
if installed {
UIApplication.sharedApplication().openURL(NSURL(string: "skype:echo123?call")!)
} else {
UIApplication.sharedApplication().openURL(NSURL(string: "https://itunes.apple.com/in/app/skype/id304878510?mt=8")!)
}
}
2.) how must i call this? (skype:username?call)
3.) my skype-login should be called automatically
4.) i want to choose the addressee
Is that possible?
This link could help.
UIApplication.sharedApplication().openURL launches skype automatically
Try openURL(NSURL(string: "skype:")!) It may launch skype,
where you could
choose the addresses. Or may not launch.

How to save/retrieve data to a user without account (Parse + Swift)

How can I use Parse to save a user's data without them having an account but still know which phone uploaded the content? For example, if a user uploads an image, how can I display that image back to only the person who uploaded it- all without requiring the user to create an account?
Thanks in advance for any help/direction!
You could use the Installations table in your data browser:
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
// Store the deviceToken in the current Installation and save it to Parse
let installation = PFInstallation.currentInstallation()
installation.setDeviceTokenFromData(deviceToken)
installation.saveInBackground()
}
That is how you link an installation to a device. You can then add a custom column to the Installation table in your data browser such as a File or array of images, whatever works for you.
If it's all within a current user session an anonymous user may work for you: https://parse.com/docs/ios/guide#users-anonymous-users

How to get the fileURL when an ACTIVE app is opened from other apps

I have already implemented DocumentTypes in the info.plist, and the applicationDidFinishedLaunchingWithOptions just works fine, because I can get the fileURL from the option dictionary.
My question is: when I opened my app and send it to background, and open other apps such as Safari and "Open in" a file, my app become active and is there anyway to get the fileURL from anyplace? I found that applicationDidBecomeActive has no userInfo.
You need to override application:handleOpenURL: or application:openURL:sourceApplication:annotation: in your UIApplicationDelegate to get this information.