2016-11-16 7 views
7

में काम नहीं कर UNLocationNotificationTrigger- उपयोगकर्ता सूचना ढांचा उपलब्ध आईओएस 10 में मैं जब भी उपयोगकर्ता UNLocationNotificationTrigger का उपयोग कर विशिष्ट भौगोलिक स्थान में प्रवेश करती है सूचना उत्प्रेरित करने की कोशिश की का उपयोग करना। जब मैंने भौगोलिक स्थान को अनुकरण करके सिम्युलेटर के माध्यम से परीक्षण करने का प्रयास किया, तो अधिसूचना ट्रिगर नहीं हो रही है, लेकिन स्थान प्रबंधक अद्यतन भौगोलिक स्थान लौटाता है। क्या इसे सिम्युलेटर में चलाने के बजाए वास्तविक डिवाइस में परीक्षण किया जाना चाहिए?सिम्युलेटर

+0

मैं मुसीबत के साथ-साथ वास्तविक डिवाइस में सूचना हो रही हो रही है। :/ – Rihards

+0

मैं सिम्युलेटर और डिवाइस दोनों पर उपयोगकर्ता अधिसूचना फ्रेमवर्क का उपयोग करके दिनांक और समय के आधार पर अधिसूचना ट्रिगर करने में सक्षम हूं। मैं अभी तक डिवाइस में स्थान आधारित अधिसूचना की जांच नहीं कर रहा हूं। – Ashok

+0

क्या आप इसे ठीक करने में कामयाब रहे हैं? यदि नहीं, तो मैं आपको एक हाथ दे सकता हूं। –

उत्तर

2

Apple Documentation के अनुसार:

Apps का स्थान सेवाओं तक पहुंच का अनुरोध करना होगा और इस वर्ग का उपयोग करने के लिए जब-इन-उपयोग अनुमतियाँ होना आवश्यक है। स्थान सेवाओं का उपयोग करने के लिए अनुमति का अनुरोध करने के लिए, किसी भी स्थान-आधारित ट्रिगर्स को शेड्यूल करने से पहले CLLocationManager की requestWhenInUseAuthorization() विधि को कॉल करें।

हालांकि मेरे अनुकरणकर्ता/उपकरणों "जब उपयोग में" अनुमतियां पर्याप्त नहीं हैं, अनुमतियों को हमेशा "हमेशा" पर सेट किया जाना चाहिए।

इस प्रकार,

<key>NSLocationAlwaysUsageDescription</key> 
<string>We use your location to warn you when there are adorable cats nearby</string> 

तो स्थान को सक्रिय अपने pinfo.list के लिए इस कुंजी जोड़ें। आपके ट्रिगर केवल एक बार आप जानते हैं कि आप हमेशा के लिए अधिकृत हैं परिभाषित करें, उदाहरण के लिए मैं इसे यहाँ किया didChangeAuthorizationStatus में:

class myClass : CLLocationManagerDelegate { 

var locationManager: CLLocationManager() 

func init() { 
    // Note: defining the location manager locally in this function won't work 
    // var locationManager: CLLocationManager() 
    // as it gets gargabe collected too early. 

    locationManager.delegate = self 
    locationManager.requestAlwaysAuthorization() 
    UNUserNotificationCenter.current().delegate = self 
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) {(accepted, error) in 
    if !accepted { 
     logger.info("Notification access denied.") 
    } 

} 

// MARK CLLocationManagerDelegate: 
func locationManager(manager: CLLocationManager, 
        didChangeAuthorizationStatus status: CLAuthorizationStatus) 
{ 
    if status == .AuthorizedAlways { 

     let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: 61.446812, longitude: 23.859914), 
        radius: 1000, identifier: "test") 
     logger.info("Notification will trigger at \(region)") 
     region.notifyOnEntry = true 
     region.notifyOnExit = false 

     let trigger = UNLocationNotificationTrigger(region: region, repeats:true) 

     let content = UNMutableNotificationContent() 
     content.title = "Oh Dear !" 
     content.body = "It's working!" 
     content.sound = UNNotificationSound.default() 

     let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger) 

     UNUserNotificationCenter.current().removeAllPendingNotificationRequests() 
     UNUserNotificationCenter.current().add(request) {(error) in 
      if let error = error { 
       print("Uh oh! We had an error: \(error)") 
      } 
     } 

    } 
} 
} 
संबंधित मुद्दे