如果未授予照片访问权限且请求权限的对象即将解除分配,应用程序将崩溃.
Apple的示例代码(用于Photos框架)也崩溃了.
我的应用程序崩溃了以下堆栈跟踪:
*** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'This application is not allowed to access Photo data.'
*** First throw call stack:
(
0 CoreFoundation 0x000000010a324f65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010c030deb objc_exception_throw + 48
2 PhotoLibraryServices 0x000000011c1f7e2d -[PLPhotoLibrary initWithTransientContext:name:] + 1183
3 Photos 0x000000011bd6a445 __30-[PHPhotoLibrary photoLibrary]_block_invoke + 59
4 libdispatch.dylib 0x0000000110f5e49b _dispatch_client_callout + 8
5 libdispatch.dylib 0x0000000110f49e28 dispatch_once_f + 543
6 Photos 0x000000011bd6a404 -[PHPhotoLibrary photoLibrary] + 140
7 Photos 0x000000011bd3e5e8 -[PHCoreImageManager _cancelAndFlushPreheatItemsForAssets:CPLPrefetching:domain:operation:passingTestHandler:didCancelHandler:] + 174
8 … 无法准备NSURL带有包含表情符号图标的字符串的对象。
当我尝试NSURL使用以下方法形成时:
let urlString = "http://www.example.com/"
let URL = NSURL(string: urlString)
Run Code Online (Sandbox Code Playgroud)
URL 返回值为 nil,而浏览器可以使用相同的 urlString 打开有效网页。
我想在 UIWebView 中打开它。那么有没有像如何正确编码以满足 NSURL 要求之类的想法?
任何指针将不胜感激。
更新: 当我尝试使用以下字符串准备 NSURL 对象时:
let encodedURLString = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
它工作得很好,因为stringByAddingPercentEscapesUsingEncoding()方法将非法 url 字符转换为合法表示。但这已被弃用。
stringByAddingPercentEncodingWithAllowedCharacters()可以使用它的替代方法,但必须在不同的 url 组件上调用它,例如:
// host: "www.example.com", path: "/"
host.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLHostAllowedCharacterSet())
path.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLPathAllowedCharacterSet())
Run Code Online (Sandbox Code Playgroud)
如何从字符串中获取 url 组件(即主机、路径等),因为当我尝试创建 NSURL/NSURLComponents 时,它返回 nil。(原因是格式错误的 url 字符串。)??
通过堆栈跟踪,unsafeMutableAddressor崩溃似乎发生在AppConstant.kShowOverlay从控制器访问时viewDidLoad()
我的定义AppConstant.swift如下:
struct AppConstant {\n // MARK: Properties\n static let kShowOverlay = false\n}\nRun Code Online (Sandbox Code Playgroud)\n\n以下是 crashlytics 的堆栈跟踪:
\n\nCrashed: com.apple.main-thread\n EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000000000048\n\n 0\xc2\xa0 libswiftCore.dylib\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa00x51bfc5c _TTWuRq_Ss9Indexable_GVSs5Sliceq__Ss14CollectionTypeSsFS1_10suffixFromuRq_S1__fq_Fqq_S_5Indexqq_S1_11SubSequence\n 1\xc2\xa0 MyAppModule\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa00x56fbad4 AppConstants.kShowOverlay.unsafeMutableAddressor (AppConstants.swift)\n 2\xc2\xa0 libdispatch.dylib\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa00x34ba9f13 dispatch_once_f + 62\n 3\xc2\xa0 MyApp\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa00x23e38e8 globalinit_33_D6508E60499748694DF7352631AEB836_func18 (MyViewController.swift:73)\n 4\xc2\xa0 libdispatch.dylib\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa00x34ba92cf _dispatch_client_callout + 22\n 5\xc2\xa0 libdispatch.dylib\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa00x34ba9f13 dispatch_once_f + 62\n 6\xc2\xa0 MyApp\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 \xc2\xa0\xc2\xa0\xc2\xa00x2383e88 MyViewController.setupCollectionView(MyViewController) -> () -> () (MyViewController.swift:530)\n 7\xc2\xa0 MyApp\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa00x29f0a3c MyViewController.viewDidLoad(MyViewController) -> () -> () (MyViewController.swift:59)\nRun Code Online (Sandbox Code Playgroud)\n\n我无法重现此崩溃,只能从 crashlytics 获取堆栈跟踪。
\n\n …