xcode学习笔记

ins分享:

遇到很神奇的问题,游戏是横屏,用documentController处理分享,会出现 帖子没有键盘,快拍布局错误,按钮点不动的情况。
于是尝试使用 外链命令的方法,发现没有直接的图片上传命令可以用。
外链拉起后,默认选取相册第一张照片。 所以有了下面的尝试。

尝试了UIKit的方法

//UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), DocumentsPath);

发现保存成功,但是相册看不见。

尝试了异步

//                PHPhotoLibrary *library = [PHPhotoLibrary sharedPhotoLibrary];
//                [library performChanges:^
//                {
//                    [PHAssetChangeRequest creationRequestForAssetFromImageAtFileURL:url];
//                }
//                completionHandler:^(BOOL success, NSError * _Nullable error) {
//                    if (success)
//                    {
//
//                        NSLog(@"保存成功");
//                    }
//                    else
//                    {
//                        NSLog(@"保存失败");
//                    }
//                }];

发现了问题, 第一次调用会卡住一会儿,看log发现openURL警告,没有在主线程调用。
所以使用了同步的方法。

   NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];
    if([[UIApplication sharedApplication] canOpenURL:instagramURL]) //check for App is install or not
    {
        NSLog(@"------ %s", imgUrl);
        
        NSString* strImgUrl = [NSString stringWithUTF8String:imgUrl];
        NSString * docPath = @"Documents";
        NSString * finalPath = [docPath stringByAppendingPathComponent:strImgUrl];
        NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent: finalPath];
        
        NSLog(@"------ %@", DocumentsPath);
        
        NSURL* url = [NSURL URLWithString:DocumentsPath];
        NSError *error = nil;
          [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
                [PHAssetChangeRequest creationRequestForAssetFromImageAtFileURL:url];
            } error:&error];
        
        if(error == nil)
        {
            NSString *str = [NSString stringWithFormat:@"instagram://library?AssetPath=%@", DocumentsPath];
            NSURL *instagramURL = [NSURL URLWithString:str];
            [[UIApplication sharedApplication] openURL:instagramURL];
            
            NSLog(@"保存成功");
        }
        else
        {
            NSLog(@"保存失败");
        }
    }
    else
    {
        NSLog(@"Instagram not found");
    }

xcode静态链接 和 动态连接
测试没问题,上传testflight报错 ITMS-90205 或 ITMS-90206, 说 UnityFramework包含framework

这个是因为 静态包动态包 设置的不对。
新版的unity打包Xcode后会有 UnityFramework的子工程。
如果动态包放在这个子工程下,并且选择 Embed&Sign, 那么动态包最后输出.app的时候,就会出现 UnityFramework包含framework,下面是所有勾选 Embed&Sign的Framework。
然而 UnityFramework 本身也是动态包, 所以会出现 动态包 包含 动态包,这是不被允许的。

解决办法是 在unity里,选中.framework, 右侧面板,勾选“Add to Embedded Binaries”, 这样在输出Xcode的时候, 这些Unity里勾选的动态包和UnityFramework是在主工程下平级。
于是乎,上传成功
(出现这个问题,很大的原因,是第三方ios的SDK是手动裸接的。没用unity的一些后处理插件。问题不大,如此解决。)


Xcode Object-c 调用unity函数
UnitySendMessage( [字符串1 UTF8String], [字符串2 UTF8String], [字符串3 UTF8String]);
字符串都是 NSString* 类型

这个函数的定义是
void UnitySendMessage(const char* obj, const char* method, const char* msg);

第一个参数是 Unity里GameObject的名字
第二个参数是 函数名字
第三个参数是 参数

所以必须先有一个 MonoBehaviour的脚本挂在一个GameObject上,参数在定义上只给了一个,所以如果是多参,那么就自己组装一串带自定义分隔符的字符串,进到unity后,string.split就好了


unity调用Xcode object-c函数
using System.Runtime.InteropServices;

[DllImport("__Internal")] private static extern void 函数名(参数);

调用Xcode OC函数,返回string给unity

- (char*)GetDocumentPath
{
    NSString * docPath = @"Documents";
    NSString * strDocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent: docPath];   // 组装本地Url
    const char* cDocumentsPath = [strDocumentsPath UTF8String];
    
    char* result = (char*)malloc(strlen(cDocumentsPath) + 1);
    strcpy(result, cDocumentsPath);
    return result;
}

extern "C" char* iosGetDocumentPath()
{
    return [对象 GetDocumentPath];
}

返回出去的是char* 对应 unity里的string。 return前需要给返回值char*开内存,不然会报错。


程序学无止尽。
欢迎大家沟通,有啥不明确的,或者不对的,也可以和我私聊
我的QQ 334524067 神一般的狄狄

猜你喜欢

转载自blog.csdn.net/qq_37776196/article/details/121406332