hopefully someone can help me with this odd problem.
I have a database which stores the Facebook ID of all users who have registered, and this works just fine.
Users can send messages to their Facebook friends through PHP script which is activated via the app.
In my script I have a section which says that if the ID of one of your friends is NOT in the database (i.e. they are not a registered user), then post a message to their Facebook wall notifying them that someone has tried to contact them using the app.
There is then a second database which stores the ID of people we have notified via FB, so that we do not send them multiple requests.
The problem is that the message doesn’t ever appear on their wall. The message is sent to the PHP script as a string like this:
NSString *urlString = [@"myApp/myphp.php?"stringByAppendingString:@"task=send"];
urlString = [urlString stringByAppendingString:@"&facebookID="];
urlString = [urlString stringByAppendingString:[friendsToSendTo objectAtIndex:i]];
urlString = [urlString stringByAppendingString:@"&message="];
urlString = [urlString stringByAppendingString:message];
urlString = [urlString stringByAppendingString:@"&sound="];
urlString = [urlString stringByAppendingString:chosenSoundToSend];
urlString = [urlString stringByAppendingString:@"&sendersName="];
urlString = [urlString stringByAppendingString:myName];
In the PHP script, the task=send part then starts the process of sending a message to the recipient. This part works absolutely fine, the message is received with no problems at all.
If their ID is not found it then moves on to this:
$this->_postToFacebook($sendersName, $facebookID);
The _postToFacebook function looks like this:
private function _postToFacebook($sender, $receiver)
{
require_once "facebook.php";
//registered with developers.facebook for this particular app
$app_id = "****************";
$app_secret = "*****************";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
// Get the url to redirect for login to facebook
// and request permission to write on the user's wall.
$login_url = $facebook->getLoginUrl(
array('scope' => 'publish_stream')
);
// If not authenticated, redirect to the facebook login dialog.
// The $login_url will take care of redirecting back to us
// after successful login.
if (! $facebook->getUser()) {
echo <<< EOT
<script type="text/javascript">
top.location.href = "$login_url";
</script>;
EOT;
exit;
}
// Do the wall post.
$facebook->api("/$receiver/feed", "post", array(
message => "$sender sent you a message",
picture => "image.jpg",
link => "mywebsite.com",
name => "Visit iTunes to download and reply",
caption => "mycaption"
));
}
This is seemingly the part that fails, or at least that was my initial assumption.
However, if I manually enter the generated URL (….task=send&facebookID=12345789…etc) into a browser and hit enter – the message DOES get posted to the users wall.
So now I am completely confused.
When triggering via the browser, the message is received in the app if the Facebook ID is registered and is posted to FB if not.
From the app itself, the message is received when the recipient is registered – but the posting to FB never works.
Presumably I have missed some kind of permission somewhere?
Thanks in advance to anyone who can help 🙂
Edit: In case it is a permissions thing, I should add that I have set these permissions:
if (![facebook isSessionValid]){
NSArray *permissions = [[NSArray alloc] initWithObjects:
@"user_relationships", @"friends_relationships",
@"user_relationship_details", @"friends_relationship_details",
@"read_friendlists",@"offline_access", @"publish_stream",
nil];
[facebook authorize:permissions];
[permissions release];
}
And in the facebook class I had to change these to NO otherwise it doesn’t connect at all, maybe this also causes the problem?
- (void)authorize:(NSArray *)permissions {
self.permissions = permissions;
[self authorizeWithFBAppAuth:NO safariAuth:NO];
}
You may need to encode your URL string. If fields (like name, message, etc.) have spaces or other special characters, the URL request/connection from iOS will fail. Web browsers usually do this encoding for you automatically but iOS doesn’t.
Once you have your URL string, try:
As a side note, you might want to use
[NSString stringWithFormat:@"", ...]rather than multiplestringByAppendingStringcalls. Something like: