How to add a attachment in outlook programatically swift 3 - ios

I have opened the outlook app and send a file in it. I am able to open the outlook and set the To,Subject and Body but not sure how to attach file in the document directory
The file is at path
var paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let fileName = "supportdata.log"
let logFilePath = (documentsDirectory as NSString).appendingPathComponent(fileName)
let scheme : String = "ms-outlook://compose?tosupport#tech.com&subject=Support data &body=Please find the attached file"
if let url = URL(string: scheme) {
UIApplication.shared.open(url, options: [:], completionHandler: {
(success) in
if (success)
{
print("Open \(scheme): \(success)")
}
})
}

Sadly, it is not possible. There are two main issues:
You are deeplinking to an app, meaning sending an on-device message to another app that is effectively the same as a normal URI with a GET method parameterized string. String only, no major data/files can be sent. The URL Scheme below is for reference. And it is the same as what you're using; though I think you are missing an '=' in your ms-outlook:// string.
ms-outlook://compose?to=%#&subject=%#&body=%#
iOS is pretty strict with app sandboxes, you cannot pass local files to other apps unless they are on the same app/file domain (e.g. you're the owner of both apps). There are a few alternatives that work in a watered-down format, but are not relevant for this scenario. Here's the thing, even if you could, Microsoft would need to support this file attachment feature, which it doesn't. Sadly, there's nothing we can do about it on our side... other than asking Microsoft to add the feature.
However, if you want to do this in the apple mail app, that's certainly doable.

Related

Share video to Facebook via Graph iOS SDK

I try to upload video to Facebook using Graph API.
But always see error message
(#100) No permission to publish the video.
Facebook app has Development mode. I created test user there. My role is admin. FB access token is valid.
Here my code here:
private func upload(url: URL) {
guard let data = try? Data(contentsOf: url) else { return }
let connection = GraphRequestConnection()
connection.delegate = self
var items: [String: Any] = [:]
items["title"] = "Test title from api"
items["\(url.absoluteString)"] = data
let keys = ["value" : "EVERYONE"]
items["privacy"] = keys
let request = GraphRequest(graphPath: "me/videos", parameters: items, httpMethod: .post)
connection.add(request) { reqconnection, anyInfo, error in
print(reqconnection)
print(anyInfo)
print(error)
}
connection.start()
}
I saw a lot of questions about it, but mostly they are too old and don't work for me.
Share video to FB
From thread FB Developers here - the same error, but they didn't find solution
Also check this docs - doesn't work
Also I'm a bit confused what docs are right for post video in Facebook.
Because in docs I see
The Video API allows you to publish Videos on Pages and Groups. Publishing on Users is not supported.
but also in another docs see Graph API with path me/videos. So it confused me a bit.
What docs are correct for my case:
here
here
From docs I see that
Your app should manage permissions as follows:
Graph API Requests - Before you send Graph API requests, you should check for necessary permissions and request them if needed.
Where can I find permission publish_actions?
Please help to figure out how to fix that.
I only can upload via ShareDialog. Like here and docs here

Link to iTunes Music in Swift

I like to forward app to iTunes or Apple Music (like done for Instagram or FV) when user clicks a button, below code gives found nil error. Is there a way to do that? Or even better play previews in my app.
var url = NSURL(string: "itms://itunes.apple.com/us/album/ne-olacak-dj-funky-c-vs-ogün-dalka-single/id1202943921")
if UIApplication.shared.canOpenURL(url! as URL) {
UIApplication.shared.openURL(url! as URL)
}
This method expects URLString to contain only characters that are allowed in a properly formed URL. All other characters must be properly percent escaped. Any percent-escaped characters are interpreted using UTF-8 encoding.
and you need to encode it before passing it to NSURL, you can do this via stringByAddingPercentEncodingWithAllowedCharacters
let url = URL(string: "itms://itunes.apple.com/us/album/ne-olacak-dj-funky-c-vs-ogün-dalka-single/id1202943921".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
if UIApplication.shared.canOpenURL(url! as URL) {
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
}
for that error you need to follow this
This is a new enforced security measure that apple has implemented on any app that is build in iOS 9.
The only solution so far is to add an entry in the info.plist file with the Key LSApplicationQueriesSchemes and add "itms" and any other url scheme that your app will be linking to in this array.

iMessaged-based invitations for GameCenter for iOS 10

I'm trying to update my app to work correctly with the new features of GameCenter in iOS10.
I create a new GKGameSession on device1, get a share URL, and all that works fine. I send the share URL out via a share sheet to device 2.
Device2 clicks the link, the device briefly displays 'Retrieving...' and then launches my app. Great! But, now what? Is there context information available for this URL that I can somehow access? Otherwise I have no way how to respond when the app is launched.
Previously you'd get a callback to something adhering to the GKLocalPlayerListener protocol, to the method player:didAcceptInvite:, and you could join the match that way. But with these iCloud-based messages, the player might not be even logged into GameCenter, right? This part seems to have been glossed over in the WWDC presentation.
Also, as of today (12/28/2016) there is no Apple documentation on these new methods.
Since the GKGameSessionEventListener callback session:didAddPlayer: only fires if the game is already running, to be sure you can process this callback every time requires a work around. I've tested this and it works.
When you send out an iMessage or email invite to the game, don't include the Game Session Invite URL directly in the message. Instead use a registered URL that will open your app when opened on a device on which your app is installed. Check here to see how:
Complete Tutorial on iOS Custom URL Schemes
But add a percent escaped encoding of the game invite URL as a parameter to this URL thusly (I'm assuming the registration of a url e.g. newGameRequest but it will be best to make this quite unique, or even better - though it requires more setup, try Universal Link Support as this will allow you to direct users who don't have your app installed to a webpage with a download link)
let openOverWordForPlayerChallenge = "newGameRequest://?token="
gameState.gameSession?.getShareURL { (url, error) in
guard error == nil else { return }
// No opponent so we need to issue an invite
let encodedChallengeURL = url!.absoluteString.addingPercentEncoding(withAllowedCharacters:.urlHostAllowed)
let nestedURLString = openOverWordForPlayerChallenge + encodedChallengeURL!
let nestedURL = URL(string: nestedURLString)!
}
send the URL in a message or email or WhatsApp or whatever. Then in your app delegate, add the following:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
var success = false
if let queryString = url.query {
if let urlStringToken = queryString.removingPercentEncoding {
let token = "token="
let startIndex = urlStringToken.startIndex
let stringRange = startIndex..<urlStringToken.index(startIndex, offsetBy: token.characters.count)
let urlString = urlStringToken.replacingOccurrences(of: token, with: "", options: .literal, range: stringRange)
if let url = URL(string: urlString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
success = true
}
}
}
}
return success
}
Now you can be sure the session:didAddPlayer: will be called. What's the betting this workarround is good for about 2 weeks, and they fix this in the next release of iOS showcased at WWDC 2017 ! Update: this problem hasn't been fixed - so the workaround above remains good!
I agree, the lack of documentation is frustrating. From what I can see, we have to:
add <GKGameSessionEventListener> protocol in the class' header
Then session:didAddPlayer: fires on the joining player's device after accepting an invite link.
update:
Unfortunately, I'm not surprised to hear your results. I hadn't tried all of those scenarios, but GKTurnBasedMatch had similar shortcomings. The way I got around it there was: I added a list of player statuses to match data (invited, active, quit, etc). I gave the player a view of "pending invitations." When they opened that view, I would load all of their matches and display the entries where the player was in invited state. With GKGameSession, that should work too.
Or, it might be easier if you could maintain a local list of sessions that you are aware of. Whenever the game becomes active, pull the entire list of sessions from the server and look for a new entry. The new entry would have to be the match the player just accepted by clicking the share URL.

Unable to share url on whatsapp for iPhone

I have the code below which i am using to share the url on whatsapp using whatsapp url scheme, but when i do this i see empty message on the whatsapp message screen.
let itunesLink = "http://google.com";
let text = itunesLink.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLPathAllowedCharacterSet());
let url = NSURL(string: "whatsapp://send?text=\(text!)")
UIApplication.sharedApplication().openURL(url!);
When this code runs, i see the contacts and after i select the whatsapp account option i dont see the url and see the empty screen.
I am always confused why there are so many allowed characters in these sets. I suspect you may need to add more percent-encoding. This is what I use to encode URLs embedded in URLs:
let set = NSCharacterSet(charactersInString: ".-0123456789#ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~")
let text = itunesLink.stringByAddingPercentEncodingWithAllowedCharacters(set)
Because "/" is an allowed character in a URL Path, URLPathAllowedCharacterSet includes "/", and the "/" in itunesLink will not be percent-encoded, using the original program. That might be confusing Whatsapp. It certainly confuses me.

