How to mention users with “@” in app like Instagram

旧时模样 提交于 2019-12-20 10:57:12

问题


I am creating an app that has photo sharing, and I am trying to add in the functionality to mention (or tag) users. Twitter is what originally starting using "@" with a username to tag users but now everything from Facebook to Instagram is using it. Is this something that is implemented on the iOS side or in the back end? I am currently programming the back end in Django, but for some reason I think this would be accomplished on the iOS end. There is surprisingly very little information out their on how to mention users in either Django or iOS.

Basically, when creating a post, I'd like to create a user mention by typing "@" followed by a username (with no spaces between) which would display their name in the post like this: @username which would be clickable, leading back to that user's profile. If I am unclear in any way with my question please let me know. Any help, such as pointing me in the right direction of where to start with something like this, would be much appreciated. Thanks! :)


回答1:


Listen to the input on iOS. When the user types an @, followed by a character, make a call to an url on your server (/user/lookup/?username=joe for example) that routes to a view which looks up up users beginning with joe. Return it as json and display it in a dropdown.

Source: How to mention users with "@" in app like Instagram




回答2:


You can try something like this in the UITextView delegate:

- (void)textViewDidChange:(UITextView *)textView
{
    _words = [self.textView.text componentsSeparatedByString:@" "];
    NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[cd] '@'"];
    NSArray* names = [_words filteredArrayUsingPredicate:predicate];
    if (_oldArray)
    {
        NSMutableSet* set1 = [NSMutableSet setWithArray:names];
        NSMutableSet* set2 = [NSMutableSet setWithArray:_oldArray];
        [set1 minusSet:set2];
        if (set1.count > 0)
            NSLog(@"Results %@", set1);
    }
    _oldArray = [[NSArray alloc] initWithArray:names];
}

where _words, _searchResults and _oldArray are NSArrays.



来源:https://stackoverflow.com/questions/13389150/how-to-mention-users-with-in-app-like-instagram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!