Open link to Facebook page from iOS app

前端 未结 8 1121
孤独总比滥情好
孤独总比滥情好 2020-11-30 18:29

I want to redirect user to my App\'s Facebook page, so I have the following code:

[[UIApplication sharedApplication] openURL:
    [NSURL URLWithString: @\"ht         


        
8条回答
  •  伪装坚强ぢ
    2020-11-30 18:56

    After much testing I have found one of the most effective solutions:

    NSString *facebookID = @"XXXXXXXXXX";
    NSString *facebookURL = @"www.facebook.com/XXXXXXXX";
    NSURL *urlModal = [NSURL URLWithString:[NSString stringWithFormat:@"fb://facewebmodal/f?href=%@", facebookURL]];
    NSURL *urlWithID = [NSURL URLWithString:[NSString stringWithFormat:@"fb://page/%@", facebookID]]; // or "fb://profile/%@" or another type of ID
    NSURL *url = [NSURL URLWithString:facebook_url];
    
    
    if(facebookID && !facebookID.isEmpty && [[UIApplication sharedApplication] canOpenURL:urlWithID]) {
        // open facebook app with facebookID
        [[UIApplication sharedApplication] openURL:urlWithID];
    } else if (facebookURL && !facebookURL.isEmpty && [[UIApplication sharedApplication] canOpenURL:urlModal]) {
        // open facebook app with facebookUrl
        [[UIApplication sharedApplication] openURL:urlModal];
    } else if (![[UIApplication sharedApplication] openURL:url]) {
        // somehow open
        NSLog(@"%@%@",@"Failed to open url:",[url description]);
    }
    

    Order of sentences "if else" vary to your liking ...

    Enjoy it!! :-)

提交回复
热议问题