Understanding simple code in swift [closed] - ios

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I found an answer to implement a link to get rated on the app store (App store link for "rate/review this app"). Answer is:
let appID = "Your App ID on App Store"
let urlStr = "itms-apps://itunes.apple.com/app/id\(appID)" //
(Option 1) Open App Page
let urlStr = "itms-apps://itunes.apple.com/app/viewContentsUserReviews?
id=\(appID)" // (Option 2) Open App Review Tab
if let url = URL(string: urlStr), UIApplication.shared.canOpenURL(url)
{
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler:
nil)
} else {
UIApplication.shared.openURL(url)
}
}
I am having trouble understanding how to implement this. Particularly I don't understand how the if statement will take the user to the app store.
Thanks.

The first part
let appID = "Your App ID on App Store"
let urlStr = "itms-apps://itunes.apple.com/app/id\(appID)" //
(Option 1) Open App Page
let urlStr = "itms-apps://itunes.apple.com/app/viewContentsUserReviews?
id=\(appID)" // (Option 2) Open App Review Tab
gets the url of your app in itunes review section so user can write a review
The second part
if let url = URL(string: urlStr), UIApplication.shared.canOpenURL(url)
checks if the url is valid and can be opened by application.shared if ok it's check the needed function according to the current version.
Actually system know that the app needs to open the url , when it executes the statement , look at open
UIApplication.shared.open(url, options: [:], completionHandler:nil)
Or
UIApplication.shared.openURL(url)

you are opening url with itms-apps:// scheme. So system itself knows what app should handle this.

Related

Share a pdf file to WhatsApp directly, without using a UIActivityViewController [duplicate]

This question already has answers here:
Share PDF through WhatsApp
(2 answers)
Closed 1 year ago.
I am using below code to send a text to a new WhatsApp number using a WhatsApp Url scheme instead of UIActivityViewController.
let urlWhats = "whatsapp://send?phone=+9197******2&text=Hi Hello"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters:
CharacterSet.urlQueryAllowed){
if let whatsappURL = URL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL){
if #available(iOS 10.0, *) {
UIApplication.shared.open(whatsappURL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(whatsappURL)
}
} else {
print("Install Whatsapp")
}
My current requirement is to share a Pdf file to a new WhatsApp number using WhatsApp Url scheme. Can anyone help me with this..
You can not pass the pdf file via url, it's make nonsense.
There are another way you can do is custom the UIActivityViewController.
You can refer this tutorial for UIActivityViewController customization: https://nshipster.com/uiactivityviewcontroller/

How to open a facebook\instagram page \ profile from your app? (swift) [duplicate]

This question already has answers here:
Facebook not open page in app swift 3
(2 answers)
Closed 3 years ago.
I'm trying to open a facebook page from my app. Here is the code:
let facebookWebURL = URL.init(string: "https://www.facebook.com/aviramnet")
let facebookAppURL = URL.init(string: "fb://profile/220780122049926")
if (UIApplication.shared.canOpenURL(facebookAppURL!)){
UIApplication.shared.open(facebookAppURL!, options: [:], completionHandler: nil)
}else{
UIApplication.shared.open(facebookWebURL!, options: [:], completionHandler: nil)
}
But it only opens the app on Safari browser. Did I miss something?
And yes, I have the Facebook app installed.
you need to add in the app's info.plist :
under the LSApplicationQueriesSchemes array add an item:
fb for facebook, and instagram for instagram :

How can I open Apple Reminders app programmatically?

Is there a way to open the Apple Reminders app programmatically?
I have tried in Swift 5+ and iOS 13, you need to use the following url scheme.(Earlier x-apple-reminder:// was working but now it isn't)
x-apple-reminderkit://
if let url = URL(string: "x-apple-reminderkit://"), UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:]) { (isDone) in
}
}
It will open the iOS Reminder App from your application.
Hope it may be helpful to other.
Try This
let url = URL(string: "x-apple-reminder://")
UIApplication.shared.open(url!, options: [:]) { (finish) in
}
Reference for all apple apps scheme :
https://ios.gadgethacks.com/news/always-updated-list-ios-app-url-scheme-names-0184033/
and if you use x-apple-reminderkit://today, you can open the Today "view" in Reminders.
Still trying to figure out how to open a specific list or the Flagged view, though.

How to check app is installed or not in phone

