Link to iTunes Music in Swift - ios

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.

Related

run iOS app from web (angular)

I'm developing an iOS app
i have a payment page designed by angular
user click on a payment button in ios app and i run a url page with few paramaters :
UIApplication.sharedApplication().openURL(NSURL(string:"http://www.testt.com/price/personId/packageName")!)
price is money user has entered in textfield and package-name id the schema name i should send it to web page that runs my app (return to app with running that string) i have declared in info.plist
then after been successful or unsuccessful payment. it should return to app by clicking on “return to app” button on web site.
actually angular runs the packageName i have sent with url like this way : http://packageName://
i tried to implement this by universal link like this way : packageName:// but wont open this link because of special chars in url.i used encoding method to encode chars but not successful because url removes the chars :// then i tried app site association method which i faced cannot parse app site association file
so i have few question for you :
1_is there any trick to run url with special chars ??
2_what would you do if you were me ??
3_i tried apple-app-site-association too but can not parse error which i have a question about this method how could this file opens my app? this way : applink:http://msite.com ?? because it contains spacial chars in it again
excuse my awful English at the end
talk to me before voting down
update :
var encodedChars="openMyApp" //schema name
encodedChars=encodedChars.addingPercentEncoding(withAllowedCharacters:CharacterSet.alphanumerics)!
let url="http://test.com/#/payment/\(id)/\(price!)/\(encodedChars)"
UIApplication.shared.openURL(NSURL(string: url)! as URL)
angular code :
if (this.accounting.packageName === 'openMyApp') {
this.url = this.accounting.packageName + '://';
} else {
this.url = 'http://' + this.accounting.packageName;
}
<a class="btn btn-default" title="" href="{{url}}"></a>
I have implemented custom url scheme for my app. If i write some thing like this
myappName://
in safari and press enter, my app is opened.
My angular + backend developer is using this line of code to open my app using custom url scheme. he is sending parameters along the scheme which i can read
this.document.location.href = 'myappname://appname.com/login?name='+id+'&id='+token;
Follow these 2 simple steps, i hope this will help you.
1.How to implement custom url schemes
Official docs
Helpful link
2.How to handle custom URL scheme with params in app delegate
handle the custom url and read params in appDelegate in this function
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if let query = url.query {
//that's my logic. yours url may be different
let components = query.components(separatedBy: "=")
print(components)
}
}
let me know if you need any help

How to add a attachment in outlook programatically swift 3

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.

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.

I am getting URL schemes error while archiving my iOS app

This is the error -
The following URL schemes found in your app are not in the correct formats.Please see RFC1738 for more detail.
This is my code -
#IBAction func webdropbox1(_ sender: Any) {
let safariVC = SFSafariViewController(url: NSURL(string: "www.dropbox.com")! as URL)
self.present(safariVC, animated: true, completion: nil)
safariVC.delegate = self
}
Also I have added the URL to my info.plist file -
Info.plist file screenshot:
A URL scheme is the part before the colon: http, https, ftp, mailto, file, etc.
www.dropbox.com is not a URL scheme. It's not even a valid URL.
You have two issues.
Your Info.plist needs to be updated to specify actually valid schemes, not a partial URL.
Your code to create the Dropbox URL needs to use "https://www.dropbox.com".

LinkedIn open url company in iOS (URL Scheme)

I need to open in the LinkedIn app a company url from my iOS app in Swift. I have tried to do that with URL Scheme, but I haven't achieved it.
This code open the LinkedIn app, but not in the url I need:
UIApplication.sharedApplication().openURL(NSURL(string: "linkedin://profile/xxx_id_company_xxx")!)
Can anyone help me?
Swift 4
In your info.plist under LSApplicationQueriesSchemes add
<string>linkedin</string>, then in your code just add
let webURL = URL(string: "https://www.linkedin.com/in/yourName-yourLastName-yourID/")!
let appURL = URL(string: "linkedin://profile/yourName-yourLastName-yourID")!
if UIApplication.shared.canOpenURL(appURL) {
UIApplication.shared.open(appURL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.open(webURL, options: [:], completionHandler: nil)
}
It seems the correct format is "linkedin://companyName/companyId"
See this SO question: How can open LinkedIn Comapny Url in iPhone app programmatically?
This linkedin company url scheme is working for me
linkedin://company?id={company-id}
This objective c code trys to open linkedin company page in linkedin app and fall backs to a modal webview presenting company web page.
/* try to open linkedin app with company url */
if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"linkedin://company?id=2589010"]]) {
/* Unable to open linked app, present web page with url #"https://www.linkedin.com/company/sesli-sozluk" */
...
}
You can find your "company id" in the source code of your company page on linkedin. Search for "company-id" or "companyId", it is in a hidden input field.

Resources