折腾:
[规避解决]swift中通过CNContactStore获取联系人列表出错:error=Error Domain=CNErrorDomain Code=100 "访问被拒绝”
期间,几次遇到:
然后对应的log很诡异,说是和自动布局有关:
addPhoneContact readPhoneContactToList contactStore=<CNDataMapperContactStore: 0x7b641c20> authStatus=CNAuthorizationStatus granted=false 2016-01-01 19:18:37.443 JianDao[34983:1721669] This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release. Stack:( 0 CoreFoundation 0x0110da14 __exceptionPreprocess + 180 1 libobjc.A.dylib 0x033fee02 objc_exception_throw + 50 2 CoreFoundation 0x0110d93d +[NSException raise:format:] + 141 3 Foundation 0x018a274b _AssertAutolayoutOnMainThreadOnly + 96 4 Foundation 0x016caea9 -[NSISEngine withBehaviors:performModifications:] + 26 5 Foundation 0x016ce404 -[NSISEngine withAutomaticOptimizationDisabled:] + 48 6 UIKit 0x02913df4 -[UIView(UIConstraintBasedLayout) _calculatedSystemLayoutSizeFittingSize:withHorizontalFittingPriority:verticalFittingPriority:hasIntentionallyCollapsedHeight:] + 1238 7 UIKit 0x0291595a -[UIView(AdditionalLayoutSupport) _systemLayoutSizeFittingSize:withHorizontalFittingPriority:verticalFittingPriority:hasIntentionallyCollapsedHeight:] + 972 8 UIKit 0x029154b1 -[UIView(UIConstraintBasedLayout) systemLayoutSizeFittingSize:withHorizontalFittingPriority:verticalFittingPriority:] + 393 9 UIKit 0x02915500 -[UIView(UIConstraintBasedLayout) systemLayoutSizeFittingSize:] + 71 10 UIKit 0x02a38e8b -[_UIAlertControllerView _minimumSizeForAllActions] + 589 11 UIKit 0x02a3570f -[_UIAlertControllerView _itemSizeForHorizontalLayout:] + 451 12 UIKit 0x02a35943 -[_UIAlertControllerView _canLayOutActionsHorizontally] + 108 13 UIKit 0x02a35e6b -[_UIAlertControllerView _actionLayoutDirectionChanged] + 42 14 UIKit 0x02a3afae -[_UIAlertControllerView _updateBackdrop] + 577 15 UIKit 0x02a39df9 -[_UIAlertControllerView _updateStyleForIdiomChange:] + 156 16 UIKit 0x02a39d25 -[_UIAlertControllerView _setVisualStyle:] + 296 17 UIKit 0x022b2d29 -[UIAlertController _updateProvidedStyleWithTraitCollection:] + 525 18 UIKit 0x022b2e1d -[UIAlertController _updateProvidedStyle] + 68 19 UIKit 0x022b2f85 -[UIAlertController _resolvedStyleChanged] + 35 20 UIKit 0x022b2456 -[UIAlertController viewDidLoad] + 98 21 UIKit 0x020f62ae -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled] + 44 22 UIKit 0x020fadce -[UIViewController loadViewIfRequired] + 1384 23 UIKit 0x020fb1ed -[UIViewController view] + 35 24 UIKit 0x021173ca -[UIViewController _setPresentationController:] + 117 25 UIKit 0x0210d9ee -[UIViewController _presentViewController:modalSourceViewController:presentationController:animationController:interactionController:completion:] + 1503 26 UIKit 0x0210f96d -[UIViewController _presentViewController:withAnimationController:completion:] + 5299 27 UIKit 0x02112b73 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 397 28 UIKit 0x02112eb8 -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 493 29 UIKit 0x0211299f -[UIViewController presentViewController:animated:completion:] + 182 30 JianDao 0x000add13 _TFFC7JianDao26PhoneContactViewController22readPhoneContactToListFS0_FT_T_U_FTSbGSqCSo7NSError__T_ + 1443 31 JianDao 0x000aaf3c _TPA__TFFC7JianDao26PhoneContactViewController22readPhoneContactToListFS0_FT_T_U_FTSbGSqCSo7NSError__T_ + 108 32 JianDao 0x000addf3 _TTRXFo_dSboGSqCSo7NSError__dT__XFdCb_dV10ObjectiveC8ObjCBooldGSqS___dT__ + 99 33 Contacts 0x00b108ac __75-[CNiOSAddressBookDataMapper requestAccessForEntityType:completionHandler:]_block_invoke + 82 34 libdispatch.dylib 0x04103377 _dispatch_call_block_and_release + 15 35 libdispatch.dylib 0x041269cd _dispatch_client_callout + 14 36 libdispatch.dylib 0x0410c985 _dispatch_root_queue_drain + 1008 37 libdispatch.dylib 0x0410c58e _dispatch_worker_thread3 + 115 38 libsystem_pthread.dylib 0x0444d43e _pthread_wqthread + 1050 39 libsystem_pthread.dylib 0x0444af72 start_wqthread + 34 ) |
难道是:
自动布局设置有误,导致了弹出用户授权的窗口没法显示?
然后此处修改了之前的frame设置,使得没有这类警告了:
但是还是没有用,没有弹窗:
self.view.frame=(0.0, 0.0, 320.0, 568.0) self.view.frame=(0.0, 0.0, 320.0, 504.0) readPhoneContactToList contactStore=<CNDataMapperContactStore: 0x7b6d9ff0> authStatus=CNAuthorizationStatus granted=false |
结果调试发现,此处还是同样的错误:
contactStore.requestAccessForEntityType(CNEntityType.Contacts, completionHandler: { (granted, error) -> Void in print("granted=\(granted), error=\(error)") //granted=false, error=Optional(Error Domain=CNErrorDomain Code=100 "访问被拒绝" UserInfo={NSLocalizedDescription=访问被拒绝, NSLocalizedFailureReason=此应用尚未获得访问“通讯录”的许可。}) |
结果后来又出现了:
readPhoneContactToList authStatus=CNAuthorizationStatus contactStore=<CNDataMapperContactStore: 0x796c35e0> granted=false, error=Optional(Error Domain=CNErrorDomain Code=100 "访问被拒绝" UserInfo={NSLocalizedDescription=访问被拒绝, NSLocalizedFailureReason=此应用尚未获得访问“通讯录”的许可。}) 2016-01-01 19:50:59.398 JianDao[35711:1762855] This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release. Stack:( 0 CoreFoundation 0x011dea14 __exceptionPreprocess + 180 1 libobjc.A.dylib 0x034cfe02 objc_exception_throw + 50 2 CoreFoundation 0x011de93d +[NSException raise:format:] + 141 3 Foundation 0x0197374b _AssertAutolayoutOnMainThreadOnly + 96 4 Foundation 0x0179bea9 -[NSISEngine withBehaviors:performModifications:] + 26 5 Foundation 0x0179f404 -[NSISEngine withAutomaticOptimizationDisabled:] + 48 6 UIKit 0x029e4df4 -[UIView(UIConstraintBasedLayout) _calculatedSystemLayoutSizeFittingSize:withHorizontalFittingPriority:verticalFittingPriority:hasIntentionallyCollapsedHeight:] + 1238 7 UIKit 0x029e695a -[UIView(AdditionalLayoutSupport) _systemLayoutSizeFittingSize:withHorizontalFittingPriority:verticalFittingPriority:hasIntentionallyCollapsedHeight:] + 972 8 UIKit 0x029e64b1 -[UIView(UIConstraintBasedLayout) systemLayoutSizeFittingSize:withHorizontalFittingPriority:verticalFittingPriority:] + 393 9 UIKit 0x029e6500 – |
看来,
真的很像是:
由于是否处于UI主线程,而导致此处的,没发弹出授权弹窗的。
搜:
swift CNContactStore This application is modifying the autolayout engine from a background thread
osx – Getting a "This application is modifying the autolayout engine" error? – Stack Overflow
然后代码改为:
case CNAuthorizationStatus.NotDetermined: dispatch_async(dispatch_get_main_queue(), { () -> Void in contactStore.requestAccessForEntityType(CNEntityType.Contacts, completionHandler: { (granted, error) -> Void in // CNContactStore().requestAccessForEntityType(CNEntityType.Contacts, completionHandler: { (granted, error) -> Void in print("granted=\(granted), error=\(error)") //granted=false, error=Optional(Error Domain=CNErrorDomain Code=100 "访问被拒绝" UserInfo={NSLocalizedDescription=访问被拒绝, NSLocalizedFailureReason=此应用尚未获得访问“通讯录”的许可。}) if granted { self.readPhoneContact(contactStore) }else{ let noAuthAlertController = UIAlertController(title: "您没有同意授权操作联系人!", message: nil, preferredStyle: UIAlertControllerStyle.Alert) let sureAlertAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Destructive, handler: nil) noAuthAlertController.addAction(sureAlertAction) self.presentViewController(noAuthAlertController, animated: true, completion: nil) } }) }) |
结果又是其它错误:
2016-01-01 20:23:49.724 JianDao[36294:1803783] Presenting view controllers on detached view controllers is discouraged <JianDao.ConversationViewController: 0x7c9b0f70>. 2016-01-01 20:23:49.740 JianDao[36294:1803783] This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release. |
然后放回到之前正常的ViewController中,则:
没有上述不正常的:Presenting view controllers on detached view controllers is discouraged
的了。
也没了:
This application is modifying the autolayout engine from a background thread
【总结】
swift中提示:
This application is modifying the autolayout engine from a background thread
指的是:
部分代码是,在非主UI线程之外,去更新UI元素,所以报此警告。
解决办法:
想办法找出,你哪端代码是在非UI主线程,更新了UI中的元素。
然后改为防到主线程即可。
转载请注明:在路上 » [已解决]swift调用CNContactStore的requestAccessForEntityType时出错:This application is modifying the autolayout engine from a background thread