2012-03-05 24 views
0

我在我的應用程序中通過從其網站下載示例代碼來整合了evernote。它可以正常工作,只是它有一個包含用戶名和密碼靜態值的單例類。這個類的.h文件看起來像這樣iphone中的單身設計模式問題sdk

extern NSString * const username; 
extern NSString * const password; 
@interface Evernote : NSObject { 
    Evernote *sharedEvernoteManager; 
    } 

在.m文件

NSString * const username = @"username"; 
    NSString * const password = @"password"; 

@implementation Evernote 
    /************************************************************ 
    * 
    * Connecting to the Evernote server using simple 
    * authentication 
    * 
    ************************************************************/ 

    - (void) connect { 

     if (authToken == nil) 
     {  
      // In the case we are not connected we don't have an authToken 
      // Instantiate the Thrift objects 
      NSURL * NSURLuserStoreUri = [[[NSURL alloc] initWithString: userStoreUri] autorelease]; 

      THTTPClient *userStoreHttpClient = [[[THTTPClient alloc] initWithURL: NSURLuserStoreUri] autorelease]; 
      TBinaryProtocol *userStoreProtocol = [[[TBinaryProtocol alloc] initWithTransport:userStoreHttpClient] autorelease]; 
      EDAMUserStoreClient *userStore = [[[EDAMUserStoreClient alloc] initWithProtocol:userStoreProtocol] autorelease]; 


      // Check that we can talk to the server 
      bool versionOk = [userStore checkVersion: applicationName :[EDAMUserStoreConstants EDAM_VERSION_MAJOR] : [EDAMUserStoreConstants EDAM_VERSION_MINOR]]; 

      if (!versionOk) { 
       // Alerting the user that the note was created 
       UIAlertView *alertDone = [[UIAlertView alloc] initWithTitle: @"Evernote" message: @"Incompatible EDAM client protocol version" delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil]; 

       [alertDone show]; 
       [alertDone release]; 

       return; 
      } 


      // Returned result from the Evernote servers after authentication 
      EDAMAuthenticationResult* authResult =[userStore authenticate:username :password : consumerKey :consumerSecret]; 

      // User object describing the account 
      self.user = [authResult user]; 
      // We are going to save the authentication token 
      self.authToken = [authResult authenticationToken]; 
      // and the shard id 
      self.shardId = [user shardId]; 

      // Creating the user's noteStore's URL 
      noteStoreUri = [[[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@%@", noteStoreUriBase, shardId] ] autorelease]; 

      // Creating the User-Agent 
      UIDevice *device = [UIDevice currentDevice]; 
      NSString * userAgent = [NSString stringWithFormat:@"%@/%@;%@(%@)/%@", applicationName,applicationVersion, [device systemName], [device model], [device systemVersion]]; 


      // Initializing the NoteStore client 
      THTTPClient *noteStoreHttpClient = [[[THTTPClient alloc] initWithURL:noteStoreUri userAgent: userAgent timeout:15000] autorelease]; 
      TBinaryProtocol *noteStoreProtocol = [[[TBinaryProtocol alloc] initWithTransport:noteStoreHttpClient] autorelease]; 
      noteStore = [[[EDAMNoteStoreClient alloc] initWithProtocol:noteStoreProtocol] retain]; 

     } 
    } 

我需要的用戶名和密碼,動態的,所以我可以從文本框的使用值如下

NSString * const username = usernametextfiled.text; 
    NSString * const password = passwrdfiled.text; 

我收到錯誤,表示我們無法在@implementation之前添加文本字段。如何解決這個問題?

+1

使他們成爲一個@property ......並將它們使用Evernote分享EvernoteManager ..盈利? – Shubhank 2012-03-05 06:19:36

+0

@Shubhank我沒有得到你,你能解釋一下嗎? – stackiphone 2012-03-05 06:28:25

回答

2
@interface Evernote : NSObject 
... 


@property(retain) NSString * username; 

@property(retain) NSString * password; 

然後

@implementation Evernote 


@synthesize username; 
@synthesize password; 

,那麼你可以將它們用

[[Evernote sharedEvernoteManager]setusername:yourvariable]; 
[[Evernote sharedEvernoteManager]setpassword:yourvariable]; 

千萬記得要釋放該變量在dealloc

+0

haii,我在哪裏放置了代碼[[Evernote sharedEvernoteManager] setusername:yourvariable]; [[Evernote共享的EvernoteManager] setpassword:yourvariable];謝謝 – stackiphone 2012-03-05 09:13:22

+0

無論你得到你想要的用戶名......因此可以說你在這裏有一個視圖的文本框用戶輸入evernote名稱......你把它放在那裏。 – Shubhank 2012-03-05 09:29:33