I need to attach a description or status when users share photos videos to apps this is the code I have right now:
for video:
let fbVideo: FBSDKShareVideo = FBSDKShareVideo()
fbVideo.videoURL = self.videoUrl!
var content: FBSDKShareVideoContent = FBSDKShareVideoContent()
content.video = fbVideo
FBSDKShareAPI.shareWithContent(content, delegate: nil)
and for images:
let photo : FBSDKSharePhoto = FBSDKSharePhoto()
photo.image = self.image
photo.userGenerated = true
var content: FBSDKSharePhotoContent = FBSDKSharePhotoContent()
content.photos = [photo]
FBSDKShareAPI.shareWithContent(content, delegate: nil)
can't seem a way to attach the text status to the FBSDKShareContent
Related
I am currently trying to enable the functionality to share a video through Facebook, however the share dialog does not show up.
Here's the code:
let video = ShareVideo(videoURL: Bundle.main.url(forResource: "Rap God", withExtension: "mp4")!)
let content = ShareVideoContent()
content.video = video
content.contentURL = Bundle.main.url(forResource: "Rap God", withExtension: "mp4")!
let dialog = ShareDialog(fromViewController: self, content: content, delegate: self)
dialog.shareContent = content
dialog.shouldFailOnDataError = true
dialog.mode = .shareSheet
dialog.fromViewController = self
dialog.show()
print(dialog.canShow)
print(dialog.canShow) is here equal to "false". Could please tell me how I could fix this?
I've been working on rich notification experience which has been introduced in iOS10 and stuck with passing images as attachments to UNNotificationContentExtension.
Here's my ContentExtension:
class NotificationViewController: UIViewController, UNNotificationContentExtension {
#IBOutlet weak var attachmentImage: UIImageView!
func didReceive(_ notification: UNNotification) {
if let attachment = notification.request.content.attachments.first {
if attachment.url.startAccessingSecurityScopedResource() {
attachmentImage.image = UIImage(contentsOfFile: attachment.url.path)
attachment.url.stopAccessingSecurityScopedResource()
}
}
}
}
As a tutorial, I've been following Advanced Notifications video from WWDC.
I've checked - UIImage I'm assigning to UIImageView:
is not nil
has proper CGSize (191x191)
attachment.url.path equals /var/mobile/Library/SpringBoard/PushStore/Attachments/<bundle of app>/<...>.png
Here's how I send local notification from the app:
let content = UNMutableNotificationContent()
content.title = "Sample title"
content.body = "Sample body"
content.categoryIdentifier = "myNotificationCategory"
let attachement = try! UNNotificationAttachment(identifier: "image",
url: Bundle.main.url(forResource: "cat", withExtension: "png")!,
options: nil)
content.attachments = [ attachement ]
let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: nil)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().add(request){(error) in
if (error != nil){
}
}
"cat.png" is just a dummy resource I've added to proj.
As you can see, notification shows the pic, so I assume, that I'm sending it correctly, but in the expanded state(in NotificationViewController) I've never succeed at showing the same image.
What am I doing wrong?
Thanks!
When you create an UIImage with contentsOfFile, the UIImage object reads the image header only, which indicates basic info, such as image size, etc.
So, try move stopAccessingSecurityScopedResource to [NotificationViewController dealloc].
Or using following:
objective-c code:
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
swift code:
let imageData = NSData(contentsOf: attachment.url)
let image = UIImage(data: imageData! as Data)
There is no document saying that contentsOfFile only reads the image header. But when I run the following code:
NSString *docFolderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *pngPath = [docFolderPath stringByAppendingPathComponent:#"test.png"];
UIImage *image = [UIImage imageWithContentsOfFile:pngPath];
[[NSFileManager defaultManager] removeItemAtPath:pngPath error:nil];
imageView.image = image;
An error occurs:
ImageIO: createDataWithMappedFile:1322: 'open' failed '/Users/fanjie/Library/Developer/CoreSimulator/Devices/FFDFCA06-A75E-4B54-9FC2-8E2AAE3B1405/data/Containers/Data/Application/E2D26210-4A53-424E-9FE8-D522CFD4FD9E/Documents/test.png'
error = 2 (No such file or directory)
So I made a conclusion that UIImage contentsOfFile only reads the image header.
Thanks to #jeffery ,
Here is the exact code for the image shown in the notification extension:
if let attachment = notification.request.content.attachments.first {
if attachment.url.startAccessingSecurityScopedResource() {
let data = NSData(contentsOfFile: attachment.url.path);
self. attachmentImage?.image = UIImage(data: data! as Data);
attachment.url.stopAccessingSecurityScopedResource()
}
}
I want to share image on Facebook, for this I do:
let photo: FBSDKSharePhoto = FBSDKSharePhoto()
photo.image = croppedImage
photo.userGenerated = true
photo.caption = "Add Your caption"
let content: FBSDKSharePhotoContent = FBSDKSharePhotoContent()
content.photos = [photo]
FBSDKShareDialog.showFromViewController(self, withContent: content, delegate: nil)
but it does not show me ShareViewController. But when I'm trying this code:
let content: FBSDKShareLinkContent = FBSDKShareLinkContent()
content.contentURL = NSURL(string: "http://")
content.contentTitle = "App Testing"
content.contentDescription = "I'm working over the app!"
How can I fix the first snippet?
I know this is an old question but just in case anyone faces the same issue:
According to the documentation, You need the native Facebook app to be installed in order to use FBSDKSharePhotoContent and you cannot use FBSDKShareDialog in that case.
A close workaround to achieve almost the same result is using FBSDKShareDialog (which only supports FBSDKShareLinkContent) as follows:
let content: FBSDKShareLinkContent = FBSDKShareLinkContent()
content.imageURL = URL(string: "www.example.com/my_image.jpg")
content.contentURL = URL(string: "www.example.com/target_url")
content.contentTitle = "My Title"
content.contentDescription = "My Description"
let dialog: FBSDKShareDialog = FBSDKShareDialog()
dialog.fromViewController = self
dialog.delegate = self // make sure to conform to FBSDKSharingDelegate protocol
dialog.shareContent = content
if !dialog.canShow() {
// fallback to non-native mode
dialog.mode = .feedBrowser
}
dialog.show()
You can find the protocol's methods here.
It only works when Facebook app is installed. Also, specify mode = .native explicitly.
let content = FBSDKSharePhotoContent()
let photo = FBSDKSharePhoto(image: image, userGenerated: true)
content.photos = [photo].flatMap({ $0 })
let dialog = FBSDKShareDialog()
dialog.fromViewController = viewController
dialog.shareContent = content
dialog.delegate = self
dialog.mode = .native
dialog.show()
If not, it will use SLComposeViewController, and you will get error
SLComposeViewController isAvailableForServiceType got serviceType com.apple.social.facebook isAvailable 0
Good practice is to debug on FBSDKShareDialog.m to see if things don't work for you
- (BOOL)show
{
BOOL didShow = NO;
NSError *error = nil;
if ([self _validateWithError:&error]) {
switch (self.mode) {
case FBSDKShareDialogModeAutomatic:{
didShow = [self _showAutomatic:&error];
break;
}
case FBSDKShareDialogModeBrowser:{
didShow = [self _showBrowser:&error];
break;
}
case FBSDKShareDialogModeFeedBrowser:{
didShow = [self _showFeedBrowser:&error];
break;
}
case FBSDKShareDialogModeFeedWeb:{
didShow = [self _showFeedWeb:&error];
break;
}
case FBSDKShareDialogModeNative:{
didShow = [self _showNativeWithCanShowError:&error validationError:&error];
break;
}
case FBSDKShareDialogModeShareSheet:{
didShow = [self _showShareSheetWithCanShowError:&error validationError:&error];
break;
}
case FBSDKShareDialogModeWeb:{
didShow = [self _showWeb:&error];
break;
}
}
}
if (!didShow) {
[self _invokeDelegateDidFailWithError:error];
} else {
[self _logDialogShow];
[FBSDKInternalUtility registerTransientObject:self];
}
return didShow;
}
Lesson learned:
Be aware of FB changes.
Check for change log every morning
Things that work today may not work tomorrow
I use google maps api for iOS. I want to get static image of special city and paste it in UIImageView. How can I make it?
the reply of #Ankit is right, but #Alexsander asked in Swift, so :
var staticMapUrl: String = "http://maps.google.com/maps/api/staticmap?markers=color:blue|\(self.staticData.latitude),\(self.staticData.longitude)&\("zoom=13&size=\(2 * Int(mapFrame.size.width))*\(2 * Int(mapFrame.size.height))")&sensor=true"
var mapUrl: NSURL = NSURL(string: staticMapUrl.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding))!
var image: UIImage = UIImage.imageWithData(NSData.dataWithContentsOfURL(mapUrl))
var mapImage: UIImageView = UIImageView(frame: mapFrame)
for swift 4
let staticMapUrl: String = "http://maps.google.com/maps/api/staticmap?markers=\(self.finalLatitude),\(self.finalLongitude)&\("zoom=15&size=\(2 * Int(imgViewMap.frame.size.width))x\(2 * Int(imgViewMap.frame.size.height))")&sensor=true"
let mapUrl: NSURL = NSURL(string: staticMapUrl)!
self.imgViewMap.sd_setImage(with: mapUrl as URL, placeholderImage: UIImage(named: "palceholder"))
NSString *staticMapUrl = [NSString stringWithFormat:#"http://maps.google.com/maps/api/staticmap?markers=color:blue|%#,%#&%#&sensor=true",self.staticData.latitude, self.staticData.longitude, [NSString stringWithFormat:#"zoom=13&size=%dx%d",2*(int)mapFrame.size.width, 2*(int)mapFrame.size.height]];
NSURL *mapUrl = [NSURL URLWithString:[staticMapUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:mapUrl]];
UIImageView *mapImage = [[UIImageView alloc] initWithFrame:mapFrame];
This should help.
Using Swift 3:
let lat = ..
let long = ..
let staticMapUrl: String = "http://maps.google.com/maps/api/staticmap?markers=color:red|\(lat),\(long)&\("zoom=13&size=\(2 * Int(cell.imgAddress.frame.size.width))x\(2 * Int(cell.imgAddress.frame.size.height))")&sensor=true"
let url = URL(string: staticMapUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
do {
let data = try NSData(contentsOf: url!, options: NSData.ReadingOptions())
cell.imgAddress.image = UIImage(data: data as Data)
} catch {
cell.imgAddress.image = UIImage()
}
Try this. Note you have to get API key from Google cloud
let API_Key = //Your API Key.
let url = "https://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=13&size=\(2 * Int(imgBanner.frame.size.width))x\(2 * Int(imgBanner.frame.size.height))&maptype=roadmap&key=\(API_Key)"
let mapUrl: NSURL = NSURL(string: staticMapUrl)!
self.imgBanner.sd_setImage(with: mapUrl as URL, placeholderImage: UIImage(named: "palceholder"))
Always check the link in browser to view whether it is working fine or not means in the image is visible or not.
var friendRQ: FBRequest = FBRequest(forGraphPath: "me/taggable_friends?fields=name,picture.width(66).height(66)")
friendRQ.startWithCompletionHandler({ (Connection, results, error) -> Void in
self.FBData = results.objectForKey("data") as NSMutableArray
println(self.FBData)
})
I have no problem on retrieve the name, but picture.
FBData.picture reutrns a NSURL
picture = {
data = {
height = 68;
"is_silhouette" = 0;
url = "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpa1/v/t1.0-1/c9.0.68.68/p68x68/10410849_1410555062563816_XXXXX70293207008499_n.jpg?oh=ddd9e2d36cc13bc0095b243097a199b4&oe=54FC0162&__gda__=1427125899_dc78acc5e14b7d25aee153ccf8bb1543";
width = 68;
};
};
how can I show this picture properly in an ImageView?
var picture: NSURL = FBFriendHelper.sharedInstance.FBData[indexPath.row].picture
var data: AnyObject? = picture.valueForKey("data")
and then?
var FBpicture: NSURL = FBHelper.sharedInstance.FBData[indexPath.row].picture
var url: NSURL = NSURL(string: String(FBpicture.valueForKey("data")?.url! as NSString) as String)!
var picData: NSData = NSData(contentsOfURL: url)!
cell.FBProfile.image = UIImage(data: picData)
I found the solution of mine code