2014-06-20 101 views
0

Dropbox文檔默認解釋了身份驗證的響應會被觸發到Appdelegate.m中 如何讓我自己的類的委託相同?處理Dropbox身份驗證響應

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url 
    sourceApplication:(NSString *)source annotation:(id)annotation { 
    if ([[DBSession sharedSession] handleOpenURL:url]) { 
     if ([[DBSession sharedSession] isLinked]) { 
      NSLog(@"App linked successfully!"); 
      // At this point you can start making API calls 
     } 
     return YES; 
    } 
    // Add whatever other url handling code your app requires here 
    return NO; 
} 
+0

我不認爲你有一個選擇「openURL」的實現位置。我相信它必須是你的'UIApplication'類的成員(通常在名爲AppDelegate.m的文件中)。 – smarx

回答

0

enter image description here在信息的plist URL類型 - >在URL方案只需添加DB-YourAppKey這種方法將被調用。

此方法自動調用。我希望你已經從Dropbox開發者網站創建應用程序,並獲得appKey和appSecret。在應用程序委託中使用此代碼NSString * appKey = @「」;

NSString* appSecret = @""; 
     NSString *root = kDBRootDropbox; 
     NSString* errorMsg = nil; 
     if ([appKey rangeOfCharacterFromSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]].location != NSNotFound) { 
      errorMsg = @"Make sure you set the app key correctly in DBRouletteAppDelegate.m"; 
     } else if ([appSecret rangeOfCharacterFromSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]].location != NSNotFound) { 
      errorMsg = @"Make sure you set the app secret correctly in DBRouletteAppDelegate.m"; 
     } else if ([root length] == 0) { 
      errorMsg = @"Set your root to use either App Folder of full Dropbox"; 
     } else { 
      NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"]; 
      NSData *plistData = [NSData dataWithContentsOfFile:plistPath]; 
      NSDictionary *loadedPlist = 
      [NSPropertyListSerialization 
      propertyListFromData:plistData mutabilityOption:0 format:NULL errorDescription:NULL]; 
      NSString *scheme = [[[[loadedPlist objectForKey:@"CFBundleURLTypes"] objectAtIndex:0] objectForKey:@"CFBundleURLSchemes"] objectAtIndex:0]; 
      if ([scheme isEqual:@"db-APP_KEY"]) { 
       errorMsg = @"Set your URL scheme correctly in DBRoulette-Info.plist"; 
      } 
     } 

     DBSession* session = 
     [[DBSession alloc] initWithAppKey:appKey appSecret:appSecret root:root]; 
     session.delegate = self; // DBSessionDelegate methods allow you to handle re-authenticating 
     [DBSession setSharedSession:session]; 
     [DBRequest setNetworkRequestDelegate:self]; 
     // [[DBSession sharedSession]unlinkAll]; 

     if ([[DBSession sharedSession] isLinked]) 
     { 
      isAccountForDropBox = YES; 
     } 
     else{ 
      isAccountForDropBox = NO; 
     } 

//使用這個打開的URL會得到自動調用後。

+0

是的。希望我在Dropbox創建了應用程序,並且我得到了App-key,Secret key,我用在了我的Appdelegate.m中,但是如何在自定義類上實現您的代碼。你可以幫我嗎。 ? – iTag

+0

我已經遵循他們的文件工作正常。但我的要求是我需要使用這些到我的自定義類。而已。 – iTag

+0

在信息plist url類型 - >在url方案只是添加db-YourAppKey這個方法將得到調用不需要應用程序委託代碼也。我已經更新文件,只是檢查它。 – user3663584

0

這些方法只在AppDelegate.m中響應,不能在外部使用。 要在您的視圖控制器或任何類中使用時,應使用後通知

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url 
    sourceApplication:(NSString *)source annotation:(id)annotation { 
    if ([[DBSession sharedSession] handleOpenURL:url]) { 
     if ([[DBSession sharedSession] isLinked]) { 
      NSLog(@"App linked successfully!"); 
      // Post Notify here 
      [[NSNotificationCenter defaultCenter] postNotificationName:@"applicationDidLinkWithDropbox" object:self]; 

     } 
     return YES; 
    } 
    // Add whatever other url handling code your app requires here 
    return NO; 
} 

然後在你的類收到此通知,在一個視圖控制器,例如:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
[[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(dropBoxDidLink:) 
                name:@"applicationDidLinkWithDropbox" 
                object:nil]; 
} 
- (void) dropBoxDidLink:(NSNotification *)notification { 
    if ([[notification name] isEqualToString:@"applicationDidLinkWithDropbox"]) { 
    //Handle your task here 

    } 
}