Autolayout程式設計
Autolayout仰賴物件已經被加入到 View之中,才能正確的計算每個物件的位置及大小,所以任何一個在程式中 alloc、init的物件,在加到 View之前執行 Autolayout程式的話,就會發生 Exception。
- 取得父層的UIView
UIView* parentView = self.superview; - 取得父層View的 LayoutGuide
UILayoutGuide* margins = parentView.layoutMarginsGuide; - 物件置中並設定寬度與父層相等
[[self.centerXAnchor constraintEqualToAnchor: margins.centerXAnchor] setActive: true];
[[self.widthAnchor constraintEqualToAnchor: margins.widthAnchor] setActive: true]; - 左右對齊
[[self.leadingAnchor constraintEqualToAnchor: margins.leadingAnchor] setActive: true];
[[self.trailingAnchor constraintEqualToAnchor: margins.trailingAnchor] setActive: true]; - 如果想要對齊 Xcode的 Interface builder中的 top layout guide與 bottom layout guid,採用下列方式(參考 UIViewController的 topLayoutGuide)
UIViewController* parentViewController = (UIViewController*)self.superview;
[self setTranslatesAutoresizingMaskIntoConstraints: NO];
[parentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"V:[topLayoutGuide]-0-[self]" options: 0 metrics: nil views: NSDictionaryOfVariableBindings(parentViewController.topLayoutGuide, self)]];
[parentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"V:[self]-0-[bottomLayoutGuide]" options: 0 metrics: nil views: NSDictionaryOfVariableBindings(self, parentViewController.bottomLayoutGuide)]];
[self.superview layoutSubviews]; // You must call this method here or the system raises an exception
UIWebViewDelegate與 WKNavigationDelegate
- didFailLoadWithError:didFailNavigation
- webViewDidFinishLoad:didFinishNavigation
- webViewDidStartLoad:didStartProvisionalNavigation
- shouldStartLoadWithRequest:decidePolicyForNavigationAction
略過 SSL憑證錯誤
處理方式:
- 在Info.plist中加入 NSAppTransportSecurity、NSAllowsArbitraryLoads,如下:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict> - 實作 WKNavigationDelete的 didReceiveAuthenticationChallenge
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
CFDataRef exceptions = SecTrustCopyExceptions(serverTrust);
SecTrustSetExceptions(serverTrust, exceptions);
CFRelease(exceptions);
completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:serverTrust]);
}


