4

I recently upgraded to Xcode 8 and converted my code to Swift 3. I am making a custom keyboard (extension) which worked perfectly fine till iOS 9, but i am facing a couple of issues in iOS 10.

  1. The container app of the custom keyboard contains a button which directs the user to the keyboard settings to add the keyboard

Issue: This button click is not working in iOS 10 i.e the user is not directed to the settings. I have configured the URL Schemes in my project and have tried the following code :

@IBAction func btnGetStarted(_ sender: AnyObject) {

    let settingsUrl = URL(string: UIApplicationOpenSettingsURLString)
    if let url = settingsUrl {
        UIApplication.shared.openURL(url)
    }
}

Also tried :

@IBAction func btnGetStarted(_ sender: AnyObject) {

if let settingsURL = URL(string:"prefs:root=General&path=Keyboard/KEYBOARDS") {
 UIApplication.shared.openURL(settingsURL)
 }
}
  1. The custom keyboard also contains emoji images. The user requires to enable "Allow Access" in the settings to use the emoji images. If the user does not enable "Allow access" then he cannot use the emoji images. If the "Allow access" is not enable and the user tries to click the emoji a toast pops up which tells the user to go to settings and enable "Allow access".

Issue: This toast does not pop up when the app is run in iOS 10

Code of the toast:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){

    let pbWrapped: UIPasteboard? = UIPasteboard.general

    if let pb = pbWrapped {
        if  currentKeyboard == XXXXXX.emoji {
            if let data = UIImagePNGRepresentation(dataEmoji[(indexPath as NSIndexPath).row]) {
                pb.setData(data, forPasteboardType: "public.png")

                  self.makeToast(pasteMessage, duration: 3.0, position: .center)
            }
        }

    } else {
        var style = ToastStyle()
        style.messageColor = UIColor.red
        style.messageAlignment = .center
        //style.backgroundColor = UIColor.whiteColor()

        self.makeToast("To really enjoy the keyboard, please Allow Full Access in the settings application.", duration: 8.0, position: .center, title: nil, image: UIImage(named: "toast.png"), style: style, completion: nil)
    }
}

I did check out few solutions on stackoverflow but none of them worked for me, as I said before my app works perfectly fine on all versions except iOS 10. Please can someone help me out?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Khadija Daruwala
  • 1,185
  • 3
  • 25
  • 54

6 Answers6

5

Swift 3 iOS 10

let settingsUrl = NSURL(string:UIApplicationOpenSettingsURLString) as! URL
UIApplication.shared.open(settingsUrl, options: [:], completionHandler: nil)
Uma Madhavi
  • 4,851
  • 5
  • 38
  • 73
Włodzimierz Woźniak
  • 3,106
  • 1
  • 26
  • 23
3

@persianBlue: This is working on Xcode8 + iOS10.

UIApplication.shared.openURL(URL(string:UIApplicationOpenSettingsURLString)!)
RJ168
  • 1,006
  • 2
  • 12
  • 22
  • 1
    I have already tried this. This directs the app to the phone settings app but its only a white screen and before the settings options are displayed the app closes – Khadija Daruwala Sep 29 '16 at 06:32
  • Looks like some people are not affected by the crash. I'm facing the same crash from the day I updated to iOS 10. Even updating to iOS 10.0.2 didn't resolve it. – Mesbah Oct 02 '16 at 12:49
  • Just for reference the crash log is here: http://pastebin.com/L9zQ7kmZ. Hopefully someone will be able to extract what's actually happening. – Mesbah Oct 02 '16 at 13:13
  • openURL(url: URL) is deprecated in Swift 3. You'll need to use open(url:, options, completionHandler:) – Peter Johnson Nov 07 '16 at 00:58
  • @PeterJohnson It was deprecated in iOS 10, but if you're targeting anything less than iOS 10, you'll need to continue using `openUrl` – mylogon Dec 20 '16 at 11:56
1

UIApplicationOpenSettingsURLString provides a link to the app's settings. Previous to iOS10, if the app lacked a settings.Bundle, it would link to the settings home page. The logs would show:

_BSMachError: (os/kern) invalid capability (20) _BSMachError: (os/kern) invalid name (15)

If your intention is to link to the app's settings, you simply need to add a settings bundle. Apple Documentation

I have yet to find a way to link to the phone's setting's Home page.

mrmomoko
  • 11
  • 1
1

This is no possible in iOS11 anymore, we can just open Settings like:

if let url = URL(string:UIApplicationOpenSettingsURLString), UIApplication.shared.canOpenURL(url) {
   //iOS 10 +
   UIApplication.shared.open(url, options: [:], completionHandler:{ didOpen in
      print("Opened \(didOpen)") 
   })
   //iOS 9 +
   UIApplication.shared.open(url)
}
Hobbes the Tige
  • 3,753
  • 2
  • 22
  • 21
chawki
  • 867
  • 1
  • 8
  • 13
0

If you see a white screen just like @PersianBlue your app might be missing custom settings. Here's what the documentation has to say about UIApplicationOpenSettingsURLString:

Used to create a URL that you can pass to the openURL(_:) method. When you open the URL built from this string, the system launches the Settings app and displays the app’s custom settings, if it has any.

0

I wrote the below function, it worked for me. I help it will help you guys.

func openLocationSettings(){ let scheme:String = UIApplicationOpenSettingsURLString if let url = URL(string: scheme) { if #available(iOS 10, *) { UIApplication.shared.open(url, options: [:], completionHandler: { (success) in print("Open \(scheme): \(success)") }) } else { let success = UIApplication.shared.openURL(url) print("Open \(scheme): \(success)") } } }

Naveed Ahmad
  • 6,627
  • 2
  • 58
  • 83