I have a Share-Extension from which I would like to redirect the user to the main app. This is what I tried:
let signInAction = UIAlertAction(title: "Anmelden", style: .default) { (alert) in
let myAppUrl = NSURL(string: "open://")!
self.extensionContext?.open(myAppUrl as URL, completionHandler: { (success) in
if (!success) {
// let the user know it failed
print("fail!")
}
})
}
And my info-plist :
Right now it is printing the "fail!". What am I doing wrong here?
Have a look at your project settings in xcode.
Did you set a custom url scheme in the "URL-Types" setting in your apps target?
Xcode - Target - General Settings - URL Types
Related
I'm trying to show my data in my app in the Files app on my iPhone
I searched a lot and did everything right, I don't know where the error is
func fileManger(nameFolder: String) {
let manager = FileManager.default
let DecomentFolder = manager.urls(for: .documentDirectory, in: .userDomainMask).last
let Folder = DecomentFolder?.appendingPathComponent("\(nameFolder)")
do {
try manager.createDirectory(at: Folder!, withIntermediateDirectories: true, attributes: [:])
} catch let error {
print(error.localizedDescription)
}
}
Also here I am sending the value to be a folder
#objc func alertNewFolder () {
let alert = UIAlertController(title: "Create an album", message: "", preferredStyle: .alert)
alert.addTextField(configurationHandler: nil)
alert.textFields![0].placeholder = "name album"
alert.textFields![0].textAlignment = .right
alert.addAction(UIAlertAction(title: "cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: "save", style: .default, handler: { ـ in
if let textFileds = alert.textFields {
let links = textFileds[0].text
self.arrS1.append(addCatogrey(nameCatog: links!, imageSection: UIImage(named: "folder")))
// Here Send FileManager
helperCoding().fileManger(nameFolder: links!)
self.collection.reloadData()
}
}))
self.present(alert, animated: true, completion: nil)
}
In the Simulators it saved in the Documents folder here correctly
/Users/badrshammry/Library/Developer/CoreSimulator/Devices/7986A27F-7026-45E1-9073-78CCD6A9B90A/data/Containers/Data/Application/3173C4DC-BCDE-41B9-89E1-6E8D9B52EF25/Documents
If you would like to expose your App document files inside Apple's Files App you need to include the "Supports Document Browser" key in your info plist file and set it to YES:
Add these two keys to 'info.plist' -
...
<key>UIFileSharingEnabled</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
...
After this, files saved in '.documentDirectory' will appear in 'Files App' inside a folder with your app name.
If editing 'info.plist' in Xcode then add below keys -
Application supports iTunes file sharing = YES
Supports opening documents in place = YES
info.plist
To set Supports Document Browser has to be set in Custom iOS Target Propertiesin the info tab
info.plist is no more from Xcode 13
Anyhow, it does not work. The files App will not find files in the your app's bundle. They need to be "shared" (In SwiftUI use UIViewControllerRepresentable)
But your App will not have a directory in the file structure visible to the Files App. Adding that folder is still a mystery
See: How can folders visible to FIles app be created by App programatically
I'm trying to show my data in my app in the Files app on my iPhone
I searched a lot and did everything right, I don't know where the error is
func fileManger(nameFolder: String) {
let manager = FileManager.default
let DecomentFolder = manager.urls(for: .documentDirectory, in: .userDomainMask).last
let Folder = DecomentFolder?.appendingPathComponent("\(nameFolder)")
do {
try manager.createDirectory(at: Folder!, withIntermediateDirectories: true, attributes: [:])
} catch let error {
print(error.localizedDescription)
}
}
Also here I am sending the value to be a folder
#objc func alertNewFolder () {
let alert = UIAlertController(title: "Create an album", message: "", preferredStyle: .alert)
alert.addTextField(configurationHandler: nil)
alert.textFields![0].placeholder = "name album"
alert.textFields![0].textAlignment = .right
alert.addAction(UIAlertAction(title: "cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: "save", style: .default, handler: { ـ in
if let textFileds = alert.textFields {
let links = textFileds[0].text
self.arrS1.append(addCatogrey(nameCatog: links!, imageSection: UIImage(named: "folder")))
// Here Send FileManager
helperCoding().fileManger(nameFolder: links!)
self.collection.reloadData()
}
}))
self.present(alert, animated: true, completion: nil)
}
In the Simulators it saved in the Documents folder here correctly
/Users/badrshammry/Library/Developer/CoreSimulator/Devices/7986A27F-7026-45E1-9073-78CCD6A9B90A/data/Containers/Data/Application/3173C4DC-BCDE-41B9-89E1-6E8D9B52EF25/Documents
If you would like to expose your App document files inside Apple's Files App you need to include the "Supports Document Browser" key in your info plist file and set it to YES:
Add these two keys to 'info.plist' -
...
<key>UIFileSharingEnabled</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
...
After this, files saved in '.documentDirectory' will appear in 'Files App' inside a folder with your app name.
If editing 'info.plist' in Xcode then add below keys -
Application supports iTunes file sharing = YES
Supports opening documents in place = YES
info.plist
To set Supports Document Browser has to be set in Custom iOS Target Propertiesin the info tab
info.plist is no more from Xcode 13
Anyhow, it does not work. The files App will not find files in the your app's bundle. They need to be "shared" (In SwiftUI use UIViewControllerRepresentable)
But your App will not have a directory in the file structure visible to the Files App. Adding that folder is still a mystery
See: How can folders visible to FIles app be created by App programatically
We would really appreciate any help on the following. Through our app, the user can initiate a WhatsApp message (what happens is that the WhatsApp client starts with the phone + text preloaded, so the user just needs to tap the "send" button from the WhatsApp application).
We have an Android and an iOS app. In Android we are using the following code to select between WhatsApp and WhatsApp Business.
String url = "https://api.whatsapp.com/send?phone=" + phoneNumberToUse + "&text=" +
URLEncoder.encode(messageText, "UTF-8");
if(useWhatsAppBusiness){
intent.setPackage("com.whatsapp.w4b");
} else {
intent.setPackage("com.whatsapp");
}
URLEncoder.encode(messageText, "UTF-8");
intent.setPackage("com.whatsapp");
intent.setData(Uri.parse(url));
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent);
} else {
Toast.makeText(this, "WhatsApp application not found", Toast.LENGTH_SHORT).show();
}
We are trying to achieve the same functionality on Swift for iOS, however, we did not find any way to programmatically define whether the OS should start WhatsApp or WhatsApp Business. The code listed below, always starts the one or other depending on which is installed. If both are installed, it starts the WhatsApp application.
let whatsApp = "https://wa.me/\(phoneNumber)/?text=\(shareableMessageText)"
guard let url = URL(string: whatsApp) else {
return
}
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: convertToUIApplicationOpenExternalURLOptionsKeyDictionary([:]), completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
So in simple words, is there any way, from our app, to select which WhatsApp application (WhatsApp or WhatsApp Business) is going to be launched?
Thanks
I have made some apps with WhatsApp but I had to use the web platform, not the business app.
You can check what app is installed in the device like this:
let app = UIApplication.shared
let appScheme = "App1://app"
if app.canOpenURL(URL(string: appScheme)!) {
print("App is install and can be opened")
} else {
print("App in not installed. Go to AppStore")
}
The 'App1' value must be changed for the app you want to check. WhatsApp App should use 'WhatsApp', and WhatsApp Business should use 'WhatsApp-Business'.
After that you can call the URL for each app, I mean, for WhatsApp you can use the URL with this format:
let whatsApp = "https://wa.me/\(phoneNumber)/?text=\(shareableMessageText)"
And for WhatsApp Business you have to use this format:
let whatsApp = "https://api.whatsapp.com/send?phone=\(phoneNumber)&text=\(shareableMessageText)"
It is possible too, that the first step was not necessary to do. Because of the call is made with the api, the device should open the Business app, and if it is made with the wa.me scheme, the device should open the WhatsApp as normal.
I am going to check my app to see if it is working or not.
UPDATE:
I have installed WhatsApp Business and I have made some test, with two different url calls.
The code I use is this:
let phoneNumber = "my_phone_number"
let shareableMessageText = "This_is_a_test_message"
let whatsApp = "https://wa.me/\(phoneNumber)/?text=\(shareableMessageText)"
if let urlString = whatsApp.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
if let whatsappURL = NSURL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL as URL) {
UIApplication.shared.openURL(whatsappURL as URL)
} else {
print("error")
}
}
}
Using this code you will see a prompt with a message giving you the option of send a message in WhatsApp or WhatsApp Business.
But if you use this other code:
let phoneNumber = "my_phone_number"
let shareableMessageText = "This_is_a_test_message"
let whatsApp = "whatsapp://send?phone=\(phoneNumber)&text=\(shareableMessageText)"
if let urlString = whatsApp.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
if let whatsappURL = NSURL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL as URL) {
UIApplication.shared.openURL(whatsappURL as URL)
} else {
print("error")
}
}
}
You will see a prompt asking you to open WhatsApp business. So the way to choose between WhatsApp and WhatsApp Business is the URL format. If you choose this format you will be ask to choose between one or another WA version:
let whatsApp = "https://wa.me/\(phoneNumber)/?text=\(shareableMessageText)"
But if you use this URL format, you will use WA Business directly:
let whatsApp = "whatsapp://send?phone=\(phoneNumber)&text=\(shareableMessageText)"
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.
I wanna go to settings if the user clicks on "go to settings", can someone please help?
heres what the notification looks like
Try this:
if let appSettings = NSURL(string: UIApplicationOpenSettingsURLString) {
UIApplication.sharedApplication().openURL(appSettings)
}