2016-08-19 11 views
1

CoreLocation이 오늘 확장 기능에서 작동하지 않는 것 같습니다. CoreLocation 프레임 워크를 내 Today Extension에 이미 추가했으며 plist (NSLocationAlwaysUsageDescription)에 필요한 항목을 추가하고 기본 위치를 추가했지만 여전히 작동하지 않습니다. 위도를 로그에 인쇄하려고합니다.오늘 확장 기능에서 CoreLocation이 작동하지 않음

iOS 프로그래밍을 처음 접했을 때 도움이 될 것입니다 !! 당신은이 일을 할 수 ↓↓↓

import Foundation 
import UIKit 
import NotificationCenter 
import CoreLocation 

class WidgetViewController: UIViewController, NCWidgetProviding, CLLocationManagerDelegate { 


var latitude: Double? 
var longitude: Double? 


var manager:CLLocationManager! 


override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view from its nib. 


    manager = CLLocationManager() 
    manager.delegate = self 
    manager.desiredAccuracy - kCLLocationAccuracyBest 
    manager.requestAlwaysAuthorization() 
    manager.startUpdatingLocation() 


    delay(1.0) { 
     print("\(self.latitude)") 
    } 


} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) { 
    // Perform any setup necessary in order to update the view. 

    // If an error is encountered, use NCUpdateResult.Failed 
    // If there's no update required, use NCUpdateResult.NoData 
    // If there's an update, use NCUpdateResult.NewData 

    completionHandler(NCUpdateResult.NewData) 
} 


// MARK: - Location Code 

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

    print(locations) 

    //userLocation - there is no need for casting, because we are now using CLLocation object 

    var userLocation:CLLocation = locations[0] 


    latitude = (userLocation.coordinate.latitude) 

    longitude = (userLocation.coordinate.longitude) 


    CLGeocoder().reverseGeocodeLocation(userLocation, completionHandler: { (placemarks, error) -> Void in 

     if (error != nil) { 

      print(error) 

     } else { 

      if let p = placemarks?[0] { 

       var subThoroughfare:String = "" 

       if (p.subThoroughfare != nil) { 

        subThoroughfare = p.subThoroughfare! 

       } 


       print(p) 

      } 


     } 

    }) 

} 


func delay(delay: Double, closure:()->()) { 


    dispatch_after(
     dispatch_time(
      DISPATCH_TIME_NOW, 
      Int64(delay * Double(NSEC_PER_SEC)) 
     ), 
     dispatch_get_main_queue(), 
     closure 
    ) 
} 
} 
+0

위치 권한 경고를 받았습니까? – firstinq

+0

예, 알림을 받았습니다. – fellowProgrammer

답변

0

아래 코드를 참조하십시오 : 그것은 실제 장치에서 작동

  • 확인합니다.
  • 또는 대신 엑스 코드 빌드 방식에 미리 정의 된 위치 (I는 자신이 확인하고 작동)

샘플 GPX 파일 내용의 GPX 파일을 사용하여 위치를 시뮬레이션 :

<gpx version="1.1" creator="Xcode"> 
    <wpt lat="47.52789018096815" lon="7.385249894876031"></wpt> 
    <wpt lat="47.52720984945248" lon="7.382647784661411"></wpt> 
</gpx> 
+0

감사합니다. 시도해보십시오 !! – fellowProgrammer