iOS集成Facebook登录点击Continue后崩溃,报NSInvalidArgumentException异常求助
Hey Daniel, let's break down this crash and get your Facebook login working smoothly again.
Root Cause Analysis
From your crash log, the key error line tells us everything we need to know:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:' -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[3]'
This means in your -[ViewController Callfb_LoginService:] method, you're trying to create an NSDictionary where the 4th element (index 3) in your objects array is nil. NSDictionaries don't allow nil values for keys or values, so this triggers an immediate crash.
Since the crash happens right after getting Facebook user details (seen in __33-[ViewController GetFBUserDetail]_block_invoke), the nil value is almost certainly coming from a field you're trying to pull from the Facebook Graph API response—something like email, birthday, or another permission-based field that the user didn't grant access to, or Facebook didn't return.
Step-by-Step Fixes
1. Locate the Dictionary Creation Code
First, find the section in Callfb_LoginService: where you're creating a dictionary with dictionaryWithObjects:forKeys:count: or a similar method. It'll look something like this:
NSArray *keys = @[@"user_id", @"name", @"profile_pic", @"email"]; NSArray *objects = @[fbUserId, fbUserName, fbProfilePic, fbEmail]; NSDictionary *loginParams = [NSDictionary dictionaryWithObjects:objects forKeys:keys count:4];
In this example, fbEmail (the 4th element, index 3) is the nil value causing the crash.
2. Handle Potential Nil Values
You have two solid options to fix this:
Option A: Use Default Values for Nil Fields
Replace any potentially nil values with a safe fallback (like an empty string):
NSString *fbEmail = [fbUserDetails objectForKey:@"email"] ?: @""; // Use empty string if nil NSString *fbBirthday = [fbUserDetails objectForKey:@"birthday"] ?: @"N/A";
Option B: Filter Out Nil Key-Value Pairs
Use a mutable dictionary to only add non-nil entries:
NSMutableDictionary *loginParams = [NSMutableDictionary dictionary]; if (fbUserId) [loginParams setObject:fbUserId forKey:@"user_id"]; if (fbUserName) [loginParams setObject:fbUserName forKey:@"name"]; if (fbProfilePic) [loginParams setObject:fbProfilePic forKey:@"profile_pic"]; if (fbEmail) [loginParams setObject:fbEmail forKey:@"email"];
3. Add Debug Logs to Confirm the Issue
To double-check which field is nil, add a log right before creating the dictionary:
NSLog(@"FB User Data - ID: %@, Name: %@, Pic: %@, Email: %@", fbUserId, fbUserName, fbProfilePic, fbEmail);
This will print the values to your console, making it crystal clear which one is coming up nil.
内容的提问来源于stack exchange,提问作者DanielB

