Skip to main content

Phương pháp fake location (simulation location) trong Iphone

Việc sử dụng tính năng simulation location trong Xcode , chúng ta có thể fake đc location trong máy iphone dev
Chúng ta có thể sử dụng Simulate Location của Xcode để fake GPS location của Iphone. Location của IOS luôn đc chia sẻ với toàn bộ Appli nên việc fake GPS sẽ làm ảnh hưởng đến các app khóc.

Cách mô phỏng vị trí GPS

1.Sử dụng Xcode để tạo project mới

Nội dung blank sẽ tốt nhất nên ta sử dụng Single View Application để tạo project mới

2. Viết thêm xử lý Background vào trong file AppDelegate.swift

Ta cần share location trong app nhưng Simulate Location thì bị suspend trong app nên chúng ta cần viết thêm xử lý trong phần BackGround ở AppDelegate.swift
import UIKit
import CoreData

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    var backgroundTaskID : UIBackgroundTaskIdentifier = 0

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        self.backgroundTaskID = application.beginBackgroundTaskWithExpirationHandler(){
            [weak self] in
            application.endBackgroundTask((self?.backgroundTaskID)!)
            self?.backgroundTaskID = UIBackgroundTaskInvalid
        }
    }
}
Ta đã thêm phần backgroundTaskID và applicationWillResignActive method vào trong file, các phần còn lại giữ nguyên

3. Sử dụng Device để chạy App

Chọn device là Iphone , và thực hiện run App

4. Sử dụng Simulate Location

Khi app đang running chúng ta chọn Debug -> Simulate Location và chọn để di chuyển đến một thành phố nào đó , ví dụ thử chọn đến London

Sau đó với trạng thái này ta mở thử app Map ra và confirm location đã ở London

5. Fake location tuỳ ý

Để fake location tuỳ ý chúng ta cần tạo file GPX với format XML và chỉ đưa thông tin Lat Long vào bên trong file MyLocation.gpx
<?xml version="1.0"?>
<gpx version="1.1">
    <wpt lat="35.6575811" lon="139.7009455" />
</gpx>
file này tôi đã fake thành vị trí của ga shibuya bên Nhật. Sau đó chọn Debug > Simulate Location > Add GPX File to Project để chọn file GPX vừa rồi
Và để confirm về vị trí mới từ file GPX chúng ta lại mở app Map ra confirm thêm lần nữa

Để tạo file GPX bạn có thể dùng Website www.gpsvisualizer.com

 

Comments

Popular posts from this blog

RxSwift - Giới thiệu

Bạn có nghe qua về Reactive Programming chưa, đây là một khái niệm lập trình hoàn toàn mới. “Reactive Programming là lập trình theo hướng bất đồng bộ các dòng chảy dữ liệu(data streams)” Sau khi đọc khái niệm các bạn có lẽ rất mơ hồ, vậy các data streams là gì. Vậy để mình ví dụ cho các bạn: - Trong lập trình bình thường ( imperative programming ) a = b + c , tức a được gắn với kết quả cộng của b và c .Khi b với c thay đổi thì tất nhiên sẽ không ảnh hưởng tới a, nhưng trong Reactive Programming này a sẽ tự động thay đổi khi b hoặc c thay đổi, kết quả của a sẽ lập tức thay đổi theo. Vậy tại sao nên dùng RXSwift: + Giúp ứng dụng của bạn hoạt động 1 cách như là real time update ui khi có thay đổi data flow, mang đến trải nghiệm tuyệt vời cho User. + Dễ dàng lập trình bất đồng bộ (Async) thay vì phải dùng : callbacks, delegates, notifications, kvo… Bạn phải dùng nhiều thứ gây ra ứng dụng của bạn phức tạp, khó kiểm soát. + Chuyển đổi các thread khi thực hiện 1 cách...

Swift GCD part 1: Thread safe singletons

Preview Singletons are entities, referenced to the same instance of a class from everywhere in your code. It doesn't matter if you like them or not, you will definitely meet them, so it's better to understand how they work. Constructing and handling a set of data doesn't seem to be a big challenge at first glance. The problems appear when you try to optimise the user experience with background work and your app starts acting weird. ??‍♂️ After decades of watching your display mostly with a blank face, you finally realize that your data isn't handled consistently by the manager because you're accessing it (running tasks on it) from multiple threads at the same time. So you really do have to deal with making your singletons thread safe. This article series is dedicated to thread handling using Swift. In the first part below you will get a comprehensive insight into som...

Enum Initializers

In Swift Talk 45 , we wrote a small library for parsing routes. We represented routes as enums, and added a custom initializer to parse routes: enum Route { case home case users(id: Int ) } extension Route { init ?(path: String ) { if path == "/" { self = .home } else if path.hasPrefix( "/users" ) { self = .users(id: 1 ) } else { return nil } } } Of course, this code isn’t complete (see the full public episode for that). Today, we want to focus on assigning to self inside the initializer. In the initializer of any struct or enum we can treat self as a mutable variable (as if it were declared using inout ). This is why we can do the following: enum Result < A > { case success( A ) case error( Error ) } extension Result { init ( _ value: A ) { self = .success(value) } } We can even do this in a struct, although it’s easie...