2016-11-23 8 views
0

私のアプリは復元購入後にサーバーに領収書を送信します。 To辞書には、「bundleId」(アプリバンドルID)、「UUID」(アプリIDForVendor)の2つの追加キーが追加されます。 承認され、最初に実行された後、アプリケーションのすべてが正常です(すべてのキーを取得した後に復元した後)。ユーザーがアプリケーションを削除して再インストールすると、これらのキーにはnull値が設定されます。parson jsonの後のヌル値

現在appStoreReceiptを取得:

if(!self.receiptData){ 

    NSURL *receiptURL = [[NSBundle mainBundle] 
         appStoreReceiptURL]; 
self.receiptData = [NSData 
      dataWithContentsOfURL:receiptURL] 
receipt = [self.receiptData bkrBase64EncodedString]; 
    } 
    else{ 
     receipt = [self.receiptData bkrBase64EncodedString]; 
    } 

アップルの要求:

if(receipt){ 
    NSError *error; 
    NSDictionary *requestContents = @{ 
             @"receipt-data" : receipt, 
             @"password" : //purchaseAppSecreatKey 
             }; 
    NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents 
                  options:0 
                  error:&error]; 

    if (!requestData) { /* ... Handle error ... */ 
    } 
    // Create a POST request with the receipt data. 
    NSURL *storeURL = ///iTunesVerificationURL 

    NSMutableURLRequest *storeRequest = 
    [NSMutableURLRequest requestWithURL:storeURL]; 
    [storeRequest setHTTPMethod:@"POST"]; 
    [storeRequest setHTTPBody:requestData]; 

    // Make a connection to the iTunes Store on a background queue. 
    NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init]; 
    [NSURLConnection 
    sendAsynchronousRequest:storeRequest 
    queue:operationQueue 
    completionHandler:^(NSURLResponse *response, NSData *data, 
         NSError *connectionError) { 

     if (connectionError) { 
     NSLog(@"response error %@", connectionError.localizedDescription); 

     } else { 

      NSError *error; 
      NSDictionary *jsonResponse = 
      [NSJSONSerialization JSONObjectWithData:data 
               options:0 
               error:&error] ; 

      if(!error){ 
       //success, sending to server 
      }else{ 
       NSLog(@"parse error %@", error.localizedDescription); 
      } 
     } 
    }]; 
} 

NSMutableDictionary *requestBodyDictonary = [NSMutableDictionary dictionaryWithDictionary:reciptDic]; 
[requestBodyDictonary setObject:[self bundleId] forKey:@"bundleId"]; 
[requestBodyDictonary setObject:[self UUID] forKey:@"uuid"]; 

NSURL *url = [NSURL URLWithString:]; 
NSError *error = nil; 
NSData *bodyData = [NSJSONSerialization dataWithJSONObject:requestBodyDictonary options:0 error:&error]; 

if(error == nil){ 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] 
            initWithURL:url 
            cachePolicy:NSURLRequestUseProtocolCachePolicy 
            timeoutInterval:15.0]; 

    [request setHTTPMethod:@"POST"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 


    [request setHTTPBody:bodyData]; 

    NSURLSession *defaultstSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 

    NSURLSessionDataTask *task = [defaultstSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 
     NSLog(@"complete %ld", (long)[(NSHTTPURLResponse *)response statusCode]); 
    }]; 

    [task resume]; 

}else{ 

    NSLog(@"error parshe %@", [error localizedDescription]); 
} 

にサーバーコードにフェッチUUIDを送信し、

#pragma mark - User ID 
-(NSString *)UUID{ 

    if(!_UUID){ 
     _UUID = [[NSUserDefaults standardUserDefaults] stringForKey:@"identifierForVendor_UUID"]; 
     if(!_UUID){ 
      _UUID = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; 
      [[NSUserDefaults standardUserDefaults]setObject:_UUID forKey:@"identifierForVendor_UUID"]; 
      [[NSUserDefaults standardUserDefaults]synchronize]; 
     } 
    } 

    return _UUID; 
} 

#pragma mark bundle id 
-(NSString *)bundleId{ 

    if(!_bundleId){ 
     _bundleId = [[NSUserDefaults standardUserDefaults] stringForKey:@"app_BundleId"]; 

     if(!_bundleId){ 

      _bundleId = [[NSBundle mainBundle]bundleIdentifier]; 
      if(!_bundleId){ 
       _bundleId = (__bridge_transfer NSString *)CFDictionaryGetValue(CFBundleGetInfoDictionary(CFBundleGetMainBundle()), 
                       (const void *)(@"CFBundleIdentifier")); 
      } 
      [[NSUserDefaults standardUserDefaults] setObject:_bundleId forKey:@"app_BundleId"]; 
      [[NSUserDefaults standardUserDefaults] synchronize]; 
     } 
    } 

    return _bundleId; 
} 
をbundleid

なぜ私のアプリは再インストール後にnullを返しますか? サンドボックスモードですべてOKです

+1

ヌルを返すものは何ですか? NSUserDefaultsに関連している場合は、再インストール後に完全に削除されたことを知っている必要があります。 –

+0

@IulianOnofrei私はそれを知っていますが、もしNSUserDefaultsからの値がnilなら、私はもう一度フェッチして、それをNSUserDefaultsに格納します。最初のバージョンのアプリケーションではNSUserDefaultsを使用していませんが、まだnullがあります – quba88

答えて

0

あなたがAPPを削除すると、すべてのNSUserDefault値もクリアされます。キーチェーンを使用して値を保存する必要があります。アプリを削除すると同じままです。

使用KeychainItemWrapperファイルが

輸入 "KeychainItemWrapper.h"

その後、設定値は次のように行うgithubのからUDIDとBundleID

両方ダウンロードkeychanItemwrapperを保存するために:

UIDevice *デバイス= [UIDevice currentDevice];

NSString * div = [[device identifierForVendor] UUIDString];

keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@ "TestUDID" accessGroup:nil];

userDevice_id = [keychain objectForKey :(__ bridge id)(kSecAttrAccount)];

if([userDevice_id isEqualToString:@""]) 

{ 

チェーン= [[KeychainItemWrapper ALLOC] initWithIdentifier: "TestUDID" accessGroup @:なし]。

[keychain setObject:div forKey:(__bridge id)(kSecAttrAccount)]; 

}

変数から値を得る:

チェーン= [[KeychainItemWrapper ALLOC] initWithIdentifier: "TestUDID" accessGroup @:なし]。

userDevice_id = [keychain objectForKey:(__bridge id)(kSecAttrAccount)]; 
+0

NSUserDefaultの値がnilの場合、私は '[[NSBundle mainBundle] bundleIdentifier];'を使用して値を取り出し、NSUserDefaultに格納します。私のアプリの最初のバージョンはNSUserDefaultを使用せず、私のサーバーからの息子を解析した後にもnullが残っています。 – quba88

関連する問題