Can't open a link when the default browser has been changed - swift

After updating to iOS 14 I cannot open urls in the browser if it has been changed from default Safari to Chrome.
I did some investigation and found that UIApplication.shared.canOpenURL(url) always returns false.
Please help.

For iOS 14+
I suggest to add this to your Info.plist
<key>LSApplicationQueriesSchemes</key>
<array>
<string>https</string>
</array>
By adding this you can continue to use method canOpenURL(_ url: URL) -> Bool
guard let url = URL(string: "https://example.com") else { return }
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
}

Related

Files backed up to icloud in swift are changed when the app is deleted

Added backup and restore functions to the app
And it works fine until I delete the app
But if you delete the app and reinstall it, the file name you backed up is changed
The backup.db file is changed to .backupp.db.icloud when you reinstall the app after deleting it
And after the backup file, which was normal before deleting the app, is changed to .icloud, it seems that the file is not normal.
Below is the Info.plist and the source code
What's the problem?
<key>NSUbiquitousContainers</key>
<dict>
<key>iCloud.com.sinwho.CheckDay</key>
<dict>
<key>NSUbiquitousContainerIsDocumentScopePublic</key>
<true/>
<key>NSUbiquitousContainerName</key>
<string>CheckDay</string>
<key>NSUbiquitousContainerSupportedFolderLevels</key>
<string>Any</string>
</dict>
</dict>
func copyiCloudToLocal() throws -> Bool {
var result:Bool = false
let dbFileURL = CloudDataManager.DocumentsDirectory.dbFileURL.appendingPathComponent(Define.DB_NAME)
guard let containerURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents") else
{
return result
}
let iCloudFileURL = containerURL.appendingPathComponent(Define.DB_NAME)
if !FileManager.default.fileExists(atPath: iCloudFileURL.path)
{
return false
}
if FileManager.default.fileExists(atPath: dbFileURL.path) {
try FileManager.default.removeItem(at: dbFileURL)
try FileManager.default.copyItem(at: iCloudFileURL, to: dbFileURL)
result = true
} else {
try FileManager.default.copyItem(at: iCloudFileURL, to: dbFileURL)
result = true
}
return result
}

URL not being passed to appdelegate

I can't get dynamic links or universal links working. My app won't read in a URL. I've started with a fresh project, added a URL Scheme of com.company.AppLoginView and the below code into AppDelegate. I added a url in notes of: com.company.AppLoginView://Register?User=Name. Clicking the link requests to open the app which then opens. However, nothing seems to be passed to the app and the below isn't being called. What am I doing wrong? This is a completely empty app and should work.
func application(_ app: UIApplication, open url: URL,
options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
if let scheme = url.scheme,
scheme.localizedCaseInsensitiveCompare("com.myApp") == .orderedSame,
let view = url.host {
var parameters: [String: String] = [:]
URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems?.forEach {
parameters[$0.name] = $0.value
}
print("View:\(view) Params:\(parameters) Scheme:\(scheme)")
// redirect(to: view, with: parameters)
}
return true
}
This is because target is iOS 13 and SceneDelegate takes over some functions from AppDelegate. In particular, open:url is replaced by SceneDelegate scene:openURLContexts.
More here: Apple openURLContexts
3 days of my life I won't get back!
You should add ULR scheme and URL identifier to plist. You can use this:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>com.company.AppLoginView</string>
<key>CFBundleURLSchemes</key>
<array>
<string>com.company.AppLoginView</string>
</array>
</dict>
</array>
and check it with print in method in AppDelegate
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
print(url)
return true
}
open Safari and type your address
com.company.AppLoginView://Register?User=Name

iOS Today Widget Universal Links From Lock Screen and Notification Center

We have a today widget to deep link into the app. The deep links work just fine when the user accesses the widget from the home screen. However, when a user accesses the widget when the device is locked, or when the user slides down from the top of the screen, the links open in Safari.
I was wondering if anyone else has come across this issue, and if so, how they solved it.
Here was the solution we came upon (Swift 4.1). We needed to support a custom URL scheme to tell iOS that we can open links from the today widget. This uses a different UIApplication delegate function. Along with implementing func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: #escaping ([Any]?) -> Void) -> Bool, we also need to implement func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool
First, in Info.plist, we have our supported schemes under CFBUndleURLTypes.
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>todayWidgetScheme</string>
</array>
</dict>
</array>
Then, also in Info.plist, we also listed the scheme under LSApplicationQueriesSchemes.
<key>LSApplicationQueriesSchemes</key>
<array>
<string>todayWidgetScheme</string>
</array>
Next, when opening the link from the today widget, set the url scheme to the iOS recognized todayWidgetScheme.
func openAppFromTodayWidget() {
if let url = URL(string: "https://url.com") {
var components = URLComponents(url: url, resolvingAgainstBaseURL: true)
components?.scheme = "todayWidgetScheme"
if let todayWidgetUrl = components?.url {
extensionContext?.open(todayWidgetUrl)
}
}
}
Finally, in AppDelegate.swift, when iOS asks the application to handle the universal link, set the original url scheme
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if url.scheme == "todayWidgetScheme" {
var components = URLComponents(url: url, resolvingAgainstBaseURL: true)
components?.scheme = "https"
if let todayWidgetUrl = components?.url {
// do your thing
return true
}
}
return false
}

Settings button in swift 3, Xcode

The problem I have is that I want to have a settings button which when the user clicks it, takes them to a specified path in the settings such as sound settings. The only issue is that every method I've tried has been outdated for more recent versions of iOS 10. Does anyone have any suggestions for a method which I can use? Any help will be greatly appreciated.
You can open your app Settings page by...
let settings_app: URL = URL(string: UIApplicationOpenSettingsURLString)!
UIApplication.shared.open(settings_app)
But you can't open other Settings app section from any app.
Try the following code, I hope to help you
let settingsUrl = NSURL(string:UIApplicationOpenSettingsURLString) as! URL
if #available(iOS 10.0, *) {
UIApplication.shared.open(settingsUrl, options: [:], completionHandler: nil)
} else {
// Fallback on earlier versions
}

launch Youtube channel on your App Swift Code

I have spent some days to find the Swift code for youtube Channel to open from my app. but i couldn't find At all, can some one help me please!!
i need the Code in Swift.
Update for Swift 3 and iOS 10+
OK, here is how to do it in Swift 3. Basically, there are two easy steps to achieve this:
First, you have to modify Info.plist to list Youtube with LSApplicationQueriesSchemes. Simply open Info.plist as a Source Code, and paste this:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>youtube</string>
</array>
After that, you can open any youtube URL inside Youtube application by simply substituting https:// with youtube://. Here is a complete code, you can link this code to any button you have as an Action:
#IBAction func YoutubeAction() {
let YoutubeUser = "Your Username"
let appURL = NSURL(string: "youtube://www.youtube.com/user/\(YoutubeUser)")!
let webURL = NSURL(string: "https://www.youtube.com/user/\(YoutubeUser)")!
let application = UIApplication.shared
if application.canOpenURL(appURL as URL) {
application.open(appURL as URL)
} else {
// if Youtube app is not installed, open URL inside Safari
application.open(webURL as URL)
}
}
I hope this suits your purpose:
var url = NSURL(string: "https://youtube.com/your-channel")
if UIApplication.sharedApplication().canOpenURL(url!) == true {
UIApplication.sharedApplication().openURL(url!)
}