iOS 开发随笔iOS DeveloperiOS 开发

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

2016-01-07  本文已影响157人  Hollylord

1. 如何去除Today扩展左边的margin

Paste_Image.png

如上图去除红色边框。
在Today的这个类中,重写下面这个方法:

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

2. 单独测试Today扩展

Paste_Image.png

将target换成扩展再运行。

3. Today扩展获取地理位置

import CoreLocation
var locationManager: CLLocationManager = CLLocationManager()
locationManager.delegate = self
        if locationManager.respondsToSelector("requestWhenInUseAuthorization") {
            //这个方法是当用户允许定位之后就立刻响应的
            locationManager.requestWhenInUseAuthorization()
        } 
        locationManager.startUpdatingLocation()
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location: CLLocation = locations.last!
        city.text = "\(location.coordinate)"
        
    }
上一篇 下一篇

猜你喜欢

热点阅读