I don't know how to check if app is installed or not on phone! Or when App is installed, open the app, otherwise open the Appstore link to download the app. I'm using swift 3. I want to do it using app name or bundle identifier.
Thank You!
func openApp(appName:String) {
let appName = "instagram"
let appScheme = "\(appName)://app"
let appUrl = URL(string: appScheme)
if UIApplication.shared.canOpenURL(appUrl! as URL) {
UIApplication.shared.open(appUrl!)
} else {
print("App not installed")
}
}
After looking into so many answers, i am writing a common code which will help for new users. If you have two mobile apps as App1 and App2, if you want to check in App2 that App1 is already installed in his device or not, here is code below.
In App1 add this property in info.plist file
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.companyName.App1</string>
<key>CFBundleURLSchemes</key>
<array>
<string>App1</string>
</array>
</dict>
</array>
In App2 add this property in info.plist file
<key>LSApplicationQueriesSchemes</key>
<array>
<string>App1</string>
</array>
In App2 write the method to check if app is installed or not on phone! Or when App is installed, open the app, otherwise open the Appstore link to download the app as below.
func checkAndOpenApp(){
let app = UIApplication.shared
let appScheme = "App1://app"
if app.canOpenURL(URL(string: appScheme)!) {
print("App is install and can be opened")
let url = URL(string:appScheme)!
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
} else {
print("App in not installed. Go to AppStore")
if let url = URL(string: "https://apps.apple.com/us/app/App1/id1445847940?ls=1"),
UIApplication.shared.canOpenURL(url)
{
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
}
}
I hope it will help some one.
Between the other answers and their comments, I'm getting the impression that the asker wants to be able to see if any given app is installed.
Beginning with iOS 9.0, that is not possible.
Apps for iOS 9 and later must have a list of requested URL schemes in the Info.plist before being allowed to use canOpenURL:. This is to protect user privacy, as advertisers were abusing this functionality in an arguably invasive fashion. (See this excellent post for more details on those changes.)
Of course, that list is static and cannot be changed after build time or submission to the App Store. If Apple doesn't like the ones you chose, they have every right to reject it.
I'm afraid that what you're asking isn't possible within reason for iOS 9.0 and later.
Edit: I also want to make clear that an app's URL scheme may not necessarily match nicely with its name. (This is more of an issue of a badly named constant than a functional issue.) There used to be a giant list of known URI schemes with documentation for each, but poignantly and fittingly enough, it seems that the wiki hosting it has shut down.
Swift 4.1. One developer can have more than one app on AppStore. So, I need to check if user has installed other apps or not by the same developer. I had Bundle ID's of other apps. Although you can use Appname instead of Bundle Id. So I followed the following steps.
In your current app add LSApplicationQueriesSchemes key of type Array in your info.plist file. Make entry of bundle id or Appname of app there which you want to open from your app.
Other app should have their bundle id or Appname entry in that app URL Scheme.
In your current app check if that app in installed in iPhone or not and can open accordingly.
let app = UIApplication.shared
let bundleID = "some.Bundle.Id://"
if app.canOpenURL(URL(string: bundleID)!) {
print("App is install and can be opened")
let url = URL(string:bundleID)!
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
} else {
print("App in not installed. Go to AppStore")
}
You can also test it from Safari browser. Just type the following in search bar
URL_scheme:// or Bundle_Id://
If app is installed the it will show alert with Appname to open it in app.
This worked for me (Swift 3.0)
Below two inputs should be provided:
<APP URL SCEHME>: The URL Scheme of the app which you want to open
<YOUR APP URL>: The App Itunes URL
func openApp() {
let appURL = NSURL(string: "<APP URL SCHEME>")
if UIApplication.shared.canOpenURL(appURL as! URL) {
print("Opening App...")
}else {
UIApplication.shared.openURL(NSURL(string: "<YOUR APP URL>")! as URL)
}
}
first go to info.plist, add LSApplicationQueriesSchemes add an item and place instagram on that item. Now this code will run perfectly.
let appName = "instagram"
let appScheme = "\(appName)://"
let appUrl = URL(string: appScheme)
if UIApplication.shared.canOpenURL(appUrl! as URL)
{
UIApplication.shared.open(appUrl!)
} else {
print("App not installed")
}

URL scheme "prefs:root=PASS" iOS10

I know there are lot of topics about this problem but is there a solution? I have not found official documentation for that.
My problem is I need to redirect my code on storage settings like this : (work in iOS 9)
let settingsUrl = NSURL(string: "prefs:root=CASTLE&path=STORAGE_AND_BACKUP")
if let url = settingsUrl {
UIApplication.shared.openURL(url as URL)
}
But since ios10 this method don't work, so Is there an alternative? I saw SnapChat, Google Maps redirect their apps to different part on settings (not the main screen of settings) so I think there is a solution.
I already implement the url scheme in info.plist but it's still not work
I tried this method but same issue too
let settingsUrl = NSURL(string: "prefs:root=CASTLE&path=STORAGE_AND_BACKUP")
if let url = settingsUrl {
if #available(iOS 10.0, *) {
UIApplication.shared.open(URL.init(string:"Prefs:root=General&path=STORAGE_ICLOUD_USAGE/DEVICE_STORAGE")!, options: [UIApplicationOpenURLOptionUniversalLinksOnly:true], completionHandler:{(success: Bool?) -> Void in})
} else {
UIApplication.shared.openURL(url as URL)
}
}
Thank in advance.
Since iOS 10, it's not possible to open the Settings app from a third party app. The only settings that are allowed to be opened are Keyboard setting but only by a custom keyboard extension and your own application settings.
More details: here
Note: Even for iOS 9, using the URL string that is mentioned in the question can lead to app rejection as it violates iOS App Review Guidelines.

Resources