1

Hi I am new in development of iOS. I make class named socialClass.h

#import <Social/Social.h>
#import <Accounts/Accounts.h>

- (void)facebookSharingStatusCheckCall:(NSString *)url image:(NSString *)img status:(NSString *)text viewCont:(UIViewController *)uiVew;

- (void)facebookForIosSix:(NSString *)url image:(NSString *)img status:(NSString *)text viewCont:(UIViewController *)uiVew;

and in socialClass.m

    - (void)facebookSharingStatusCheckCall:(NSString *)url image:(NSString *)img status:(NSString *)text viewCont:(UIViewController *)uiVew 
{
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
        NSLog(@"service available");

     [self facebookForIosSix:url image:img status:text viewCont:uiVew];

    } else {
        // facebook for iOS 5 function will be called here
    }
}


-(void)facebookForIosSix:(NSString *)url image:(NSString *)img status:(NSString *)text viewCont:(UIViewController *)uiVew
{


if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {

        SLComposeViewController *fbcontroller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

    SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
        if (result == SLComposeViewControllerResultCancelled) {

                NSLog(@"Post Cancelled");

            } else

            {
                NSLog(@"Post Done");
            }

            [fbcontroller dismissViewControllerAnimated:YES completion:Nil];
        };
        fbcontroller.completionHandler =myBlock;

        //Adding the Text to the facebook post value from iOS
        [fbcontroller setInitialText:text];

        //Adding the URL to the facebook post value from iOS

        [fbcontroller addURL:[NSURL URLWithString:url]];

        //Adding the Image to the facebook post value from iOS

        [fbcontroller addImage:[UIImage imageNamed:img]];
        [uiVew presentViewController:fbcontroller animated:YES completion:nil];


    }
    else{
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Sorry"
                                  message:@"You can't Post a Status right now, make sure                                  your device has an internet connection and you have                                  at least one Twitter account setup"
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];

        NSLog(@"UnAvailable");
    }


}

Now I call in mainViewController.h

#import "socialClass.h"

and in mainViewController.m

- (IBAction)facebookShareBtn:(id)sender {

    [[socialClass facebookSharingStatusCheckCall:[[DataPassing sharedManager] pageURL ]
        image:[[DataPassing sharedManager]
        pageIMGurl]
        status:[[DataPassing sharedManager] pageTEXT]]
        viewCont:(UIViewController*)self];
}

When I use this below code with self it gives error. Definitely because there is no self view

[self presentViewController:fbcontroller animated:YES completion:nil]; so i put uiView in function and try to pass self from mainViewController. again error in mainview

How can I use this facebook from other calls function in mainviewcontroller. thx

[EDIT]

i declare socialClass.h in mainViewController and call the function in button [socialClass facebookForIosSix:...]

enter image description here

This error is comming... after updating to (UIViewController*)self

4

1 回答 1

5

好的,在你的函数定义中有几件事需要改变。

首先你需要一个 - 在前面而不是一个 +。加号表示类方法,而 - 表示实例方法。您正在像使用实例方法一样使用它。

其次,您需要在 SocialClass.h 文件中包含 MainViewController.h。然后将函数命名为...

- (void)facebookForIosSix:(NSString *)url image:(NSString *)img status:(NSString *)text viewCont:(MainViewController *)mainViewController;

然后在 mainViewController 你可以运行...

[socialViewController facebookSharingStatusCheckCall:@"url"  image:@"hello.png" status:@"Posting from App" viewCont:self];

这应该可以正常工作。

编辑

如果您想从多个地方运行该功能,则不要导入 MainViewController.h 并定义该功能,例如...

- (void)facebookForIosSix:(NSString *)url image:(NSString *)img status:(NSString *)text viewCont:(UIViewController *)viewController;

然后像...一样运行它

[socialViewController facebookSharingStatusCheckCall:@"url"  image:@"hello.png" status:@"Posting from App" viewCont:(UIViewController*)self];

TBH,我认为你根本不需要施放它,但只是为了安全起见。

编辑

你还没有平衡你的括号...

- (IBAction)facebookShareBtn:(id)sender {

    [socialClass facebookSharingStatusCheckCall:[[DataPassing sharedManager] pageURL ]
        image:[[DataPassing sharedManager] pageIMGurl]
        status:[[DataPassing sharedManager] pageTEXT]
        viewCont:(UIViewController*)self];
}

这将起作用。

于 2013-01-18T21:22:10.787 回答