我想根据用户ID显示内容。 用户登录到App,成功登录后,他将看到内容取决于从连接外部mysql数据库的JSON文件中检索到的id。
- (void)viewDidLoad { [super viewDidLoad]; NSURL *jsonURL = [NSURL URLWithString:@"http://api.example.com/page.php?user_id=1"]; NSData *jsonData = [NSData dataWithContentsOfURL:jsonURL]; NSError *error = nil; NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; NSLog(@"%@",dataDictionary); self.something = [dataDictionary objectForKey:@"something"]; }
我的问题是:
如何在( user_id = 1 )动态中设置“ 1 ”,在某种程度上,当用户登录到他的帐户时,它会自动更改。
答案 0 :(得分:0)
动态准备网址
NSString *userID = @"23";//Get your userID after login
NSURL *jsonURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.example.com/page.php?user_id=%@",userID];
答案 1 :(得分:0)
你可以使用StringWithFormat
NSString类方法,这个方法可以帮助你创建动态NSString对象,
- (void)viewDidLoad
{
[super viewDidLoad];
NSString userId = @"your user id here";
NSURL *jsonURL = [NSURL URLWithString:[NSString StringWithFormat:@"http://api.example.com/page.php?user_id=%@",userId]];
NSData *jsonData = [NSData dataWithContentsOfURL:jsonURL];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(@"%@",dataDictionary);
self.something = [dataDictionary objectForKey:@"something"];
}