手把手教你Today擴(kuò)展(三):在擴(kuò)展中使用定位功能

1. 如何去除Today擴(kuò)展左邊的margin

Paste_Image.png

如上圖去除紅色邊框。
在Today的這個(gè)類中,重寫下面這個(gè)方法:

func widgetMarginInsetsForProposedMarginInsets(defaultMarginInsets: UIEdgeInsets) -> UIEdgeInsets {
        return UIEdgeInsetsZero
    }

2. 單獨(dú)測試Today擴(kuò)展

Paste_Image.png

將target換成擴(kuò)展再運(yùn)行。

3. Today擴(kuò)展獲取地理位置

  • 導(dǎo)入CoreLocation框架
import CoreLocation
  • 修改Today的info
    添加NSLocationWhenInUseUsageDescription字段在info中。
    Paste_Image.png
  • 在Today類viewDidLoad添加如下代碼
var locationManager: CLLocationManager = CLLocationManager()
locationManager.delegate = self
        if locationManager.respondsToSelector("requestWhenInUseAuthorization") {
            //這個(gè)方法是當(dāng)用戶允許定位之后就立刻響應(yīng)的
            locationManager.requestWhenInUseAuthorization()
        } 
        locationManager.startUpdatingLocation()
  • 添加回掉方法
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location: CLLocation = locations.last!
        city.text = "\(location.coordinate)"
        
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容