iOS程式設計

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憑證錯誤

在 iOS程式中,使用 WebView元件載入網頁時,如果採用 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]);
    }

git筆記

安裝設定 git伺服器端

  1. 安裝 git-core
    sudo apt-get install git-core
  2. 建立伺服器端的檔案存放位置,這裡建置在 /var/git/
    sudo mkdir /var/git
  3. 設定資料夾權限
    1. 建立 git群組
      sudo groupadd gitusers
    2. 設定資料夾權限(這個步驟每次有新資料夾建立,就需要執行)
      sudo chgrp -R gitusers /var/git
      sudo chmod g+rwx -R /var/git
  4. 將使用者加入群組
    sudo usermod -a -G gitusers your_account
  5. 建立專案資料夾
    1. sudo mkdir /var/git/new_project.git
    2. 重新指派資料夾權限,參考步驟 3.b.
    3. 初始化
      cd /var/git/new_project.git/
      sudo git --bare init

安裝設定 git使用者端

  1. 初始化資料夾
    • cd /path/to/your/project/
    • git init
  2. 把遠端 repo加入到 git remote
    git remote add origin ssh://your_login@your.host/var/git/new_project.git
    路徑要注意不要打錯,與 ssh的格式不一樣,在 host之後沒有冒號
    ssh://your_login@your.host:/path/to/my/file
  3. Commit程式
    git add .
    git commit -m 'my comment'
    git push origin master

Ubuntu linux 筆記

建立輕量化的Ubuntu Linux

  1. 安裝Ubuntu server
  2. 安裝 xorg、openbox、lubuntu-desktop,安裝 lubuntu-desktop會一併安裝 xorg、openbox
    sudo apt-get install lubuntu-desktop
  3. 安裝 obmenu。用 obmenu編輯 openbox環境下的選單,可以提昇 openbox的使用便利性
    sudo apt-get install obmenu
  4. 複製 openbox的預設檔案到 ~/.config/openbox/
    如果目錄不存在就先建立
    cp /etc/xdg/openbox/autostart ~/.config/openbox/
    cp /etc/xdg/openbox/menu.xml ~/.config/openbox/
    cp /etc/xdg/openbox/rc.xml ~/.config/openbox/
  5. 如果有任何想要在 openbox session開啟時就執行的指令,可以進一步修改 ~/.config/openbox/autostart

以上所建立起來的環境,適用在下列狀況
需要方便使用的 UI時,請登入 lubuntu session
需要極輕量的 UI時,請登入 openbox session

Apache伺服器管理 – Response Headers

基於安全性的考量,應該要儘量減少對外界揭露系統相關資訊。但是,在 Apache伺服器及網站的預設狀態下,在網頁的回應表頭中(Response Headers)會包含一些伺服器相關的訊息,比方說:Server、X-AspNet-Version等等的資訊,因此我們需要透過一些設定去變更或是移除這些資料,以減少風險。

mod_headers
這個是 Apache的預設模組之一,可以用來設定 Response Headers,預設是沒有啟用,也沒有提供預設的設定檔。

首先,到 /etc/apache2/mods-availables/ 建立設定檔

cd /etc/apache2/mods-availables/
sudo nano headers.conf

加入下列內容,並且存檔

<IfModule mod_headers.c>
Header unset X-AspNet-Version
Header unset X-AspNetMvc-Version
Header unset X-Powered-By
</IfModule>

啟用模組並重新載入 Apache

sudo a2enmod headers
sudo service apache2 restart

Server欄位需要採用另一個模組來處理,請往下看!

mod_security

這個模組可以做非常多的事,詳情請參考modsecurity.org。這邊採用這個模組的預設值,並增加指令以便控制 Apache伺服器的 response header中的 Server欄位。

安裝 libapache2-mod-security2

sudo apt-get install libapache2-mod-security2

複製建議設定值

sudo cp /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf

修改設定檔 modsecurity.conf

sudo nano /etc/modsecurity/modsecurity.conf

在檔案的最後,加入

SecServerSignature “My Server"

啟用模組,並重啟Apache伺服器

sudo a2enmod security2
sudo service apache2 reload

 

參考資料:
Apache module – mod_headers
modsecurity.org
mod_security reference manual

實際案例:
roger543