您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS8 開發中遇到的幾個常見問題與解法

編輯:iOS 開發綜合

從 iOS 7 升級到 iOS 8 後,不少原本正常的應用會出現各種問題。本文整理了三個常見的坑與對應解法:定位、模擬器鍵盤、以及推送。

一、無法定位

在「設定 > 隱私 > 定位服務」中勾選了「使用應用程式期間」,但打開 app 再回到設定,卻發現勾選又被取消了。原因是 iOS 8 需要開發者在程式碼中主動請求授權。

解法如下:

  • 若只需在前景定位,在呼叫 startUpdatingLocation 前呼叫 requestWhenInUseAuthorization;
  • 若需要前後台都能定位,則呼叫 requestAlwaysAuthorization;
  • 同時在 plist 中加入 NSLocationWhenInUseUsageDescription 或 NSLocationAlwaysUsageDescription 欄位,值填寫向使用者說明用途的文字(如「需要定位」),這段文字會在詢問授權時顯示。
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
    [_locationManager requestWhenInUseAuthorization];
}
[_locationManager startUpdatingLocation];

二、模擬器鍵盤不管用

若 iOS 模擬器的實體鍵盤無法輸入,可透過選單修正:

iOS Simulator > Hardware > Keyboard,取消勾選「Connect Hardware Keyboard」。

三、推送(遠程通知)不管用

iOS 8 修改了通知註冊的 API,需要區分版本處理:

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
    [app registerForRemoteNotifications];
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:
        UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound
        categories:nil];
    [app registerUserNotificationSettings:settings];
} else {
    [app registerForRemoteNotificationTypes:
        UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
}

小結

iOS 8 在定位授權與通知註冊上引入了更細緻的 API 變革。掌握這些差異並做好版本判斷,就能讓應用在舊版與新版系統上都能正常運作。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved