ios 从另一个应用程序打开设置应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5655674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Opening the Settings app from another app
提问by FelipeDev.-
Okay, I know that there are many question about it, but they are all from many time ago.
好吧,我知道有很多关于它的问题,但它们都是很久以前的。
So. I know that it is possible because the Map app does it.
所以。我知道这是可能的,因为地图应用程序做到了。
In the Map app if I turn off the localization for this app, it send me a message, and if I press okay, the "Settings App" will be open. And my question is, how is this possible? How can I open the "Setting app" from my own app?
在地图应用程序中,如果我关闭此应用程序的本地化,它会向我发送一条消息,如果我按确定,“设置应用程序”将打开。我的问题是,这怎么可能?如何从我自己的应用程序打开“设置应用程序”?
Basically I need to do the same thing, if the user turn off the location for my app, then I'll show him a message saying something that will open the "Setting app"
基本上我需要做同样的事情,如果用户关闭我的应用程序的位置,那么我会向他显示一条消息,说一些会打开“设置应用程序”的内容
回答by Joe
As mentioned by Karan Duathis is now possible in iOS8using UIApplicationOpenSettingsURLString
see Apple's Documentation.
正如Karan Dua所提到的,这现在可以在 iOS8 中使用,UIApplicationOpenSettingsURLString
参见Apple 的文档。
Example:
例子:
Swift 4.2
斯威夫特 4.2
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
In Swift 3:
在 Swift 3 中:
UIApplication.shared.open(URL(string:UIApplicationOpenSettingsURLString)!)
In Swift 2:
在 Swift 2 中:
UIApplication.sharedApplication().openURL(NSURL(string:UIApplicationOpenSettingsURLString)!)
In Objective-C
在 Objective-C 中
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
Prior to iOS 8:
在 iOS 8 之前:
You can not. As you said this has been covered many times and that pop up asking you to turn on location services is supplied by Apple and not by the App itself. That is why it is able to the open the settings application.
你不能。正如您所说,这已被多次提及,并且要求您打开定位服务的弹出窗口是由 Apple 提供的,而不是由应用程序本身提供的。这就是为什么它能够打开设置应用程序的原因。
Here are a few related questions & articles:
以下是一些相关的问题和文章:
is it possible to open Settings App using openURL?
Programmatically opening the settings app (iPhone)
How can I open the Settings app when the user presses a button?
iPhone: Opening Application Preferences Panel From App
Open UIPickerView by clicking on an entry in the app's preferences - How to?
通过单击应用程序首选项中的条目打开 UIPickerView - 如何?
回答by Karan Dua
From @Yatheeshaless's answer:
来自@Yatheeshaless的回答:
You can open settings app programmatically in iOS8, but not in earlier versions of iOS.
您可以在 iOS8 中以编程方式打开设置应用程序,但不能在早期版本的 iOS 中打开。
Swift:
迅速:
UIApplication.sharedApplication().openURL(NSURL(string:UIApplicationOpenSettingsURLString)!)
Swift 4:
斯威夫特 4:
if let url = NSURL(string: UIApplicationOpenSettingsURLString) as URL? {
UIApplication.shared.openURL(url)
}
Swift 4.2 (BETA):
斯威夫特 4.2(测试版):
if let url = NSURL(string: UIApplication.openSettingsURLString) as URL? {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
Objective-C:
目标-C:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
回答by swiftBoy
YES!!you can launch Device Settings screen, I have tested on iOS 9.2
是的!!您可以启动设备设置屏幕,我已经在 iOS 9.2 上测试过
Step 1.we need to add URL schemes
步骤 1.我们需要添加 URL 方案
Go to Project settings --> Info --> URL Types --> Add New URL Schemes
转到项目设置 --> 信息 --> URL 类型 --> 添加新的 URL 方案
Step 2.Launch Settings programmatically Thanks to @davidcann
第 2 步。感谢@davidcann,以编程方式启动设置
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs://"]];
Also we can launch sub-screens like Music, Location etc. as well by just using proper name
我们也可以通过使用适当的名称来启动子屏幕,如音乐、位置等
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=MUSIC"]];
See this full name list hereshared by Henri Normak
在此处查看Henri Normak 共享的全名列表
Update:
更新:
As per the comment everyone wants to know what happens after this change to my application submission status?
根据评论,每个人都想知道我的申请提交状态发生变化后会发生什么?
So YES!! I got successful update submission and application is available on store without any complain.
所以 YES!! I got successful update submission and application is available on store without any complain.
Just to confirm, I Just downloaded this morning and disabled Location services, and then started the app, which asked me for location permission and then my alert popup was there to send me on settings -> location services page --> Enabled --> That's it!!
只是为了确认一下,我今天早上刚刚下载并禁用了定位服务,然后启动了应用程序,它要求我获得位置许可,然后我的警报弹出窗口在那里向我发送设置-> 定位服务页面--> 启用-->就是这样!!
![NOTICE: Your app might be rejected ... even if it's approved it can be rejected in future version if you use this method...]4
回答by davidcann
You can use this on iOS 5.0 and later:This no longer works.
您可以在 iOS 5.0 及更高版本上使用它:这不再有效。
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs://"]];
回答by guyromb
iOS 10 update
iOS 10 更新
Apple changed the method to open async on the main thread. However, from now it is only possible to open the app settings in native settings.
Apple 更改了在主线程上打开异步的方法。但是,从现在开始只能在本机设置中打开应用程序设置。
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
iOS 9 update
iOS 9 更新
It is now possible to go directly to sub-settings menu. However, a URL scheme has to be created. It can be done using two ways:
现在可以直接进入子设置菜单。但是,必须创建 URL 方案。可以通过两种方式完成:
- XCode - You will find it in Target, Info, URL Scheme. Then, just type prefs.
- Directly adding to *-Info.plist. Add the following:
<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleTypeRole</key> <string>Editor</string> <key>CFBundleURLSchemes</key> <array> <string>prefs</string> </array> </dict> </array>
- XCode - 您可以在 Target、Info、URL Scheme 中找到它。然后,只需键入首选项。
- 直接添加到*-Info.plist。添加以下内容:
<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleTypeRole</key> <string>Editor</string> <key>CFBundleURLSchemes</key> <array> <string>prefs</string> </array> </dict> </array>
Then the code:
然后代码:
Swift
迅速
UIApplication.sharedApplication().openURL(NSURL(string:"prefs:root=General&path=Keyboard")!)
UIApplication.sharedApplication().openURL(NSURL(string:"prefs:root=General&path=Keyboard")!)
Objective-c
目标-c
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=General&path=Keyboard"]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=General&path=Keyboard"]];
回答by Damo
In Swift 3 / iOS 10+ this now looks like
在 Swift 3 / iOS 10+ 中,现在看起来像
if let url = URL(string: "App-Prefs:root=LOCATION_SERVICES") {
UIApplication.shared.open(url, completionHandler: .none)
}
回答by Duy Doan
Swift 3:
斯威夫特 3:
guard let url = URL(string: UIApplicationOpenSettingsURLString) else {return}
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
// Fallback on earlier versions
UIApplication.shared.openURL(url)
}
回答by ynamao
In Swift 3 all I needed is this (here for example redirect to my app notifications):
在 Swift 3 中,我所需要的就是这个(例如重定向到我的应用程序通知):
if let url = URL(string: "App-Prefs:root=NOTIFICATIONS_ID&path=your app bundleID") {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, completionHandler: .none)
} else {
// Fallback on earlier versions
}
}
Source: phynet gist.
来源:phynet 要点。
This worked with me only when settings is in background. It will redirect you to your app notification settings but if settings wasn't running in the background it will just redirect you to notification settings in general.
仅当设置在后台时才对我有用。它会将您重定向到您的应用程序通知设置,但如果设置未在后台运行,它通常只会将您重定向到通知设置。
回答by Hemang
UIApplicationOpenSettingsURLString
this will only work if you have previously allowed for any permission. For example Location, Photo, Contact, Push notification access. So if you have not such permission(s) from the user:
UIApplicationOpenSettingsURLString
这仅在您之前允许任何许可时才有效。例如位置、照片、联系人、推送通知访问。因此,如果您没有来自用户的此类权限:
If iOS 10 or above,
如果iOS 10 或更高版本,
It will open the Settings but then crash it. The reason, there's nothing in settings for your app.
它将打开设置,但随后崩溃。原因是,您的应用程序设置中没有任何内容。
Below code will open your application settings inside the iOS Setting.
下面的代码将在 iOS 设置中打开您的应用程序设置。
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
Due to device unavailability, I couldn't check this on iOS < 10.
由于设备不可用,我无法在 iOS < 10 上进行检查。
Also, I could find below code from some gist and it works fine on iOS 10 as well. But I am not sure if this will approve by Apple review team or not.
另外,我可以从一些要点中找到以下代码,它在 iOS 10 上也能正常工作。但我不确定这是否会得到 Apple 审核团队的批准。
https://gist.github.com/johnny77221/bcaa5384a242b64bfd0b8a715f48e69f
https://gist.github.com/johnny77221/bcaa5384a242b64bfd0b8a715f48e69f
回答by Som
You can use the below code for it.
您可以使用以下代码。
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];