iOS:openURL到UIApplicationOpenSettingsURLString Preferences.app崩溃

时间:2017-05-16 22:51:52

标签: ios objective-c iphone xcode8

我正在尝试获取用户访问相机胶卷的权限,这是我的代码:

PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status != PHAuthorizationStatusAuthorized) {
    NSString *accessDescription = [[NSBundle mainBundle]
                                   objectForInfoDictionaryKey:@"NSPhotoLibraryUsageDescription"];
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:accessDescription
                                                                              message:@"To give permissions tap on 'Change Settings' button"
                                                                       preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                                           style:UIAlertActionStyleCancel
                                                         handler:nil];
    [alertController addAction:cancelAction];
    UIAlertAction *settingsAction = [UIAlertAction
                                     actionWithTitle:@"Change Settings"
                                     style:UIAlertActionStyleDefault
                                     handler:^(UIAlertAction * _Nonnull action) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]
                                           options:@{}
                                 completionHandler:nil];

    }];
    [alertController addAction:settingsAction];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController
                                                                                 animated:YES
                                                                               completion:nil];

}

但是,当用户按“更改设置”并尝试访问系统首选项时:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]
                                               options:@{}
                                     completionHandler:nil];

我遇到了这次崩溃:

Hardware Model:      iPhone7,2
Process:             Preferences [4364]
Path:                /Applications/Preferences.app/Preferences
Identifier:          com.apple.Preferences
Version:             1.0 (1)
Code Type:           ARM-64 (Native)
Role:                Foreground
Parent Process:      launchd [1]
Coalition:           com.apple.Preferences [1370]


Date/Time:           2017-05-16 15:43:24.1235 -0700
Launch Time:         2017-05-16 15:43:23.3450 -0700
OS Version:          iPhone OS 10.3.1 (14E304)
Report Version:      104

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Triggered by Thread:  0

Application Specific Information:
abort() called

Filtered syslog:
None found

Last Exception Backtrace:
0   CoreFoundation                  0x190c12fd8 __exceptionPreprocess + 124
1   libobjc.A.dylib                 0x18f674538 objc_exception_throw + 56
2   CoreFoundation                  0x190c12f20 +[NSException raise:format:] + 116
3   Foundation                      0x1916780b4 -[NSBundle initWithURL:] + 104
4   Foundation                      0x191678038 +[NSBundle bundleWithURL:] + 48
5   PreferencesUI                   0x1a3fd54dc -[PSUIPrefsListController _reallyLoadThirdPartySpecifiersForApps:withCompletion:] + 740
6   PreferencesUI                   0x1a3fd50d8 -[PSUIPrefsListController loadThirdPartySpecifierIfNeededForBundleID:] + 416
7   Preferences                     0x1000a0710 0x100094000 + 50960
8   Preferences                     0x10009e138 0x100094000 + 41272
9   UIKit                           0x196fcd180 __45-[UIApplication _applicationOpenURL:payload:]_block_invoke + 964
10  UIKit                           0x196fccb34 -[UIApplication _applicationOpenURL:payload:] + 644
11  UIKit                           0x196fbf9e8 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 3940
12  UIKit                           0x196fc52a8 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1684
13  UIKit                           0x196fd9de0 __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke.3151 + 48

你们中的任何人都知道我为什么或做错了什么?

我真的很感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

使用:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

您使用的方法可从iOS 10获得,如果您将在ios 10下运行应用程序,那么它将崩溃。

在调用方法之前检查iOS版本或使用上述方法。