我想我的viewController名爲「DoctorInformationController」只有當用戶第一次啓動應用程序時出現。我見過一些人的答案,但我想知道如何迅速做到這一點,請!有ViewController只出現在第一次啓動
謝謝
我想我的viewController名爲「DoctorInformationController」只有當用戶第一次啓動應用程序時出現。我見過一些人的答案,但我想知道如何迅速做到這一點,請!有ViewController只出現在第一次啓動
謝謝
有沒有辦法知道它是建立在UIKit框架的首次發射,但你可以做的另一種方式。
在AppDelegate.swift中,轉到applicationDidFinishLaunchingWithOptions方法(通常是第一個方法)。現在,請添加:
//Check for key "first_launch" in User Defaults
if let _ = NSUserDefaults.standardUserDefaults().objectForKey("first_launch") {
//Set your own global variable to be true. Then, when your ViewController
//loads, do a popup window with that DoctorViewController thingy if the
//variable is true
//Example:
isFirstLaunch = true
//Then, set "first_launch" to be a value so your app will never call this block again
NSUserDefaults.standardUserDefaults().setObject("", forKey: "first_launch")
}
爲什麼不使用'setBoolForKey:'? – nielsbot
@nielsbot無所謂,只要有東西在那裏。我不檢查它是否屬實,我正在檢查它是否存在。同樣的區別,對吧? – brimstone
我添加了這個,我似乎在if語句中得到一個錯誤:「條件綁定的初始化必須有可選類型,而不是'string'」。我該如何解決這個問題,對不起新的快速! – OriginalAlchemist
我使用的代碼將基於應用程序的第一次或第二次啓動啓動viewcontoller。
This in the AppDelegate。
var defaults: NSUserDefaults
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let firstLaunch = defaults.boolForKey("FirstLaunch")
if firstLaunch {
print("Not first launch.")
} else {
}
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewControllerWithIdentifier("navController") as! UINavigationController
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
else {
print("First launch, setting NSUserDefault.")
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewControllerWithIdentifier("firstrunVCstoryboard") as UIViewController
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
return true
}
你需要去適應它,以適應你的故事板和viewcontrollers名。
編輯:
這怎麼我在第一個視圖控制器設置的值,表明首次運行已完成。所以它不會再顯示。
self.defaults.setBool(true, forKey: "FirstLaunch")
嘿!謝謝!此代碼是否調整爲在第二次啓動時運行?因爲我只是需要它的第一個,那就是它。 – OriginalAlchemist
它每次運行,如果第一次啓動已被記錄,則它會加載第二次啓動的視圖控制器。我使用這種格式,以便在需要檢查某些內容時觸發首次啓動屏幕。 – MwcsMac
只需發送一個GET請求到
http://ip-api.com/json
,它會以飽滿設備地理位置數據,包括IP地址進行響應。
{
"status": "success",
"country": "COUNTRY",
"countryCode": "COUNTRY CODE",
"region": "REGION CODE",
"regionName": "REGION NAME",
"city": "CITY",
"zip": "ZIP CODE",
"lat": LATITUDE,
"lon": LONGITUDE,
"timezone": "TIME ZONE",
"isp": "ISP NAME",
"org": "ORGANIZATION NAME",
"as": "AS NUMBER/NAME",
"query": "IP ADDRESS USED FOR QUERY"
}
我將在'NSUserDefaults'中使用首選項來跟蹤工作表是否顯示在前面。您可以決定在您的應用程序委託中顯示工作表,方法applicationDidFinishLaunching() – nielsbot
好吧!你介意用一些示例代碼回答這個問題嗎? – OriginalAlchemist
@OriginalAlchemist如果您發現答案可以標記爲最佳或顯示您是如何完成這項任務的? – brimstone