2012-12-04 38 views
1

我們開發了iphone應用程序,供差異員工使用。組織(實際上我們沒有製作它的企業應用程序,相反我們打算把這個應用程序放在應用程序商店付費),因此,我們想要的是什麼時候該人將安裝該應用程序,並且該應用程序將第一次打開,一個彈出窗口會出現在那裏,詢問輸入URL(這將是指向該組織的服務器的URL),因此,通過代碼邏輯,我想要的是何時我們將訪問視圖中的登錄頁面加載我想檢查URL是否已經存儲在應用程序中,如果它沒有存儲,那麼我會給那個彈出詢問輸入URL,如果它存儲然後只是我會讓他輸入登錄細節。如何將數據存儲到iPhone應用程序

我的問題是,我應該如何存儲該URL,我應該在哪裏存儲該URL以及我應該如何檢查是否已存儲該URL。我試圖將該URL存儲在... urlfield.text是我要存儲的URL。

NSError *error; 
    // For error information 

    // Create file manager 
    fileMgr = [NSFileManager defaultManager]; 

    // Point to Document directory 
    documentsDirectory = [NSHomeDirectory() 
          stringByAppendingPathComponent:@"Documents"]; 
    fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath]; 
    // File we want to create in the documents directory 
    // Result is: /Documents/file1.txt 
    filePath = [documentsDirectory 
       stringByAppendingPathComponent:@"file1.txt"]; 
    str=urlField.text; 
    // Write the file 
    [str writeToFile:filePath atomically:YES 
      encoding:NSUTF8StringEncoding error:&error]; 

    // Show contents of Documents directory 
    NSLog(@"Documents directory: %@", 
      [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]); 

和我檢查它的存在或者不使用是否..

// fileExists is a BOOL variable 
      fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath]; 
     NSLog(@"Boolean value:=%d",fileExists); 
     if (fileExists == 0) 
     { 
      ....... 
      ........ 
     } 

但每次我運行它返回「0」的應用程序(是指布爾變量FILEEXISTS)。 請有人告訴我完成我的任務的方式,我最終會用這個邏輯來嘗試所有的一面。

+0

在檢查文件是否存在之前檢查字符串「filePath」。 –

+0

在檢查之前是否設置了字符串「filePath」? –

+0

@ anusha:感謝您的更新,實際上我解決了它的幫助下「NSUserDefaults」 – iShwar

回答

5

我爲此使用NSUserDefaults。

第一時用於檢查用戶是否被打開的應用程序第一次檢查值NSUserDefault的第一個目的等

//defaults is a object of NSUserDefault 
NSString *URLEntered =[defaults objectForKey:@"URLISENTERED"]; 
if (!URLEntered) { 

     [defaults setValue:@"0" forKey:@"URLISENTERED"]; 
    } 

的URLEntered返回在​​第一次爲空,所以我設定的值0對於關鍵URLISENTERED

,我允許用戶內的進入有機構服務器, 的URL我設置VALU關鍵URLISENTERED爲1,像

[defaults setValue:@"1" forKey:@"URLISENTERED"]; 

,並在同一時刻i採取NSUserDefault之一多個對象來存儲服務器的URL和存儲在像

// defaultURL is the object of NSUserDefault, and main URL is the URL of server user entered 

[defaultURL setValue:mainURL forKey:@"mainURL"]; 

所以下次當用戶再次登錄到應用程序的

[defaults objectForKey:@"URLISENTERED"] 

是1,因爲我們設定和時刻i導航用戶直接登錄屏幕,並已取出使用

mainURL=[defaultURL objectForKey:@"mainURL"]; 

的mainURL的值和使用該供進一步使用的URL。

0

其實如果你想檢查客戶端的URL。你可以使用sqlite數據庫來完成這個任務。

+0

雅Mohd我們可以使用sqlite,但我發現它有些複雜,因爲我不太熟悉sqlite,所以我用NSUserDefault及其完成。 – iShwar

1

正如我理解您的問題您可以使用NSUserDefault的plist

  1. 使用的plist:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"plist.plist"]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    
    if (![fileManager fileExistsAtPath: path]) 
    { 
        path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"plist.plist"] ]; 
    } 
    
    NSMutableDictionary *data; 
    NSString *userURL ; 
    
    if ([fileManager fileExistsAtPath: path]) 
    { 
          data = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; 
          //To retrieve the data from the plist 
          NSMutableDictionary *savedURL= [[NSMutableDictionary alloc] initWithContentsOfFile: path]; 
    
    userURL = [savedURL objectForKey:@"USER_URL"]; 
    NSLog(@"%@",userURL); 
         // present login detail or more stuff with userURL 
    } 
    else 
    { 
    
        userURL = // prompt for user to enter URL 
        data = [[NSMutableDictionary alloc] init]; 
        [data setObject:[NSNumber numberWithInt:value] forKey:@"USER_URL"]; 
        [data writeToFile: path atomically:YES]; 
    
    } 
    
  2. 使用NSUserDefault:

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    NSString *userURL = [defaults objectForKey:@"USER_URL"]; 
    if(userURL) { 
         NSLog(@"%@",userURL); 
         //present login detail or more stuff with userURL 
    } 
    else { 
    userURL = // prompt for userURL 
    [defaults setObject:userURL forKey:@"USER_URL"]; 
    } 
    
相關問題