SwiftでのGeoFence機能実装

1a1d418bdf51cbf8fce1317f6c80a907?s=47 satoshi0212
August 20, 2016
6.2k

 SwiftでのGeoFence機能実装

1a1d418bdf51cbf8fce1317f6c80a907?s=128

satoshi0212

August 20, 2016
Tweet

Transcript

  1. SwiftͰͷGeoFenceػೳ࣮૷ Tips Λఴ͑ͯ ෰෦ ஐ satoshi0212 גࣜձࣾΤΫεϥΠζ νʔϑΤϯδχΞ

  2. ࣗݾ঺հ iOSΞϓϦΤϯδχΞ ΧϦϑΥϧχΞभཱେֶίϯϐϡʔλαΠΤϯεֶ෦ଔ גࣜձࣾΤΫεϥΠζॴଐ

  3. ࣗݾ঺հ ৽͍ٕ͠ज़ͱɺͦͷগ͠มΘͬͨ࢖͍ํ͕େ޷͖ ɹTechCrunch Tokyo 2015 Hackathon ༏ल৆ ɹMizuho.Hack ༏ल৆ ɹMicrosoft

    MPC Hackathon TMCN৆ ɹHack Cars Days 2015 χϑςΟ৆ ɹMUSICIANS HACKATHON 2015 ༏ल৆ ɹɹͳͲ
  4. ࠓ೔࿩͢಺༰ GeoFenceػೳͱ͸ ɹ1. ಛ௃ͱ੍ݶ ɹ2. ࣮૷ํ๏ ɹ3. Tips΍௚໘ͨ͠໰୊ͳͲ ·ͱΊ

  5. ࠓ೔࿩͢಺༰ GeoFenceػೳͱ͸ ɹ1. ಛ௃ͱ੍ݶ ɹ2. ࣮૷ํ๏ ɹ3. Tips΍௚໘ͨ͠໰୊ͳͲ ·ͱΊ

  6. GeoFenceػೳͱ͸ ʮҐஔ৘ใαʔϏεΛ࢖༻ͯ͠ɺط஌ͷ஍ཧతҐஔʹ͍ͭ ͯͷ͋ΒΏΔਐೖ·ͨ͸ୀग़Λݕग़͠·͢ʯ "Ґஔ৘ใͱϚοϓϓϩάϥϛϯάΨΠυ" ΑΓ

  7. GeoFenceػೳͱ͸ ʮҐஔ৘ใαʔϏεΛ࢖༻ͯ͠ɺط஌ͷ஍ཧతҐஔʹ͍ͭ ͯͷ͋ΒΏΔਐೖ·ͨ͸ୀग़Λݕग़͠·͢ʯ "Ґஔ৘ใͱϚοϓϓϩάϥϛϯάΨΠυ" ΑΓ

  8. GeoFenceػೳͱ͸ ʮҐஔ৘ใαʔϏεΛ࢖༻ͯ͠ɺط஌ͷ஍ཧతҐஔʹ͍ͭ ͯͷ͋ΒΏΔਐೖ·ͨ͸ୀग़Λݕग़͠·͢ʯ "Ґஔ৘ใͱϚοϓϓϩάϥϛϯάΨΠυ" ΑΓ

  9. GeoFenceػೳͱ͸ ʮͨͱ͑͹ɺॴఆͷυϥΠΫϦʔχϯάళʹ͍ۙͮͨͱ͖ɺ ࢓্͕͍ͬͯΔ෰Λड͚औΔΑ͏௨஌͢ΔΞϓϦέʔγϣ ϯ͕ߟ͑ΒΕ·͢ʯ"Ґஔ৘ใͱϚοϓϓϩάϥϛϯάΨΠυ" ΑΓ

  10. GeoFenceػೳͱ͸ ɹAppleެࣜʹ͸“Geographical Region Monitoring” ɹ೔ຊޠͰ͸ʮ஍ཧతྖҬͷ؍ଌʯ

  11. Ґஔ৘ใ࣮૷ํ๏ͷ؆୯ͳ·ͱΊ

  12. 1 Standard Location 2 Background Location Session 3 Beacon Ranging

    4 Geographical Region Monitoring 5 Visit Monitoring 6 Significant Location Change Monitoring Ґஔ৘ใ࣮૷ํ๏ͷ؆୯ͳ·ͱΊ
  13. 1 Standard Location ඪ४Ґஔ৘ใ 2 Background Location Session όοΫάϥ΢ϯυ ͰͷҐஔ৘ใ

    3 Beacon Ranging ϏʔίϯྖҬ؍ଌ 4 Geographical Region Monitoring ஍ཧతྖҬ؍ଌ 5 Visit Monitoring ๚໰ઌ௥੻ 6 Significant Location Change Monitoring େ෯มߋҐஔ৘ใ Ґஔ৘ใ࣮૷ํ๏ͷ؆୯ͳ·ͱΊ
  14. 4VTQFOEFE /PU3VOOJOH U3VOOJOH 1 Standard Location ඪ४Ґஔ৘ใ 2 Background Location

    Session όοΫάϥ΢ϯυ ͰͷҐஔ৘ใ 3 Beacon Ranging ϏʔίϯྖҬ؍ଌ ✔ 4 Geographical Region Monitoring ஍ཧతྖҬ؍ଌ ✔ 5 Visit Monitoring ๚໰ઌ௥੻ ✔ 6 Significant Location Change Monitoring େ෯มߋҐஔ৘ใ ✔ Ґஔ৘ใ࣮૷ํ๏ͷ؆୯ͳ·ͱΊ
  15. 4VTQFOEFE /PU3VOOJOH U3VOOJOH ಛ௃ 1 Standard Location ඪ४Ґஔ৘ใ 2 Background

    Location Session όοΫάϥ΢ϯυ ͰͷҐஔ৘ใ 3 Beacon Ranging ϏʔίϯྖҬ؍ଌ ✔ σόΠεઃஔ 4 Geographical Region Monitoring ஍ཧతྖҬ؍ଌ ✔ ೚ҙͷՕॴొ࿥ 5 Visit Monitoring ๚໰ઌ௥੻ ✔ ৔ॴొ࿥ͳ͠ 6 Significant Location Change Monitoring େ෯มߋҐஔ৘ใ ✔ େ͖͘Ҡಈͨ͠ ৔߹ Ґஔ৘ใ࣮૷ํ๏ͷ؆୯ͳ·ͱΊ
  16. ࠓ೔࿩͢಺༰ GeoFenceػೳͱ͸ ɹ1. ಛ௃ͱ੍ݶ ɹ2. ࣮૷ํ๏ ɹ3. Tips΍௚໘ͨ͠໰୊ͳͲ ·ͱΊ

  17. ಛ௃

  18. ಛ௃ 1. SuspendedɺNot Runningͷঢ়ଶͰ΋ݕ஌ಈ࡞ܧଓ

  19. ಛ௃ 1. SuspendedɺNot Runningͷঢ়ଶͰ΋ݕ஌ಈ࡞ܧଓ ɹɹˠ ͭ·ΓΞϓϦ͕ࢮΜͰ͍ͯ΋ݕ஌Մೳʂ

  20. ಛ௃ 1. SuspendedɺNot Runningͷঢ়ଶͰ΋ݕ஌ಈ࡞ܧଓ 2. লόοςϦʔফඅ

  21. ಛ௃ 1. SuspendedɺNot Runningͷঢ়ଶͰ΋ݕ஌ಈ࡞ܧଓ 2. লόοςϦʔফඅ 3. ௥ՃσόΠεෆཁͰ೚ҙͷ஍఺Λొ࿥Մೳ

  22. ੍ݶ

  23. 1. ਫ਼౓ͷݶք : ੍ݶ

  24. 1. ਫ਼౓ͷݶք : ʮςετ໨తͷ৔߹͸ɺ࠷খڑ཭͸໿200ϝʔτϧͰ͋Δ ͱԾఆ͢Δ͜ͱ͕Ͱ͖·͢ɻʯ “Ґஔ৘ใͱϚοϓ ϓϩάϥϛϯάΨΠυ” ΑΓ ੍ݶ

  25. 1. ਫ਼౓ͷݶք : 100m୯Ґ ʮςετ໨తͷ৔߹͸ɺ࠷খڑ཭͸໿200ϝʔτϧͰ͋Δ ͱԾఆ͢Δ͜ͱ͕Ͱ͖·͢ɻʯ “Ґஔ৘ใͱϚοϓ ϓϩάϥϛϯάΨΠυ” ΑΓ ੍ݶ

  26. 1. ਫ਼౓ͷݶք : 100m୯Ґ 2. ݕ஌λΠϛϯάͷ஗͞ : ੍ݶ

  27. 1. ਫ਼౓ͷݶք : 100m୯Ґ 2. ݕ஌λΠϛϯάͷ஗͞ : ʮϢʔβͷҐஔ͕ྖҬͷڥքઢΛԣஅ͠ɺ͜ͷڥքઢ͔Β ࠷খڑ཭͚ͩ཭Ε͔ͯΒɺ͜ͷҐஔΛগͳ͘ͱ΋20ඵؒอ ࣋͠ͳ͍ͱɺ௨஌͸ใࠂ͞Ε·ͤΜɻʯ

    “Ґஔ৘ใͱϚοϓ ϓϩάϥϛϯάΨΠυ” ΑΓ ੍ݶ
  28. 1. ਫ਼౓ͷݶք : 100m୯Ґ 2. ݕ஌λΠϛϯάͷ஗͞ : ʮϢʔβͷҐஔ͕ྖҬͷڥքઢΛԣஅ͠ɺ͜ͷڥքઢ͔Β ࠷খڑ཭͚ͩ཭Ε͔ͯΒɺ͜ͷҐஔΛগͳ͘ͱ΋20ඵؒอ ࣋͠ͳ͍ͱɺ௨஌͸ใࠂ͞Ε·ͤΜɻʯ

    “Ґஔ৘ใͱϚοϓ ϓϩάϥϛϯάΨΠυ” ΑΓ ɹɹIN : ໿10ඵҎ಺ ɹɹOUT : 1ʙ15෼ఔͷ஗Ε ੍ݶ
  29. 1. ਫ਼౓ͷݶք : 100m୯Ґ 2. ݕ஌λΠϛϯάͷ஗͞ : Outݕ஌͸1ʙ15෼ޙ ʮϢʔβͷҐஔ͕ྖҬͷڥքઢΛԣஅ͠ɺ͜ͷڥքઢ͔Β ࠷খڑ཭͚ͩ཭Ε͔ͯΒɺ͜ͷҐஔΛগͳ͘ͱ΋20ඵؒอ

    ࣋͠ͳ͍ͱɺ௨஌͸ใࠂ͞Ε·ͤΜɻʯ “Ґஔ৘ใͱϚοϓ ϓϩάϥϛϯάΨΠυ” ΑΓ ɹɹIN : ໿10ඵҎ಺ ɹɹOUT : 1ʙ15෼ఔͷ஗Ε ੍ݶ
  30. 1. ਫ਼౓ͷݶք : 100m୯Ґ 2. ݕ஌λΠϛϯάͷ஗͞ : Outݕ஌͸1ʙ15෼ޙ 3. ஍఺ొ࿥਺:

    ੍ݶ
  31. 1. ਫ਼౓ͷݶք : 100m୯Ґ 2. ݕ஌λΠϛϯάͷ஗͞ : Outݕ஌͸1ʙ15෼ޙ 3. ஍఺ొ࿥਺:

    1ΞϓϦʹ͖ͭ20Օॴ·Ͱ ੍ݶ
  32. ಛ௃ͱ੍ݶ ಛ௃: ɹ1. SuspendedɺNot Runningͷঢ়ଶͰ΋ݕ஌ಈ࡞ܧଓ ɹ2. লόοςϦʔফඅ ɹ3. ௥ՃσόΠεෆཁͰ೚ҙͷ஍఺Λొ࿥Մೳ ੍ݶ:

    ɹ1. ਫ਼౓ͷݶք : 100m୯Ґ ɹ2. ݕ஌λΠϛϯάͷ஗͞ : Outݕ஌͸1ʙ15෼ޙ ɹ3. ஍఺ొ࿥਺: 1ΞϓϦʹ͖ͭ20Օॴ·Ͱ
  33. ࠓ೔࿩͢಺༰ GeoFenceػೳͱ͸ ɹ1. ಛ௃ͱ੍ݶ ɹ2. ࣮૷ํ๏ ɹ3. Tips΍௚໘ͨ͠໰୊ͳͲ ·ͱΊ

  34. ࣮૷ํ๏

  35. 1. Ґஔ৘ใऔಘڐՄ ࣮૷ํ๏

  36. 1. Ґஔ৘ใऔಘڐՄ ɹɹinfo.plist ɹɹʮNSLocationAlwaysUsageDescriptionʯ௥Ճ ࣮૷ํ๏

  37. // γϯάϧτϯͷϚωʔδϟΫϥε // GeoFenceManager.swift import CoreLocation private let locationManager =

    CLLocationManager() override init() { super.init() self.locationManager.delegate = self // Ґஔ৘ใ࢖༻ڐՄ֬ೝ(Always) self.locationManager.requestAlwaysAuthorization() }
  38. // γϯάϧτϯͷϚωʔδϟΫϥε // GeoFenceManager.swift import CoreLocation private let locationManager =

    CLLocationManager() override init() { super.init() self.locationManager.delegate = self // Ґஔ৘ใ࢖༻ڐՄ֬ೝ(Always) self.locationManager.requestAlwaysAuthorization() }
  39. None
  40. 2. ྖҬ؍ଌ։࢝ ࣮૷ํ๏

  41. // ؍ଌྖҬੜ੒ let coordinate = CLLocationCoordinate2DMake(35.658517,139.701334) let region = CLCircularRegion(center:

    coordinate, radius: 200, identifier: “ौ୩Ӻ”) // ؍ଌ։࢝ self.locationManager.startMonitoringForRegion(region)
  42. 2. ྖҬ؍ଌ։࢝ ࣮૷ํ๏

  43. 3. IN/OUT ݕ஌࣌ͷॲཧΛ࣮૷ ࣮૷ํ๏

  44. // GeoFenceManager.swift // ྖҬʹೖͬͨ௨஌ड৴ func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion)

    { if region is CLCircularRegion { print("GeoFence: In") } }
  45. // GeoFenceManager.swift // ྖҬ͔Βग़ͨ௨஌ड৴ func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion)

    { if region is CLCircularRegion { print("GeoFence: Out") } }
  46. // GeoFenceManager.swift // ྖҬ؍ଌΛऴྃ func stopMonitoringGeoFenceItem(geoFenceItem: GeoFenceItem) { for region

    in self.locationManager.monitoredRegions { if region.identifier == geoFenceItem.identifier { self.locationManager.stopMonitoringForRegion(region) } } }
  47. DEMO 1. ஍఺ͱ൒ܘઃఆ͠ྖҬ؂ࢹ։࢝ 2. ΞϓϦΛkill 3. γϛϡϨʔλͰҐஔಈ࡞ςετ 4. LINE BOT΁௨஌

  48. ࠓ೔࿩͢಺༰ GeoFenceػೳͱ͸ ɹ1. ಛ௃ͱ੍ݶ ɹ2. ࣮૷ํ๏ ɹ3. Tips΍௚໘ͨ͠໰୊ͳͲ ·ͱΊ

  49. ɾҐஔ৘ใ+ϝλ৘ใͷΫϥεΛ࡞ΓUserDefaultsͰอ࣋ ɾSingletonͷManagerΫϥεͰCLLocationManagerΛอ࣋ ɾIdentifierʹ͸UUIDΛઃఆ͍ͯ͠Δ ɾྖҬ؍ଌ։࢝࣌ʹطʹྖҬ಺ʹඋ͑ঢ়ଶݕ஌Λग़͢ ɾॳظ༗ޮ࣌ͷ଴ػ࣌ؒॲཧ ɾNSLogͩͱSystemLogʹ࢒Δ(printͰ͸࢒Βͳ͍) ɾγϛϡϨʔλͰҐஔઃఆ͠։ൃ ɾRegion MonitoringશফڈॲཧΛ࡞͓ͬͯ͘ͱศར ɾΞϓϦফ͢ͱྖҬ؍ଌ͸͢΂ͯফ͑Δ

    Tips
  50. ɾҐஔ৘ใ+ϝλ৘ใͷΫϥεΛ࡞ΓUserDefaultsͰอ࣋ ɾSingletonͷManagerΫϥεͰCLLocationManagerΛอ࣋ ɾIdentifierʹ͸UUIDΛઃఆ͍ͯ͠Δ ɾྖҬ؍ଌ։࢝࣌ʹطʹྖҬ಺ʹඋ͑ঢ়ଶݕ஌Λग़͢ ɾॳظ༗ޮ࣌ͷ଴ػ࣌ؒॲཧ ɾNSLogͩͱSystemLogʹ࢒Δ(printͰ͸࢒Βͳ͍) ɾγϛϡϨʔλͰҐஔઃఆ͠։ൃ ɾRegion MonitoringશফڈॲཧΛ࡞͓ͬͯ͘ͱศར ɾΞϓϦফ͢ͱྖҬ؍ଌ͸͢΂ͯফ͑Δ

    Tips
  51. ɾҐஔ৘ใ+ϝλ৘ใͷΫϥεΛ࡞ΓUserDefaultsͰอ࣋ ɾSingletonͷManagerΫϥεͰCLLocationManagerΛอ࣋ ɾIdentifierʹ͸UUIDΛઃఆ͍ͯ͠Δ ɾྖҬ؍ଌ։࢝࣌ʹطʹྖҬ಺ʹඋ͑ঢ়ଶݕ஌Λग़͢ ɾॳظ༗ޮ࣌ͷ଴ػ࣌ؒॲཧ ɾNSLogͩͱSystemLogʹ࢒Δ(printͰ͸࢒Βͳ͍) ɾγϛϡϨʔλͰҐஔઃఆ͠։ൃ ɾRegion MonitoringશফڈॲཧΛ࡞͓ͬͯ͘ͱศར ɾΞϓϦফ͢ͱྖҬ؍ଌ͸͢΂ͯফ͑Δ

    Tips
  52. ɾྖҬ಺ͰྖҬ؂ࢹ։࢝ͨ͠ࡍʹ࣌ʑࣦഊ͢Δ໰୊ ɾελδΞϜۙྡॅຽ໰୊ ɾྖҬ಺଺ࡏͰ௨஌͕Կ౓΋དྷΔ໰୊ ɾ஍ԼమͰؼΔਓ໰୊ ɾಛٸʹ৐ͬͨΒ௨஌དྷͳ͔ͬͨ໰୊ ɾαʔό΁ͷ௨৴͕஗͍໰୊ ɾొ࿥Օॴ20ݸͷ੍ݶಥഁํ๏ ɾҐஔ৘ใِ૷ͷରࡦ ௚໘ͨ͠໰୊

  53. ɾྖҬ಺ͰྖҬ؂ࢹ։࢝ͨ͠ࡍʹ࣌ʑࣦഊ͢Δ໰୊ ɾελδΞϜۙྡॅຽ໰୊ ɾྖҬ಺଺ࡏͰ௨஌͕Կ౓΋དྷΔ໰୊ ɾ஍ԼమͰؼΔਓ໰୊ ɾಛٸʹ৐ͬͨΒ௨஌དྷͳ͔ͬͨ໰୊ ɾαʔό΁ͷ௨৴͕஗͍໰୊ ɾొ࿥Օॴ20ݸͷ੍ݶಥഁํ๏ ɾҐஔ৘ใِ૷ͷରࡦ ௚໘ͨ͠໰୊

  54. ྖҬ಺଺ࡏͰ௨஌͕Կ౓΋དྷΔ໰୊

  55. ʲݱ৅ʳ ɹ؍ଌྖҬ಺ʹډଓ͚ͨ࣌ɺಥવIN/OUT͕ൃੜͨ͠ɻ ɹෆఆظʹ਺ճൃੜɻ ྖҬ಺଺ࡏͰ௨஌͕Կ౓΋དྷΔ໰୊

  56. ʲݱ৅ʳ ɹ؍ଌྖҬ಺ʹډଓ͚ͨ࣌ɺಥવIN/OUT͕ൃੜͨ͠ɻ ɹෆఆظʹ਺ճൃੜɻ ʲରԠʳ ɹݕ஌ͦͷ΋ͷͷ੍ޚ͸೉͍͠ɻ ɹϢʔεέʔε͔Βߟ͑ͯରԠɻ ɹˠʮ2࣌ؒܦա͍ͯͨ͠Β௨஌ʯ΍ ɹɹʮ1೔ʹ1౓͚ͩ௨஌ʯͳͲϩδοΫͰରԠɻ ྖҬ಺଺ࡏͰ௨஌͕Կ౓΋དྷΔ໰୊

  57. ࠓ೔࿩͢಺༰ GeoFenceػೳͱ͸ ɹ1. ಛ௃ͱ੍ݶ ɹ2. ࣮૷ํ๏ ɹ3. Tips΍௚໘ͨ͠໰୊ͳͲ ·ͱΊ

  58. ·ͱΊ ಛ௃: ɹ1. SuspendedɺNot Runningͷঢ়ଶͰ΋ݕ஌ಈ࡞ܧଓ ɹ2. লόοςϦʔফඅ ɹ3. ௥ՃσόΠεෆཁͰ೚ҙͷ஍఺Λొ࿥Մೳ ੍ݶ:

    ɹ1. ਫ਼౓ͷݶք : 100m୯Ґ ɹ2. ݕ஌λΠϛϯάͷ஗͞ : Outݕ஌͸1ʙ15෼ޙ ɹ3. ஍఺ొ࿥਺: 1ΞϓϦʹ͖ͭ20Օॴ·Ͱ
  59. ·ͱΊ ɾ࣮૷: ಈ͔͢ͷ͸೉͘͠ͳ͍ ɾ࣮ࡍ: ݱ࣮ੈքͰ࢖͓͏ͱ͢Δͱ໰୊ʹ௚໘͢Δɻ ɹɹɹ ࢖͍ํ͕ॏཁʂ·͡Ͱɻ

  60. ·ͱΊ ɾ࣮૷: ಈ͔͢ͷ͸೉͘͠ͳ͍ ɾ࣮ࡍ: ݱ࣮ੈքͰ࢖͓͏ͱ͢Δͱ໰୊ʹ௚໘͢Δɻ ɹɹɹ ࢖͍ํ͕ॏཁʂ·͡Ͱɻ ɹͰ΋ɺ࣮ࡍʹ௨஌͕དྷΔͱخ͍͠ʂ

  61. ੋඇɺΩϨοΩϨͷΞϓϦΛ࡞͍ͬͯͩ͘͞ʂ

  62. ऄ଍: GeoFenceϋοΧιϯ͋ͬͨΒ͜Μͳ෺࡞Γ͍ͨͳҊ ɾεϚʔτϗʔϜ ɹɹམͪࠐΜͩ࣌ʹެԂʹدͬͯؼΔͱՈి͕༏͍͠ ɹɹ؁͍΋ͷɺອը৽ץͷAmazonϦϯΫΛૹ৴ͯ͘͠Δ ɹɹδϜدͬͯؼΔͱϨϕϧΞοϓԻ ɾҰ೔ͷ༧ఆೖΕΔͱྑ͍ײ͡ʹ৔ॴ͕͖͔͚ͬͰϦϚΠϯμ ɾΧʔφϏΞϓϦ: ֶߍɺ͜Ͳ΋Ԃपลͩͱεϐʔυམͱ͢࢓૊Έ ɹɹɹˠɹֶߍपลͰ͸φϏͷ੠Λࢠڙʹมߋ

    ɾҧ͏֗ʹདྷΔͱBGM͕มΘΔ ɹɹɹˠɹ֗ͷ৘ใΛ఻ୡ + ԻָϥΠϒΠϕϯτͱ࿈ಈ ɾGeoFence࿈࠯ͰͷԾ૝ύϯͣ͘φϏήʔγϣϯήʔϜ GeoFenceDemo ιʔείʔυ https://github.com/satoshi0212/GeoFenceDemo
  63. ࢀߟURL ɾҐஔ৘ใͱϚοϓ ϓϩάϥϛϯάΨΠυ https://developer.apple.com/jp/documentation/LocationAwarenessPG.pdf ɾCore Location Best Practices https://developer.apple.com/videos/play/wwdc2016/716/ ɾApple

    Sample Code: Regions: region monitoring, significant location changes, background location service, location service authorization https://developer.apple.com/library/ios/samplecode/Regions/Introduction/ Intro.html#//apple_ref/doc/uid/DTS40010726 ɾRegion Monitoring and iBeacon https://developer.apple.com/library/ios/documentation/UserExperience/ Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html