POST Queries in Swift for given website

I am trying to make queries to get the fuel type and consumption of a specified car (the user enters both make and model) for an iOS app written in Swift.
The app is targeted for Spain, and I have found a website that allows the user to enter make and model, and it returns the details for that car (http://coches.idae.es/portal/BaseDatos/MarcaModelo.aspx). I have seen using the tool WireShark, that the query is based on POST instead of GET. But I am not quite sure how I can make the requests within the app I am developing, or how to handle the info that is sent to me back from the sender.
Is there any way to make those requests to the given website? If so, I would really appreciate some help on the subject, I am new in iOS development and am looking forward to learning as much as possible.
Thanks :)
Many people prefer to use AFNetworking for making HTTP requests. However you don't need to do that. You said that its a POST request. Setting that up is easy even without AFNetworking using NSMutableURLRequest. I'm assuming you have a link to the API and not just to the aspx page. My Spanish is pretty weak so I can't look up the API reference for you but here's how you can make the request and receive data from the server. You will have to put the correct values and parse the responses:
let request = NSMutableURLRequest(URL: NSURL(string: "/* Paste URL here */")!)
request.HTTPMethod = "POST"
// Do this as many times are required for filling in the headers.
request.addValue("/* The value for the HTTP header */", forHTTPHeaderField: "/*The header field like Accept-Type, etc..*/")
// If you need an HTTP body too then make the JSONObj as a dictionary or array or whatever and then
let data = NSJSONSerialization.dataWithJSONObject(JSONObj, options: [])
request.HTTPBody = data // This needs to be NSData.
// Now make the request.
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request, { (data, response, error) -> Void in
if error == nil
{
assert(data != nil)
let JSON = NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [NSObject: AnyObject]
// If you are using swift 2 this needs to be in a do try catch statement.
// TODO: Use JSON for whatever.
}
else
{
print(error!.localizedDescription)
}
}
task?.resume()
Let me know if you have any other questions or if the API doesn't use JSON or is completely different.

Resources